JDK-4631820 : Temporary files generated by Java applications remain when VM terminates
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.0_01
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-02-01
  • Updated: 2002-02-11
  • Resolved: 2002-02-11
Related Reports
Relates :  
Description
This bug has been filed to add the missing functionality for deleteOnExit
function in java.io.File class.

Whenever a temporary file is created by an application using "createTempFile" function of java.io.File, deleteOnExit function registers with the VM that it should be deleted when the VM exits.

However, this is valid only for normal termination of the VM. If a signal
terminates the VM, like Ctrl-C, the temporary file does not get deleted and hence the temporary files start getting adding up.

This behavior has been observed in Windows.

The attached test file, "tempfiletest.java" can be run in any Windows NT machine
after changing the third argument of line 10. This is to specify the directory
on the local machine where the temporary file needs to be generated.

After compiling and running the test program, it is observed that the temporary
file is generated while the for loop is running. However, if the VM terminates
normally, the temporary file gets deleted on it's own. If Ctrl-C is done while
the for loop is on, the temporary file remains behind.

Comments
WORK AROUND The workaround is to use java.io.File.delete instead of deleteOnExit. delete immediately removes the temporary file while deleteOnExit waits till the VM closes.
11-06-2004

SUGGESTED FIX If the VM terminates by a signal, the signal handlers in the VM need to be enabled to delete the file, exactly what the deleteOnExit function does.
11-06-2004

EVALUATION It's not just Ctrl-C. I noticed the temporary file does not get deleted even under normal program termination (NT4 with SP6). The following test program works fine under Solaris, but fails to delete "test.dat" on NT. However, 1.3.1 seems to work just fine. -------------------------------------------------------- import java.io.*; class test { public static void main(String [] args) { File newfile = new File("test.dat"); try { newfile.createNewFile(); } catch (IOException x) { x.printStackTrace(); } newfile.deleteOnExit(); } } -------------------------------------------------------- I checked VM shutdown code, the deleteOnExit hook has been installed and is invoked during shutdown. So it looks like the bug is in the hook itself. Transfering to java/classes_io. ###@###.### 2002-02-07 Hui's example is due to another bug 4614121, which has been fixed in Hopper and now is being backported to 1.4.0_01. The essence of the present bug is that temp files do not get deleted if VM exits on Ctrl-C. One can reproduce this behaviour by adding Thread.sleep() into the above program and pressing Ctrl-C. The bug affects 1.3.1 only, Hopper works OK. It is probably related to the shutdown hooks behaviour on Ctrl-C in 1.3.1 vs. Hopper - somewhere on the path between 1.3.1 and Hopper the bug has been fixed. Since it is not a high priority bug and Hopper works OK, the bug should be closed. ###@###.### 2002-02-07
07-02-2002

PUBLIC COMMENTS The essence of the present bug is that temp files do not get deleted if VM exits on Ctrl-C. One can reproduce this behaviour by adding Thread.sleep() into the above program and pressing Ctrl-C. The bug affects 1.3.1 only, Hopper works OK. It is probably related to the shutdown hooks behaviour on Ctrl-C in 1.3.1 vs. Hopper - somewhere on the path between 1.3.1 and Hopper the bug has been fixed. Since it is not a high priority bug and Hopper works OK, the bug should be closed. ###@###.### 2002-02-07
07-02-2002