Skip to content

Commit 98d460c

Browse files
thachlpjrhee17
andauthored
Avoid addShutdownHook to JVM during its shutdown (#5524)
Motivation: Currently a JVM shutdown hook is added when DefaultClientFactory is used for the first time ([code](https://github.com/line/armeria/blob/main/core/src/main/java/com/linecorp/armeria/client/DefaultClientFactory.java#L80-L83)). It is possible that a JVM shutdown hook is being added after the JVM has started its shutdown and results in an exception below: ``` Caused by: java.lang.IllegalStateException: Shutdown in progress at java.base/java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66) at java.base/java.lang.Runtime.addShutdownHook(Runtime.java:216) at grpc_shaded.com.linecorp.armeria.client.DefaultClientFactory.<clinit>(DefaultClientFactory.java:79) ... 24 more ``` Modifications: - Skip adding the hook if we explicitly disabled it. - Add try-catch for ` java.lang.IllegalStateException` Result: - Closes #5494 --------- Co-authored-by: jrhee17 <[email protected]>
1 parent 8263e80 commit 98d460c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Diff for: core/src/main/java/com/linecorp/armeria/client/DefaultClientFactory.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ final class DefaultClientFactory implements ClientFactory {
7777

7878
static {
7979
if (DefaultClientFactory.class.getClassLoader() == ClassLoader.getSystemClassLoader()) {
80-
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
81-
if (!shutdownHookDisabled) {
82-
ClientFactory.closeDefault();
83-
}
84-
}));
80+
try {
81+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
82+
if (!shutdownHookDisabled) {
83+
ClientFactory.closeDefault();
84+
}
85+
}));
86+
} catch (IllegalStateException e) {
87+
logger.debug("Skipped adding a shutdown hook to the DefaultClientFactory.", e);
88+
}
8589
}
8690
}
8791

0 commit comments

Comments
 (0)