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

ApplicationShutdownHook: Thread.isAlive() is not equivalent to not being startable

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 13
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:
      None

      Description

      As reported by Thomas Stuefe in:

      http://mail.openjdk.java.net/pipermail/hotspot-runtime-dev/2019-April/033512.html

      ApplicationShutdownHook.java:

      static synchronized void add(Thread hook) {
          if(hooks == null)
              throw new IllegalStateException("Shutdown in progress");

          if (hook.isAlive())
              throw new IllegalArgumentException("Hook already running");

          if (hooks.containsKey(hook))
              throw new IllegalArgumentException("Hook previously registered");

          hooks.put(hook, hook);
      }

      would register a terminated thread as shutdown hook.
      ---

      When start that hook we would get an IllegalThreadStateException that would terminate the hook starting and joining logic:

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

              for (Thread hook : threads) {
                  hook.start(); <= // BOOM!
              }
              for (Thread hook : threads) {
                  while (true) {
                      try {
                          hook.join();
                          break;
                      } catch (InterruptedException ignored) {
                      }
                  }
              }
          }

      We need to check for getState() == Thread.State.NEW

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              chegar Chris Hegarty
              Reporter:
              dholmes David Holmes
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: