aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar Julio Merino <jmmv@google.com>2016-10-17 09:50:11 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2016-10-17 11:20:00 +0000
commitd1619b75fc07cd0661bd23062b0698b36109d91e (patch)
treef3ebc069ef6093c10920ec0fe6a57564d2c62793 /src/main/java/com/google/devtools
parent5338f48b54c996265271ba83561fe0a0b9ac43ab (diff)
Support per-module shutdown code on crash.
Add a new "shutdown on crash" hook to the Blaze modules and call it when we exit due to a crash. This allows modules to perform necessary cleanup of internal state when exiting abruptly. Note that the need for implementing "shutdown on crash" is rare. As an example of when this may be useful is a module that has spawned background threads to communicate with a remote server and we want to shut down those connections in a controlled manner. -- MOS_MIGRATED_REVID=136330678
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java12
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java8
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/BugReport.java5
3 files changed, 24 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
index a2e71cc9a4..e6cce932fb 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java
@@ -241,11 +241,23 @@ public abstract class BlazeModule {
/**
* Called when Blaze shuts down.
+ *
+ * <p>If you are also implementing {@link #shutdownOnCrash()}, consider putting the common
+ * shutdown code in the latter and calling that other hook from here.
*/
public void blazeShutdown() {
}
/**
+ * Called when Blaze shuts down due to a crash.
+ *
+ * <p>Modules may use this to flush pending state, but they must be careful to only do a minimal
+ * number of things. Keep in mind that we are crashing so who knows what state we are in. Modules
+ * rarely need to implement this.
+ */
+ public void blazeShutdownOnCrash() {}
+
+ /**
* Perform module specific check of current command environment.
*/
public void checkEnvironment(CommandEnvironment env) {
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
index dc67a17f9d..9d040bd31d 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
@@ -473,12 +473,20 @@ public final class BlazeRuntime {
return commandMap;
}
+ /** Invokes {@link BlazeModule#blazeShutdown()} on all registered modules. */
public void shutdown() {
for (BlazeModule module : blazeModules) {
module.blazeShutdown();
}
}
+ /** Invokes {@link BlazeModule#blazeShutdownOnCrash()} on all registered modules. */
+ public void shutdownOnCrash() {
+ for (BlazeModule module : blazeModules) {
+ module.blazeShutdownOnCrash();
+ }
+ }
+
/**
* Returns the defaults package for the default settings. Should only be called by commands that
* do <i>not</i> process {@link BuildOptions}, since build options can alter the contents of the
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BugReport.java b/src/main/java/com/google/devtools/build/lib/runtime/BugReport.java
index aadaf10c51..9a6e978878 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BugReport.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BugReport.java
@@ -89,7 +89,10 @@ public abstract class BugReport {
if (runtime != null) {
runtime.notifyCommandComplete(exitCode);
// We don't call runtime#shutDown() here because all it does is shut down the modules,
- // and who knows if they can be trusted.
+ // and who knows if they can be trusted. Instead, we call runtime#shutdownOnCrash()
+ // which attempts to cleanly shutdown those modules that might have something pending
+ // to do as a best-effort operation.
+ runtime.shutdownOnCrash();
}
} finally {
// Avoid shutdown deadlock issues: If an application shutdown hook crashes, it will