Skip to content

Commit 84892a8

Browse files
authored
Do not crash when installing shutdown hook (#3456)
* Do not crash when installing shutdown hook * Changelog
1 parent 4fe50fc commit 84892a8

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Fixes
1111

1212
- Fix faulty `span.frame_delay` calculation for early app start spans ([#3427](https://github.com/getsentry/sentry-java/pull/3427))
13+
- Fix crash when installing `ShutdownHookIntegration` and the VM is shutting down ([#3456](https://github.com/getsentry/sentry-java/pull/3456))
1314

1415
## 7.9.0
1516

sentry/src/main/java/io/sentry/ShutdownHookIntegration.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
3333

3434
if (options.isEnableShutdownHook()) {
3535
thread = new Thread(() -> hub.flush(options.getFlushTimeoutMillis()));
36-
runtime.addShutdownHook(thread);
37-
options.getLogger().log(SentryLevel.DEBUG, "ShutdownHookIntegration installed.");
38-
addIntegrationToSdkVersion(getClass());
36+
handleShutdownInProgress(
37+
() -> {
38+
runtime.addShutdownHook(thread);
39+
options.getLogger().log(SentryLevel.DEBUG, "ShutdownHookIntegration installed.");
40+
addIntegrationToSdkVersion(getClass());
41+
});
3942
} else {
4043
options.getLogger().log(SentryLevel.INFO, "enableShutdownHook is disabled.");
4144
}
@@ -44,16 +47,22 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
4447
@Override
4548
public void close() throws IOException {
4649
if (thread != null) {
47-
try {
48-
runtime.removeShutdownHook(thread);
49-
} catch (IllegalStateException e) {
50-
@Nullable final String message = e.getMessage();
51-
// https://github.com/openjdk/jdk/blob/09b8a1959771213cb982d062f0a913285e4a0c6e/src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java#L83
52-
if (message != null && message.equals("Shutdown in progress")) {
53-
// ignore
54-
} else {
55-
throw e;
56-
}
50+
handleShutdownInProgress(() -> runtime.removeShutdownHook(thread));
51+
}
52+
}
53+
54+
private void handleShutdownInProgress(final @NotNull Runnable runnable) {
55+
try {
56+
runnable.run();
57+
} catch (IllegalStateException e) {
58+
@Nullable final String message = e.getMessage();
59+
// https://github.com/openjdk/jdk/blob/09b8a1959771213cb982d062f0a913285e4a0c6e/src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java#L83
60+
if (message != null
61+
&& (message.equals("Shutdown in progress")
62+
|| message.equals("VM already shutting down"))) {
63+
// ignore
64+
} else {
65+
throw e;
5766
}
5867
}
5968
}

sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ class ShutdownHookIntegrationTest {
9292
verify(fixture.runtime).removeShutdownHook(any())
9393
}
9494

95+
@Test
96+
fun `shutdown in progress is handled gracefully for registration`() {
97+
val integration = fixture.getSut()
98+
whenever(fixture.runtime.addShutdownHook(any())).thenThrow(java.lang.IllegalStateException("VM already shutting down"))
99+
100+
integration.register(fixture.hub, fixture.options)
101+
102+
verify(fixture.runtime).addShutdownHook(any())
103+
}
104+
95105
@Test
96106
fun `non shutdown in progress during removeShutdownHook is rethrown`() {
97107
val integration = fixture.getSut()

0 commit comments

Comments
 (0)