Bug 52721 - An incomplete fix for the resource leak bug in StandardContext.java
Summary: An incomplete fix for the resource leak bug in StandardContext.java
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Catalina (show other bugs)
Version: unspecified
Hardware: PC All
: P2 critical (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2012-02-21 10:00 UTC by lianggt08
Modified: 2012-03-09 20:20 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description lianggt08 2012-02-21 10:00:19 UTC
The fix revision 423920 was aimed to remove an resource leak bug on the  FileOutputStream object "fos"and the ObjectOutputStream "oos"  in the method "cacheContext" of the file "/tomcat/tc6.0.x/trunk/java/org/apache/catalina/core/StandardContext.java" , but it is incomplete. 

When the statements at lines 4792 throw any eception, the objects "fos" and "oos" can not be closed as expected. The best way to close such resource object is putting such close operations in the finaly block of a try-catch-finally structure.

Besides that, when oos is created unsuccessfully but "fos"  is created successfully,  "fos" will also be leaked. 

The buggy code in the head revision is copied as bellows:

 private void cacheContext() {
        try {
            File workDir=new File( getWorkPath() );
            File ctxSer=new File( workDir, "_tomcat_context.ser");
            FileOutputStream fos=new FileOutputStream( ctxSer );
            ObjectOutputStream oos=new ObjectOutputStream( fos );
[line 4793]            oos.close();
[line 4794]            fos.close();
        } catch( Throwable t ) {
                log.info("Error saving context.ser ", t);
Comment 1 Mark Thomas 2012-03-09 20:20:04 UTC
That method is unused. Since it is private, it could be removed. There is no issue here.