From 754021cd6c6cb9de539fc3eeca8695872d8fd52a Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 7 Feb 2023 16:43:01 +0100 Subject: [PATCH] Ignore Shutdown in progress when closing ShutdownHookIntegration (#2521) Co-authored-by: Markus Hintersteiner --- CHANGELOG.md | 6 +++++ .../io/sentry/ShutdownHookIntegration.java | 12 ++++++++- .../io/sentry/ShutdownHookIntegrationTest.kt | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 137a738287..f7cafab10f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Ignore Shutdown in progress when closing ShutdownHookIntegration ([#2521](https://github.com/getsentry/sentry-java/pull/2521)) + ## 6.13.1 ### Fixes diff --git a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java index 17e93ab891..e3c7a29f9b 100644 --- a/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java +++ b/sentry/src/main/java/io/sentry/ShutdownHookIntegration.java @@ -41,7 +41,17 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio @Override public void close() throws IOException { if (thread != null) { - runtime.removeShutdownHook(thread); + try { + runtime.removeShutdownHook(thread); + } catch (IllegalStateException e) { + @Nullable final String message = e.getMessage(); + // https://github.com/openjdk/jdk/blob/09b8a1959771213cb982d062f0a913285e4a0c6e/src/java.base/share/classes/java/lang/ApplicationShutdownHooks.java#L83 + if (message != null && message.equals("Shutdown in progress")) { + // ignore + } else { + throw e; + } + } } } diff --git a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt index aef8d50723..f7e3848cad 100644 --- a/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt +++ b/sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt @@ -5,7 +5,9 @@ import org.mockito.kotlin.eq import org.mockito.kotlin.mock import org.mockito.kotlin.never import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import kotlin.test.Test +import kotlin.test.assertFails import kotlin.test.assertNotNull class ShutdownHookIntegrationTest { @@ -77,4 +79,29 @@ class ShutdownHookIntegrationTest { verify(fixture.hub).flush(eq(10000)) } + + @Test + fun `shutdown in progress is handled gracefully`() { + val integration = fixture.getSut() + whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException("Shutdown in progress")) + + integration.register(fixture.hub, fixture.options) + integration.close() + + verify(fixture.runtime).removeShutdownHook(any()) + } + + @Test + fun `non shutdown in progress during removeShutdownHook is rethrown`() { + val integration = fixture.getSut() + whenever(fixture.runtime.removeShutdownHook(any())).thenThrow(java.lang.IllegalStateException()) + + integration.register(fixture.hub, fixture.options) + + assertFails { + integration.close() + } + + verify(fixture.runtime).removeShutdownHook(any()) + } }