Skip to content

Commit 56a122a

Browse files
committed
Remove thread sticky context feature.
Motivation: Thread sticky context was introduced in Vert.x 4 in order to maintain callback ordering when submitting tasks from a non vertx thread. Vert.x 5 introduces the sticky event-loop feature that implements the same idea for shadow contexts. In addition shadow context references cannot be reasonnably maintained. Dropping sticky context support does not break the original intent of the feature and brings behavior consistency when deadling with foreign contexts. Changes: Drop stick context feature.
1 parent 7fbc36f commit 56a122a

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

vertx-core/src/main/java/io/vertx/core/impl/VertxImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ private static ThreadFactory virtualThreadFactory() {
170170
private final Throwable transportUnavailabilityCause;
171171
private final VertxTracer tracer;
172172
private final ThreadLocal<WeakReference<EventLoop>> stickyEventLoop = new ThreadLocal<>();
173-
private final ThreadLocal<WeakReference<ContextInternal>> stickyContext = new ThreadLocal<>();
174173
private final boolean disableTCCL;
175174
private final Boolean useDaemonThread;
176175

@@ -521,7 +520,6 @@ public void execute(Runnable command) {
521520
} else {
522521
ctx = createEventLoopContext(eventLoop, workerPool, Thread.currentThread().getContextClassLoader());
523522
}
524-
stickyContext.set(new WeakReference<>(ctx));
525523
return ctx;
526524
}
527525
}
@@ -706,10 +704,6 @@ private ContextInternal getContext(Thread thread) {
706704
return new ShadowContext(this, new EventLoopExecutor(eventLoop), context);
707705
}
708706
} else {
709-
WeakReference<ContextInternal> ref = stickyContext.get();
710-
if (ref != null) {
711-
return ref.get();
712-
}
713707
return null;
714708
}
715709
}

vertx-core/src/test/java/io/vertx/tests/context/ContextTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,8 @@ public void testFailedFutureContextPropagation2() {
918918
}
919919

920920
@Test
921-
public void testSticky() {
921+
public void testStickiness() {
922922
Context ctx = vertx.getOrCreateContext();
923-
assertSame(ctx, vertx.getOrCreateContext());
924923
assertSame(((ContextInternal)ctx).nettyEventLoop(), ((ContextInternal)vertx.getOrCreateContext()).nettyEventLoop());
925924
}
926925

vertx-core/src/test/java/io/vertx/tests/context/EventExecutorProviderTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public void testExecuteTasks() {
4646
toRun.pop().run();
4747
assertEquals(1, cnt[0]);
4848
assertNull(Vertx.currentContext());
49-
// Sticky context
50-
assertSame(ctx, vertx.getOrCreateContext());
5149
}
5250

5351
@Test

vertx-core/src/test/java/io/vertx/tests/worker/NamedWorkerPoolTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,28 @@ public void testUnordered() throws Exception {
128128
}
129129

130130
@Test
131-
public void testUseDifferentExecutorWithSameTaskQueue() throws Exception {
131+
public void testUseDifferentExecutorWithSameTaskQueue() {
132132
int count = 10;
133133
waitFor(count);
134134
WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-the-executor");
135-
Thread startThread = Thread.currentThread();
136-
AtomicReference<Thread> currentThread = new AtomicReference<>();
137-
CountDownLatch latch = new CountDownLatch(1);
138-
for (int i = 0;i < count;i++) {
139-
int val = i;
140-
exec.executeBlocking(() -> {
141-
Thread current = Thread.currentThread();
142-
assertNotSame(startThread, current);
143-
if (val == 0) {
144-
assertNull(currentThread.getAndSet(current));
145-
awaitLatch(latch);
146-
} else {
147-
assertSame(current, currentThread.get());
148-
}
149-
return null;
150-
}, true).onComplete(onSuccess(v -> complete()));
151-
latch.countDown();
152-
}
135+
vertx.runOnContext(v1 -> {
136+
AtomicReference<Thread> currentThread = new AtomicReference<>();
137+
CountDownLatch latch = new CountDownLatch(1);
138+
for (int i = 0;i < count;i++) {
139+
int val = i;
140+
exec.executeBlocking(() -> {
141+
Thread current = Thread.currentThread();
142+
if (val == 0) {
143+
assertNull(currentThread.getAndSet(current));
144+
awaitLatch(latch);
145+
} else {
146+
assertSame(current, currentThread.get());
147+
}
148+
return null;
149+
}, true).onComplete(onSuccess(v2 -> complete()));
150+
latch.countDown();
151+
}
152+
});
153153
await();
154154
}
155155

0 commit comments

Comments
 (0)