JVM application shutdown hooks

Laurent Bourgès bourges.laurent at gmail.com
Wed Aug 7 12:08:58 UTC 2013


Dear core-libs members,

I finally succeed in diagnosing my shutdown hook issue in Java Web Start
environment: see Bug ID: 9005822.

Could you please give ma feedback to my questions related to LogManager and
StreamCloser shutdown hooks ?


This library uses java.util.logging to log warnings and exceptions but none
> is logged (console or java trace files).
>
> The 'lost' log messages is related to the LogManager's shutdown hook:
>     // This private class is used as a shutdown hook.
>     // It does a "reset" to close all open handlers.
>     private class Cleaner extends Thread {
>
>         private Cleaner() {
>             /* Set context class loader to null in order to avoid
>              * keeping a strong reference to an application classloader.
>              */
>             this.setContextClassLoader(null);
>         }
>
>         public void run() {
>             // This is to ensure the LogManager.<clinit> is completed
>             // before synchronized block. Otherwise deadlocks are possible.
>             LogManager mgr = manager;
>
>             // If the global handlers haven't been initialized yet, we
>             // don't want to initialize them just so we can close them!
>             synchronized (LogManager.this) {
>                 // Note that death is imminent.
>                 deathImminent = true;
>                 initializedGlobalHandlers = true;
>             }
>
>             // Do a reset to close all active handlers.
>             reset();
>         }
>     }
>
> I think it is annoying as it avoids me getting log and exceptions ...
>

Exactly what happened to me in JavaWS env !! I had to patch JSamp library
to add System.out and printStackTrace statements whereas there are plenty
of j.u.l. log statements !!

Please postpone that f*** hook after 'true' application hooks !!!


Moreover, as a RFE, it could be useful for applications to be able to
> define hook priorities within an application:
> I did that several times registering only 1 shutdown hook and handling
> correct ordering on my own ... but could be supported by the JDK itself.
>

Do you want me to submit a patch ? I think that the JVM specs should be
modified to change the addShutdownHook() signature => RFE ?


> Finally, here are (below) the Open JDK8 usages of Runtime.addShutdownHook
> [19 occurrences] which are more system hooks than application hooks:
> com.sun.imageio.stream
> StreamCloser.java
> StreamCloser
> addToQueue
> <anonymous java.security.PrivilegedAction>
> run
>   101:  Runtime.getRuntime().addShutdownHook(streamCloser);
> java.util.logging
> LogManager.java
> LogManager
> LogManager
>   255:  Runtime.getRuntime().addShutdownHook(new Cleaner());
> sun.font
> SunFontManager.java
> SunFontManager
> createFont2D
> <anonymous java.security.PrivilegedAction>
> run
> 2538:  Runtime.getRuntime().addShutdownHook(fileCloser);
> sun.rmi.server
> Activation.java
> Activation
> init
>   240:  Runtime.getRuntime().addShutdownHook(shutdownHook);
> sun.tools.jstat
>
> sun.awt.shell
> Win32ShellFolderManager2.java
> Win32ShellFolderManager2
> ComInvoker
> ComInvoker
> <anonymous java.security.PrivilegedAction<java.lang.Void>>
> run
>   493:  Runtime.getRuntime().addShutdownHook(
> sun.awt.windows
> WToolkit.java
> WToolkit
> registerShutdownHook
> <anonymous java.security.PrivilegedAction<java.lang.Void>>
> run
>   285:  Runtime.getRuntime().addShutdownHook(shutdown);
> sun.java2d.d3d
> D3DScreenUpdateManager.java
> D3DScreenUpdateManager
> D3DScreenUpdateManager
> <anonymous java.security.PrivilegedAction>
> run
>   112:  Runtime.getRuntime().addShutdownHook(shutdown);
> java.util.prefs
> FileSystemPreferences.java
> FileSystemPreferences
> <anonymous java.security.PrivilegedAction<java.lang.Void>>
> run
>   439:  Runtime.getRuntime().addShutdownHook(new Thread() {
> sun.awt.X11
> XToolkit.java
> XToolkit
> init
> <anonymous java.security.PrivilegedAction<java.lang.Void>>
> run
>   350:  Runtime.getRuntime().addShutdownHook(shutdownThread);
> sun.awt
> X11GraphicsDevice.java
> X11GraphicsDevice
> setDisplayMode
> <anonymous java.security.PrivilegedAction<java.lang.Void>>
> run
>   445:  Runtime.getRuntime().addShutdownHook(t);
> java.util.prefs
> MacOSXPreferencesFile.java
> MacOSXPreferencesFile
> timer
>   356:  Runtime.getRuntime().addShutdownHook(flushThread);
> sun.font
> CFontManager.java
> CFontManager
> createFont2D
> <anonymous java.security.PrivilegedAction<java.lang.Object>>
> run
>   232:  Runtime.getRuntime().addShutdownHook(fileCloser);
> sun.lwawt
> LWToolkit.java
> LWToolkit
> init
>     83:  Runtime.getRuntime().addShutdownHook(
>
> I hope that these hooks are already supporting concurrency execution (awt
> ?) but someone should check if there is no dependencies between them (like
> systemd or rc scripts does).
>

Could someone check if there is no dependencies between all these shutdown
hooks when they are executed in parallel with random order ?

Best regards,
Laurent



More information about the core-libs-dev mailing list