Ignore IllegalStateException if JVM is already shutting down

Trying to register/unregister a shutdown hook when the JVM is already in
shutdown throws an IllegalStateException. Ignore this exception since we
can't do anything about it.

Bug: 575367
Change-Id: Ic967c16c7f566c84778795315ab369e76668b364
This commit is contained in:
Matthias Sohn 2021-08-12 09:28:04 +02:00
parent c697f02e12
commit 6817b7e3b4
2 changed files with 16 additions and 4 deletions

View File

@ -219,8 +219,12 @@ public final void execute(String[] args) throws Exception {
case APACHE: {
SshdSessionFactory factory = new SshdSessionFactory(
new JGitKeyCache(), new DefaultProxyDataFactory());
Runtime.getRuntime()
.addShutdownHook(new Thread(factory::close));
try {
Runtime.getRuntime()
.addShutdownHook(new Thread(factory::close));
} catch (IllegalStateException e) {
// ignore - the VM is already shutting down
}
SshSessionFactory.setInstance(factory);
break;
}

View File

@ -173,7 +173,11 @@ public Git call() throws GitAPIException, InvalidRemoteException,
Repository repository = init();
FetchResult fetchResult = null;
Thread cleanupHook = new Thread(() -> cleanup());
Runtime.getRuntime().addShutdownHook(cleanupHook);
try {
Runtime.getRuntime().addShutdownHook(cleanupHook);
} catch (IllegalStateException e) {
// ignore - the VM is already shutting down
}
try {
fetchResult = fetch(repository, u);
} catch (IOException ioe) {
@ -197,7 +201,11 @@ public Git call() throws GitAPIException, InvalidRemoteException,
cleanup();
throw e;
} finally {
Runtime.getRuntime().removeShutdownHook(cleanupHook);
try {
Runtime.getRuntime().removeShutdownHook(cleanupHook);
} catch (IllegalStateException e) {
// ignore - the VM is already shutting down
}
}
if (!noCheckout) {
try {