Skip to content

Commit 334e2cd

Browse files
committed
Context created from a vertx thread should not be recorded as a sticky context.
1 parent bab52b7 commit 334e2cd

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/main/java/io/vertx/core/Vertx.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static Future<Vertx> clusteredVertx(VertxOptions options) {
177177
* @return The current context or {@code null} if there is no current context
178178
*/
179179
static @Nullable Context currentContext() {
180-
return ContextInternal.current();
180+
return ContextInternal.current(Thread.currentThread());
181181
}
182182

183183
/**

src/main/java/io/vertx/core/impl/ContextInternal.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public interface ContextInternal extends Context {
4141
/**
4242
* @return the current context
4343
*/
44-
static ContextInternal current() {
45-
Thread thread = Thread.currentThread();
44+
static ContextInternal current(Thread thread) {
4645
if (thread instanceof VertxThread) {
4746
return ((VertxThread) thread).context();
4847
} else {
@@ -237,7 +236,7 @@ default void execute(Handler<Void> task) {
237236
* @return whether the current thread is running on this context
238237
*/
239238
default boolean isRunningOnContext() {
240-
return current() == this && inThread();
239+
return current(Thread.currentThread()) == this && inThread();
241240
}
242241

243242
/**

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,18 @@ public EventLoopGroup getAcceptorEventLoopGroup() {
504504
}
505505

506506
public ContextInternal getOrCreateContext() {
507-
ContextInternal ctx = getContext();
507+
Thread thread = Thread.currentThread();
508+
ContextInternal ctx = getContext(thread);
508509
if (ctx == null) {
509-
// We are running embedded - Create a context
510-
ctx = createEventLoopContext();
510+
return createContext(thread);
511+
}
512+
return ctx;
513+
}
514+
515+
private ContextInternal createContext(Thread thread) {
516+
// We are running embedded - Create a context
517+
ContextInternal ctx = createEventLoopContext();
518+
if (!(thread instanceof VertxThread)) {
511519
stickyContext.set(new WeakReference<>(ctx));
512520
}
513521
return ctx;
@@ -677,15 +685,23 @@ public long scheduleTimeout(ContextInternal context,
677685
}
678686

679687
public ContextInternal getContext() {
680-
ContextInternal context = ContextInternal.current();
688+
return getContext(Thread.currentThread());
689+
}
690+
691+
private ContextInternal getContext(Thread current) {
692+
ContextInternal context = ContextInternal.current(current);
681693
if (context != null && context.owner() == this) {
682694
return context;
683695
} else {
684-
WeakReference<ContextInternal> ref = stickyContext.get();
685-
return ref != null ? ref.get() : null;
696+
return getStickyContext();
686697
}
687698
}
688699

700+
private ContextInternal getStickyContext() {
701+
WeakReference<ContextInternal> ref = stickyContext.get();
702+
return ref != null ? ref.get() : null;
703+
}
704+
689705
public ClusterManager getClusterManager() {
690706
return clusterManager;
691707
}

src/test/java/io/vertx/core/ContextTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,4 +1121,31 @@ public void testConcurrentLocalAccess() throws Exception {
11211121
}
11221122
}
11231123

1124+
@Test
1125+
public void testContextShouldNotBeStickyFromUnassociatedEventLoopThread() {
1126+
ContextInternal ctx = ((VertxInternal)vertx).createEventLoopContext();
1127+
testContextShouldNotBeStickyFromUnassociatedVertxThread(ctx);
1128+
}
1129+
1130+
@Test
1131+
public void testContextShouldNotBeStickyFromUnassociatedWorkerThread() {
1132+
ContextInternal ctx = ((VertxInternal)vertx).createWorkerContext();
1133+
testContextShouldNotBeStickyFromUnassociatedVertxThread(ctx);
1134+
}
1135+
1136+
private void testContextShouldNotBeStickyFromUnassociatedVertxThread(ContextInternal ctx) {
1137+
ctx.execute(() -> {
1138+
assertEquals(null, Vertx.currentContext());
1139+
Context created1 = vertx.getOrCreateContext();
1140+
assertNotSame(ctx, created1);
1141+
ctx.execute(() -> {
1142+
assertEquals(null, Vertx.currentContext());
1143+
Context created2 = vertx.getOrCreateContext();
1144+
assertNotSame(ctx, created2);
1145+
assertNotSame(created1, created2);
1146+
testComplete();
1147+
});
1148+
});
1149+
await();
1150+
}
11241151
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy