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

ShutdownHook not run by Windows Logout / Shutdown

XMLWordPrintable

    • x86_64
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      A Shutdown Hook should be run at the begin of the shutdown sequence, when " The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown." (source: javadoc).

      In the current version 1.8.0_40 as well as in olders (1.7.0_67 per exemple), it is *not* started by the user logoff or system shutdown of Windows.

      The ^C runs as well.

      JUSTIFICATION :
      If some code is to be run when the application ends, it should be the case by the logout of the user too. Windows send a signal to all running applications and wait some time (Registry entry WaitToKillAppTimeout) before the application did really end.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The Shutdown Hook should be started and run. The java application should only ends after all hooks have terminated or the Windows Operating System has killed the virtual machine (after WaitToKillAppTimeout).

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Date;

      /**
       *
       */
      public class TestShutdown
      {
      /** Date format for the output */
      private final static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

      /** File to write in */
      final FileOutputStream fos;

      /** Constructor */
      private TestShutdown(final String filename) throws FileNotFoundException
      {
      fos = new FileOutputStream(new File(filename));
      }

      /**
      * Write the current time and a text in {@link #fos}, flush it and wait 100 ms before return.
      * @param text text to write in the file
      */
      private void writeAndWait(final String text )
      {
      final Object o = new Object();
      try
      {
      try
      {
      final String date = df.format(new Date());
      fos.write((date + " " + text + "\n").getBytes());
      fos.flush();
      synchronized (o)
      {
      o.wait(100l);
      }
      }
      catch (final InterruptedException ex)
      {
      fos.write((text + "Normal Thread. Time " + System.currentTimeMillis()).getBytes());
      }
      }
      catch (IOException e)
      {
      System.out.println(e.toString());
      }
      }

      /**
      * Main function
      * @param args the first is the filename (with path) we will write in
      */
      public static void main (final String[] args) throws FileNotFoundException
      {
      final TestShutdown test = new TestShutdown(args[0]);
      Runtime.getRuntime().addShutdownHook(test.new HookThread());
      while (true)
      {
      test.writeAndWait("Main Thread ");
      }
      }

      /**
      * The hook Thread
      */
      final class HookThread extends Thread
      {
      @Override
      public void run()
      {
      for (int i = 0; i<5; i++)
      writeAndWait("Hook Thread " + i + " ");

      try
      {
      fos.write("End of hook thread".getBytes());
      fos.flush();
      fos.close();
      }
      catch (IOException e)
      {
      System.out.println(e);
      }
      }

      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Workaround is to exit the application before logging out or shutdown the machine, which is not praticable.

        1. TestShutdown.java
          2 kB
        2. 9ea152_dumpfile_win7_shutdown
          2 kB
        3. 9ea152_dumpfile_win7_logoff
          2 kB
        4. 9ea152_dumpfile_win7_ctrl+c
          1 kB
        5. 8u40_dumpfile_win7_shutdown
          1 kB
        6. 8u40_dumpfile_win7_logoff
          2 kB
        7. 8u40_dumpfile_win7_ctrl+c
          1 kB
        8. 8u121_dumpfile-win7-shutdown
          16 kB
        9. 8u121_dumpfile-win7-logoff
          2 kB
        10. 8u121_dumpfile-win7-ctrl+C
          3 kB

            robm Robert Mckenna
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: