6829503: addShutdownHook fails if called after shutdown has commenced
Allow shutdown hook to be added during shutdown and handle properly if it fails to add Reviewed-by: alanb, dholmes, martin
This commit is contained in:
parent
b5ace034c3
commit
f7b87611c6
@ -503,11 +503,12 @@ public final class Console implements Flushable
|
|||||||
|
|
||||||
// Set up JavaIOAccess in SharedSecrets
|
// Set up JavaIOAccess in SharedSecrets
|
||||||
static {
|
static {
|
||||||
|
try {
|
||||||
// Add a shutdown hook to restore console's echo state should
|
// Add a shutdown hook to restore console's echo state should
|
||||||
// it be necessary.
|
// it be necessary.
|
||||||
sun.misc.SharedSecrets.getJavaLangAccess()
|
sun.misc.SharedSecrets.getJavaLangAccess()
|
||||||
.registerShutdownHook(0 /* shutdown hook invocation order */,
|
.registerShutdownHook(0 /* shutdown hook invocation order */,
|
||||||
|
false /* only register if shutdown is not in progress */,
|
||||||
new Runnable() {
|
new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
@ -517,6 +518,10 @@ public final class Console implements Flushable
|
|||||||
} catch (IOException x) { }
|
} catch (IOException x) { }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// shutdown is already in progress and console is first used
|
||||||
|
// by a shutdown hook
|
||||||
|
}
|
||||||
|
|
||||||
sun.misc.SharedSecrets.setJavaIOAccess(new sun.misc.JavaIOAccess() {
|
sun.misc.SharedSecrets.setJavaIOAccess(new sun.misc.JavaIOAccess() {
|
||||||
public Console console() {
|
public Console console() {
|
||||||
|
@ -34,23 +34,31 @@ import java.io.File;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class DeleteOnExitHook {
|
class DeleteOnExitHook {
|
||||||
|
private static LinkedHashSet<String> files = new LinkedHashSet<String>();
|
||||||
static {
|
static {
|
||||||
|
// DeleteOnExitHook must be the last shutdown hook to be invoked.
|
||||||
|
// Application shutdown hooks may add the first file to the
|
||||||
|
// delete on exit list and cause the DeleteOnExitHook to be
|
||||||
|
// registered during shutdown in progress. So set the
|
||||||
|
// registerShutdownInProgress parameter to true.
|
||||||
sun.misc.SharedSecrets.getJavaLangAccess()
|
sun.misc.SharedSecrets.getJavaLangAccess()
|
||||||
.registerShutdownHook(2 /* Shutdown hook invocation order */,
|
.registerShutdownHook(2 /* Shutdown hook invocation order */,
|
||||||
|
true /* register even if shutdown in progress */,
|
||||||
new Runnable() {
|
new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
runHooks();
|
runHooks();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
private static LinkedHashSet<String> files = new LinkedHashSet<String>();
|
}
|
||||||
|
|
||||||
private DeleteOnExitHook() {}
|
private DeleteOnExitHook() {}
|
||||||
|
|
||||||
static synchronized void add(String file) {
|
static synchronized void add(String file) {
|
||||||
if(files == null)
|
if(files == null) {
|
||||||
|
// DeleteOnExitHook is running. Too late to add a file
|
||||||
throw new IllegalStateException("Shutdown in progress");
|
throw new IllegalStateException("Shutdown in progress");
|
||||||
|
}
|
||||||
|
|
||||||
files.add(file);
|
files.add(file);
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,26 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class ApplicationShutdownHooks {
|
class ApplicationShutdownHooks {
|
||||||
|
/* The set of registered hooks */
|
||||||
|
private static IdentityHashMap<Thread, Thread> hooks;
|
||||||
static {
|
static {
|
||||||
|
try {
|
||||||
Shutdown.add(1 /* shutdown hook invocation order */,
|
Shutdown.add(1 /* shutdown hook invocation order */,
|
||||||
|
false /* not registered if shutdown in progress */,
|
||||||
new Runnable() {
|
new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
runHooks();
|
runHooks();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
hooks = new IdentityHashMap<Thread, Thread>();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// application shutdown hooks cannot be added if
|
||||||
|
// shutdown is in progress.
|
||||||
|
hooks = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The set of registered hooks */
|
|
||||||
private static IdentityHashMap<Thread, Thread> hooks = new IdentityHashMap<Thread, Thread>();
|
|
||||||
|
|
||||||
private ApplicationShutdownHooks() {}
|
private ApplicationShutdownHooks() {}
|
||||||
|
|
||||||
|
@ -53,6 +53,9 @@ class Shutdown {
|
|||||||
private static final int MAX_SYSTEM_HOOKS = 10;
|
private static final int MAX_SYSTEM_HOOKS = 10;
|
||||||
private static final Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS];
|
private static final Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS];
|
||||||
|
|
||||||
|
// the index of the currently running shutdown hook to the hooks array
|
||||||
|
private static int currentRunningHook = 0;
|
||||||
|
|
||||||
/* The preceding static fields are protected by this lock */
|
/* The preceding static fields are protected by this lock */
|
||||||
private static class Lock { };
|
private static class Lock { };
|
||||||
private static Object lock = new Lock();
|
private static Object lock = new Lock();
|
||||||
@ -68,17 +71,39 @@ class Shutdown {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Add a new shutdown hook. Checks the shutdown state and the hook itself,
|
/**
|
||||||
|
* Add a new shutdown hook. Checks the shutdown state and the hook itself,
|
||||||
* but does not do any security checks.
|
* but does not do any security checks.
|
||||||
|
*
|
||||||
|
* The registerShutdownInProgress parameter should be false except
|
||||||
|
* registering the DeleteOnExitHook since the first file may
|
||||||
|
* be added to the delete on exit list by the application shutdown
|
||||||
|
* hooks.
|
||||||
|
*
|
||||||
|
* @params slot the slot in the shutdown hook array, whose element
|
||||||
|
* will be invoked in order during shutdown
|
||||||
|
* @params registerShutdownInProgress true to allow the hook
|
||||||
|
* to be registered even if the shutdown is in progress.
|
||||||
|
* @params hook the hook to be registered
|
||||||
|
*
|
||||||
|
* @throw IllegalStateException
|
||||||
|
* if registerShutdownInProgress is false and shutdown is in progress; or
|
||||||
|
* if registerShutdownInProgress is true and the shutdown process
|
||||||
|
* already passes the given slot
|
||||||
*/
|
*/
|
||||||
static void add(int slot, Runnable hook) {
|
static void add(int slot, boolean registerShutdownInProgress, Runnable hook) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (state > RUNNING)
|
|
||||||
throw new IllegalStateException("Shutdown in progress");
|
|
||||||
|
|
||||||
if (hooks[slot] != null)
|
if (hooks[slot] != null)
|
||||||
throw new InternalError("Shutdown hook at slot " + slot + " already registered");
|
throw new InternalError("Shutdown hook at slot " + slot + " already registered");
|
||||||
|
|
||||||
|
if (!registerShutdownInProgress) {
|
||||||
|
if (state > RUNNING)
|
||||||
|
throw new IllegalStateException("Shutdown in progress");
|
||||||
|
} else {
|
||||||
|
if (state > HOOKS || (state == HOOKS && slot <= currentRunningHook))
|
||||||
|
throw new IllegalStateException("Shutdown in progress");
|
||||||
|
}
|
||||||
|
|
||||||
hooks[slot] = hook;
|
hooks[slot] = hook;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,11 +111,15 @@ class Shutdown {
|
|||||||
/* Run all registered shutdown hooks
|
/* Run all registered shutdown hooks
|
||||||
*/
|
*/
|
||||||
private static void runHooks() {
|
private static void runHooks() {
|
||||||
/* We needn't bother acquiring the lock just to read the hooks field,
|
for (int i=0; i < MAX_SYSTEM_HOOKS; i++) {
|
||||||
* since the hooks can't be modified once shutdown is in progress
|
|
||||||
*/
|
|
||||||
for (Runnable hook : hooks) {
|
|
||||||
try {
|
try {
|
||||||
|
Runnable hook;
|
||||||
|
synchronized (lock) {
|
||||||
|
// acquire the lock to make sure the hook registered during
|
||||||
|
// shutdown is visible here.
|
||||||
|
currentRunningHook = i;
|
||||||
|
hook = hooks[i];
|
||||||
|
}
|
||||||
if (hook != null) hook.run();
|
if (hook != null) hook.run();
|
||||||
} catch(Throwable t) {
|
} catch(Throwable t) {
|
||||||
if (t instanceof ThreadDeath) {
|
if (t instanceof ThreadDeath) {
|
||||||
|
@ -1171,8 +1171,8 @@ public final class System {
|
|||||||
public void blockedOn(Thread t, Interruptible b) {
|
public void blockedOn(Thread t, Interruptible b) {
|
||||||
t.blockedOn(b);
|
t.blockedOn(b);
|
||||||
}
|
}
|
||||||
public void registerShutdownHook(int slot, Runnable r) {
|
public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
|
||||||
Shutdown.add(slot, r);
|
Shutdown.add(slot, registerShutdownInProgress, hook);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,22 @@ public interface JavaLangAccess {
|
|||||||
/** Set thread's blocker field. */
|
/** Set thread's blocker field. */
|
||||||
void blockedOn(Thread t, Interruptible b);
|
void blockedOn(Thread t, Interruptible b);
|
||||||
|
|
||||||
/** register shutdown hook */
|
/**
|
||||||
void registerShutdownHook(int slot, Runnable r);
|
* Registers a shutdown hook.
|
||||||
|
*
|
||||||
|
* It is expected that this method with registerShutdownInProgress=true
|
||||||
|
* is only used to register DeleteOnExitHook since the first file
|
||||||
|
* may be added to the delete on exit list by the application shutdown
|
||||||
|
* hooks.
|
||||||
|
*
|
||||||
|
* @params slot the slot in the shutdown hook array, whose element
|
||||||
|
* will be invoked in order during shutdown
|
||||||
|
* @params registerShutdownInProgress true to allow the hook
|
||||||
|
* to be registered even if the shutdown is in progress.
|
||||||
|
* @params hook the hook to be registered
|
||||||
|
*
|
||||||
|
* @throw IllegalStateException if shutdown is in progress and
|
||||||
|
* the slot is not valid to register.
|
||||||
|
*/
|
||||||
|
void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook);
|
||||||
}
|
}
|
||||||
|
69
jdk/test/java/lang/Runtime/shutdown/ShutdownHooks.java
Normal file
69
jdk/test/java/lang/Runtime/shutdown/ShutdownHooks.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
||||||
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
||||||
|
* have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @bug 6829503
|
||||||
|
* @summary 1) Test Console and DeleteOnExitHook can be initialized
|
||||||
|
* while shutdown is in progress
|
||||||
|
* 2) Test if files that are added by the application shutdown
|
||||||
|
* hook are deleted on exit during shutdown
|
||||||
|
*/
|
||||||
|
import java.io.*;
|
||||||
|
public class ShutdownHooks {
|
||||||
|
private static File file;
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (args.length != 2) {
|
||||||
|
throw new IllegalArgumentException("Usage: ShutdownHooks <dir> <filename>");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a shutdown hook
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Cleaner());
|
||||||
|
|
||||||
|
File dir = new File(args[0]);
|
||||||
|
file = new File(dir, args[1]);
|
||||||
|
// write to file
|
||||||
|
System.out.println("writing to "+ file);
|
||||||
|
PrintWriter pw = new PrintWriter(file);
|
||||||
|
pw.println("Shutdown begins");
|
||||||
|
pw.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Cleaner extends Thread {
|
||||||
|
public void run() {
|
||||||
|
// register the Console's shutdown hook while the application
|
||||||
|
// shutdown hook is running
|
||||||
|
Console cons = System.console();
|
||||||
|
// register the DeleteOnExitHook while the application
|
||||||
|
// shutdown hook is running
|
||||||
|
file.deleteOnExit();
|
||||||
|
try {
|
||||||
|
PrintWriter pw = new PrintWriter(file);
|
||||||
|
pw.println("file is being deleted");
|
||||||
|
pw.close();
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
57
jdk/test/java/lang/Runtime/shutdown/ShutdownHooks.sh
Normal file
57
jdk/test/java/lang/Runtime/shutdown/ShutdownHooks.sh
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
|
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
#
|
||||||
|
# This code is free software; you can redistribute it and/or modify it
|
||||||
|
# under the terms of the GNU General Public License version 2 only, as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
#
|
||||||
|
# This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
# version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
# accompanied this code).
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License version
|
||||||
|
# 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
#
|
||||||
|
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
||||||
|
# CA 95054 USA or visit www.sun.com if you need additional information or
|
||||||
|
# have any questions.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
# @test
|
||||||
|
# @bug 6829503
|
||||||
|
# @summary 1) Test Console and DeleteOnExitHook can be initialized
|
||||||
|
# while shutdown is in progress
|
||||||
|
# 2) Test if files that are added by the application shutdown
|
||||||
|
# hook are deleted on exit during shutdown
|
||||||
|
#
|
||||||
|
# @build ShutdownHooks
|
||||||
|
# @run shell ShutdownHooks.sh
|
||||||
|
|
||||||
|
if [ "${TESTJAVA}" = "" ]
|
||||||
|
then
|
||||||
|
echo "TESTJAVA not set. Test cannot execute. Failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
FILENAME=fileToBeDeleted
|
||||||
|
rm -f ${TESTCLASSES}/${FILENAME}
|
||||||
|
|
||||||
|
# create the file to be deleted on exit
|
||||||
|
echo "testing shutdown" > ${TESTCLASSES}/${FILENAME}
|
||||||
|
|
||||||
|
${TESTJAVA}/bin/java ${TESTVMOPTS} -classpath ${TESTCLASSES} ShutdownHooks ${TESTCLASSES} $FILENAME
|
||||||
|
if [ $? != 0 ] ; then
|
||||||
|
echo "Test Failed"; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f ${TESTCLASSES}/${FILENAME} ]; then
|
||||||
|
echo "Test Failed: ${TESTCLASSES}/${FILENAME} not deleted"; exit 2
|
||||||
|
fi
|
||||||
|
echo "ShutdownHooks test passed.";
|
Loading…
Reference in New Issue
Block a user