Skip to content

Commit

Permalink
Add sys property to control how to cleanup context's ThreadLocal (Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored Jun 20, 2023
1 parent 724779a commit e2effb3
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit e2effb3

Please sign in to comment.