Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sys property to control how to cleanup context's ThreadLocal (Fixes #424) #425

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@

public class SmallRyeThreadContext implements ThreadContext {

private static final String CLOSE_SETTING_NULL_PROP_NAME = "io.smallrye.context.storage.CLOSE_SETTING_NULL";
private static final boolean CLOSE_SETTING_NULL = Boolean.getBoolean(CLOSE_SETTING_NULL_PROP_NAME);
final static ThreadLocal<SmallRyeThreadContext> currentThreadContext = StorageManager
.threadLocal(SmallRyeThreadContextStorageDeclaration.class);

private final static CleanAutoCloseable NULL_THREAD_STATE = new CleanAutoCloseable() {
@Override
public void close() {
currentThreadContext.remove();
if (!CLOSE_SETTING_NULL) {
currentThreadContext.remove();
} else {
currentThreadContext.set(null);
}
}
};

Expand Down Expand Up @@ -118,7 +124,7 @@ public static void withThreadContext(SmallRyeThreadContext threadContext, Runnab
try {
f.run();
} finally {
if (oldValue == null) {
if (!CLOSE_SETTING_NULL && oldValue == null)
currentThreadContext.remove();
} else {
currentThreadContext.set(oldValue);
Expand All @@ -141,7 +147,7 @@ public static <T> T withThreadContext(SmallRyeThreadContext threadContext, Suppl
try {
return f.get();
} finally {
if (oldValue == null) {
if (!CLOSE_SETTING_NULL && oldValue == null) {
currentThreadContext.remove();
} else {
currentThreadContext.set(oldValue);
Expand Down