FULL PRODUCT VERSION :
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
Runtime hooks added by Runtime.getRuntime().addShutdownHook(...) are not executed when closing a java console by "X" button of the console window.
It's working well by pressing CTRL-C.
It'a also working well when pressing "X" running at Windows XP Professional SP3 or Windows 2003 Server.
I was always able to reproduce the problem with JDK 1.6.0_17, 1.6.0_23 and JDK 1.6.0_26. I've not tested it with older JDK versions.
We're also using a C-wrapper where we're able to install java applications as Windows service, and here we've seen that the events sent by Windows seem to be the same in all the above mentioned OS versions. By pressing CTRL-C we're always getting CTRL_C_EVENT to our by the C-code installed ConsoleHandler, by closing the console we're getting a CTRL_CLOSE_EVENT.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute following steps with class from submitted test code:
1. Run the code above inside a console (cmd) with java.exe.
2. You should see following output:
will add shutdown hook...
shutdown hook added, will sleep for 15 seconds. Stop application in this time...
3. Now close the console by pressing "X" button of the console window.
(You can see how it should behave by pressing CTRL-C instead of closing console window - it should behave in same way)
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The application should print following output:
shutdown hook is currently executed...
will sleep for 5 seconds...
ACTUAL -
The application is stopped immediately without any output or sleeping for 5 seconds before closing the window.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.concurrent.TimeUnit;
public class ShutdownHookTest
{
public static void main(String[] args) throws InterruptedException
{
System.out.println("will add shutdown hook...");
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
try
{
System.out.println("shutdown hook is currently executed...");
}
finally
{
try
{
System.out.println("will sleep for 5 seconds...");
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
}
catch (InterruptedException P_ex)
{
// ignore
}
}
}
});
System.out.println("shutdown hook added, will sleep for 15 seconds. Stop application in this time...");
Thread.sleep(TimeUnit.SECONDS.toMillis(15));
}
}
---------- END SOURCE ----------
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
Runtime hooks added by Runtime.getRuntime().addShutdownHook(...) are not executed when closing a java console by "X" button of the console window.
It's working well by pressing CTRL-C.
It'a also working well when pressing "X" running at Windows XP Professional SP3 or Windows 2003 Server.
I was always able to reproduce the problem with JDK 1.6.0_17, 1.6.0_23 and JDK 1.6.0_26. I've not tested it with older JDK versions.
We're also using a C-wrapper where we're able to install java applications as Windows service, and here we've seen that the events sent by Windows seem to be the same in all the above mentioned OS versions. By pressing CTRL-C we're always getting CTRL_C_EVENT to our by the C-code installed ConsoleHandler, by closing the console we're getting a CTRL_CLOSE_EVENT.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute following steps with class from submitted test code:
1. Run the code above inside a console (cmd) with java.exe.
2. You should see following output:
will add shutdown hook...
shutdown hook added, will sleep for 15 seconds. Stop application in this time...
3. Now close the console by pressing "X" button of the console window.
(You can see how it should behave by pressing CTRL-C instead of closing console window - it should behave in same way)
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The application should print following output:
shutdown hook is currently executed...
will sleep for 5 seconds...
ACTUAL -
The application is stopped immediately without any output or sleeping for 5 seconds before closing the window.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.concurrent.TimeUnit;
public class ShutdownHookTest
{
public static void main(String[] args) throws InterruptedException
{
System.out.println("will add shutdown hook...");
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
try
{
System.out.println("shutdown hook is currently executed...");
}
finally
{
try
{
System.out.println("will sleep for 5 seconds...");
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
}
catch (InterruptedException P_ex)
{
// ignore
}
}
}
});
System.out.println("shutdown hook added, will sleep for 15 seconds. Stop application in this time...");
Thread.sleep(TimeUnit.SECONDS.toMillis(15));
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8079631 ShutdownHook not run by Windows Logout / Shutdown
-
- Closed
-
- relates to
-
JDK-8253445 Java applications on Windows do not execute shutdown hooks on system reboot/shutdown or user log-off
-
- New
-