From 446987e20aacd870e1bc3f950b2bd4bbda1f9571 Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Thu, 21 Jan 2016 20:08:24 +0530 Subject: [PATCH] HADOOP-12423. Handle failure of registering shutdownhook by ShutdownHookManager in static block (Contributed by Abhishek Agarwal) --- .../hadoop-common/CHANGES.txt | 3 ++ .../hadoop/util/ShutdownHookManager.java | 31 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 85f304ab14a..e775f1b9219 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -1630,6 +1630,9 @@ Release 2.8.0 - UNRELEASED HADOOP-12356. Fix computing CPU usage statistics on Windows. (Inigo Goiri via wangda) + HADOOP-12423. Handle failure of registering shutdownhook by + ShutdownHookManager in static block (Abhishek Agarwal via vinayakumarb) + Release 2.7.3 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java index 85533dbffae..843454b08de 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java @@ -44,22 +44,27 @@ public class ShutdownHookManager { private static final Log LOG = LogFactory.getLog(ShutdownHookManager.class); static { - Runtime.getRuntime().addShutdownHook( - new Thread() { - @Override - public void run() { - MGR.shutdownInProgress.set(true); - for (Runnable hook: MGR.getShutdownHooksInOrder()) { - try { - hook.run(); - } catch (Throwable ex) { - LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() + - "' failed, " + ex.toString(), ex); + try { + Runtime.getRuntime().addShutdownHook( + new Thread() { + @Override + public void run() { + MGR.shutdownInProgress.set(true); + for (Runnable hook: MGR.getShutdownHooksInOrder()) { + try { + hook.run(); + } catch (Throwable ex) { + LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() + + "' failed, " + ex.toString(), ex); + } } } } - } - ); + ); + } catch (IllegalStateException ex) { + // JVM is being shut down. Ignore + LOG.warn("Failed to add the ShutdownHook", ex); + } } /**