Skip to content
Merged
Show file tree
Hide file tree
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 @@ -367,7 +367,7 @@ public Context swap(Context context) {
ContinuableScope newScope;
if (context instanceof ScopeContext) {
// restore previously swapped context stack
newStack = ((ScopeContext) context).scopeStack;
newStack = ((ScopeContext) context).restore();
newScope = newStack.top;
} else if (context != Context.root()) {
// start a new stack and record the new context as active
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

/** Wraps a {@link ScopeStack} as a {@link Context} so it can be swapped back later. */
final class ScopeContext implements Context {
final ScopeStack scopeStack;
private final Thread originalThread = Thread.currentThread();
private final ScopeStack scopeStack;
private final Context context;

ScopeContext(ScopeStack scopeStack) {
Expand All @@ -18,6 +19,11 @@ private ScopeContext(ScopeStack scopeStack, Context context) {
this.context = context;
}

ScopeStack restore() {
// take defensive copy of original scope stack when restoring on different thread
return originalThread == Thread.currentThread() ? scopeStack : scopeStack.copy();
}

@Nullable
@Override
public <T> T get(ContextKey<T> key) {
Expand Down