Uploaded image for project: 'James Server'
  1. James Server
  2. JAMES-1381

An incomplete fix for the resource leak bugs in NNTPHandler.java

    XMLWordPrintableJSON

Details

    Description

      The fix revision 108172 was aimed to remove resource leak bugs on the BufferedReader object "reader" (created in line 308), the InternetPrintWriter object "writer" in the method "handleConnection"of the file "/james/server/trunk/src/java/org/apache/james/nntpserver/NNTPHandler.java (now moved to /james/server/branches/v2.3/src/java/org/apache/james/nntpserver/NNTPHandler.java)" , but it is incomplete.

      There are some problems:
      1. when "reader" isn't created successfully but the temp InputStreamReader object is created successfully (at line 308), the temp InputStreamReader object will be leaked.
      2. when "writer" isn't created successfully but the temp BufferedWriter object is created successfully (at line 309), the temp BufferedWriter object will be leaked.
      3. when the temp BufferedWriter object isn't created successfully but the temp OutputStreamWriter object is created successfully (at line 309), the temp OutputStreamWriter object will be leaked.

      The best way to close such resource objects is putting such close operations for all resource
      objects in the finaly block of a try-catch-finally structure and then putting all other code in a try block.

      The problem still exists in the head revision (the temp InputStreamReader object created at line 383 and the "outs" created at line 384 can be leaked). The buggy code is copied as bellows:

      public void handleConnection( Socket connection ) throws IOException {
      try {
      this.socket = connection;
      synchronized (this)

      { handlerThread = Thread.currentThread(); }

      remoteIP = socket.getInetAddress().getHostAddress();
      remoteHost = socket.getInetAddress().getHostName();
      in = new BufferedInputStream(socket.getInputStream(), 1024);
      // An ASCII encoding can be used because all transmissions other
      // that those in the message body command are guaranteed
      // to be ASCII
      383 reader = new BufferedReader(new InputStreamReader(in, "ASCII"), 512);
      384 outs = new BufferedOutputStream(socket.getOutputStream(), 1024);
      385 writer = new InternetPrintWriter(outs, true);
      } catch (Exception e)

      { ...... }

      try { ...... } catch (Exception e) { ...... }

      finally

      { 436 resetHandler(); }

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            guangtai Guangtai Liang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 10m
                10m
                Remaining:
                Remaining Estimate - 10m
                10m
                Logged:
                Time Spent - Not Specified
                Not Specified