JDK-7092892 : DeleteOnExitHook throws ExceptionInInitializerError
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6u26
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2011-09-20
  • Updated: 2012-08-03
  • Resolved: 2012-01-10
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.0.6002]

A DESCRIPTION OF THE PROBLEM :
File.deleteOnExit throws undocumented ExceptionInInitializerError.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Be the first call to File.deleteOnExit from a Thread that is a shutdown hook.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The file is deleted at a safe point or an undocumented IllegalStateException.  See: 6831947
ACTUAL -
java.lang.ExceptionInInitializerError wrapping a IllegalStateException.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "Thread-0" java.lang.ExceptionInInitializerError
        at java.io.File.deleteOnExit(File.java:939)
        at ErrorOnDelete.close(ErrorOnDelete.java:33)
        at java.util.logging.LogManager.resetLogger(LogManager.java:682)
        at java.util.logging.LogManager.reset(LogManager.java:665)
        at java.util.logging.LogManager$Cleaner.run(LogManager.java:223)
Caused by: java.lang.IllegalStateException: Shutdown in progress
        at java.lang.Shutdown.add(Shutdown.java:62)
        at java.lang.System$2.registerShutdownHook(System.java:1146)
        at java.io.DeleteOnExitHook.<clinit>(DeleteOnExitHook.java:20)
        ... 5 more

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class ErrorOnDelete extends Handler {

    private static final Logger logger = Logger.getLogger("ErrorOnDelete");
    
    public static void main(String[] args) {
        logger.addHandler(new ErrorOnDelete());
    }

    @Override
    public void publish(LogRecord record) {
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() throws SecurityException {
        try {
            File f = File.createTempFile("error", ".txt");
            try {
                f.deleteOnExit();
            } finally {
                f.delete();
            }
        } catch(IOException IOE) {
            throw new AssertionError(IOE);
        }
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Trap all runtime exceptions and errors from any call to File.deleteOnExit.
Ensure that methods that can be called from application code or shutdown hooks avoid calling File.deleteOnExit.  That gets tricky when a File.deleteOnExit called is hidden inside some other 3rd party lib.