Bug 52721

Summary: An incomplete fix for the resource leak bug in StandardContext.java
Product: Tomcat 6 Reporter: lianggt08
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED INVALID    
Severity: critical    
Priority: P2    
Version: unspecified   
Target Milestone: default   
Hardware: PC   
OS: All   

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 );
            oos.writeObject(this);
[line 4793]            oos.close();
[line 4794]            fos.close();
        } catch( Throwable t ) {
            if(log.isInfoEnabled())
                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.