Bug 40326 - DefaultServlet uses File.deleteOnExit() which is known to leak jvm native memory
DefaultServlet uses File.deleteOnExit() which is known to leak jvm native memory
Status: RESOLVED FIXED
Product: Tomcat 5
Classification: Unclassified
Component: Catalina
5.5.20
All All
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2006-08-25 20:43 UTC by quartz
Modified: 2006-12-25 05:32 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description quartz 2006-08-25 20:43:46 UTC
DefaultServlet uses File.deleteOnExit() which is known to leak jvm native memory.
The call is made on executePartialPut() (called from doPut()).

Any server running for a long time will not get rid of these orphan requests to
delete tmp PUT files on exit and may run out of memory if PUTting many files.

The solution is to completely avoid such method. You must deal manually with the
deletion of unused files. You can use try{create+use}finally{delete}.

If it is done in many phases (I'm not sure what partial PUT means) you must
associate that file to the httpsession so that the httpsession disposal will
delete the tmp file safely. If there is no session ongoing you must deny
multiphase PUT to stay safe.

In any case, partial PUT or not, a jvm crash may leave those tmp files
undeleted, so it would be safer to assign a directory to tomcat tmpfiles
(already exists I think) and wipe it clean on tomcat start. If you don't want to
delete all files, then choose filenames that have a pattern unlikely to clash
with existing files. Better, choose filenames that you can assert they came from
tomcat.

example: _tmp_tomcat_[userfilename]_[privatehashOf(userfilename)].tmp
when you read the "userfilename", only tomcat can rehash and compare with the
private hash number. If the hash number is not equal, it is not a tomcat tmp
file. This makes it really hard for a user to accidentally loose a file in that
tmp folder.
Comment 1 Yoav Shapira 2006-12-25 05:20:32 UTC
See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817 for the Sun Java
BugParade reference on this bug.
Comment 2 Yoav Shapira 2006-12-25 05:23:01 UTC
Also updating version to latest 5.5, as the same code is still there
(org.apache.catalina.servlets.DefaultServlet#executePartialPut, line 494).

The method returns a handle to the file, so it's not a method-local fix.
Comment 3 Yoav Shapira 2006-12-25 05:32:26 UTC
Actually, looking at the code a little further, the fix is simpler than I
thought.  I've just committed it to SVN after a bit of local testing, and it
looks fine, but I'd like further testing.  I'm going to ask on the dev list as
well, but if you could test, that'd be great.