Skip to content

Commit 1b0a661

Browse files
committed
Wrap runnable
1 parent 6f23a76 commit 1b0a661

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

sentry/src/main/java/io/sentry/SentryWrapper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,23 @@ public static <U> Supplier<U> wrapSupplier(final @NotNull Supplier<U> supplier)
5757
}
5858
};
5959
}
60+
61+
/**
62+
* Helper method to wrap {@link Runnable}
63+
*
64+
* <p>Forks current and isolation scope before execution and restores previous state afterwards.
65+
* This prevents reused threads (e.g. from thread-pools) from getting an incorrect state.
66+
*
67+
* @param runnable - the {@link Runnable} to be wrapped
68+
* @return the wrapped {@link Runnable}
69+
*/
70+
public static Runnable wrapRunnable(final @NotNull Runnable runnable) {
71+
final IScopes newScopes = Sentry.forkedScopes("SentryWrapper.wrapRunnable");
72+
73+
return () -> {
74+
try (ISentryLifecycleToken ignore = newScopes.makeCurrent()) {
75+
runnable.run();
76+
}
77+
};
78+
}
6079
}

0 commit comments

Comments
 (0)