Skip to content

Commit

Permalink
Ignore Shutdown in progress when closing ShutdownHookIntegration (#2521)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
  • Loading branch information
adinauer and markushi authored Feb 7, 2023
1 parent 3e773a7 commit 754021c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion sentry/src/main/java/io/sentry/ShutdownHookIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions sentry/src/test/java/io/sentry/ShutdownHookIntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}

0 comments on commit 754021c

Please sign in to comment.