Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8154017

Shutdown hooks are racing against shutdown sequence, if System.exit()-calling thread is interrupted

    Details

    • Subcomponent:
    • Resolved In Build:
      b125
    • Verification:
      Verified

      Backports

        Description

        This happens because ApplicationShutdownHooks.runHooks joins the shutdown hooks. This works normally if interrupt status is not set in advance. But if it does, we exit the loop early:

            static void runHooks() {
                Collection<Thread> threads;
                synchronized(ApplicationShutdownHooks.class) {
                    threads = hooks.keySet();
                    hooks = null;
                }

                for (Thread hook : threads) {
                    hook.start();
                }
                for (Thread hook : threads) {
                    try {
                        hook.join();
                    } catch (InterruptedException x) { } // oops, no join for you, falling through...
                }
            }

        ...and already started shutdown hooks begin to race against the rest of the shutdown sequence. Notably, this may lead to premature VM shutdown, with shutdown hooks not fully executed, as test demonstrates.

        ApplicationShutdownHooks is called from the thread that originally called System.exit:

        j java.lang.ApplicationShutdownHooks.runHooks()V+113
        j java.lang.ApplicationShutdownHooks$1.run()V+0
        j java.lang.Shutdown.runHooks()V+100
        j java.lang.Shutdown.sequence()V+26
        j java.lang.Shutdown.exit(I)V+95
        j java.lang.Runtime.exit(I)V+14
        j java.lang.System.exit(I)V+4
        j Main.main([Ljava/lang/String;)V+20

        Full jtreg test:
          http://cr.openjdk.java.net/~shade/8154017/ShutdownInterruptedMain.java

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  chegar Chris Hegarty
                  Reporter:
                  shade Aleksey Shipilev
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  8 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: