@@ -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 }
0 commit comments