Skip to content

Commit 1187349

Browse files
tsegismontvietj
authored andcommitted
HTTP server started on a duplicated context shares contextual data
See #5589 If the server is started from a duplicated context, the listen context should be the underlying context (unwrapped). And then we should duplicate the unwrapped context. This is to make sure the creating context and the request context are not sharing local data. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent c01eac9 commit 1187349

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/HttpServerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public synchronized Future<HttpServer> listen(SocketAddress address) {
185185
ContextInternal listenContext;
186186
// Not sure of this
187187
if (context.isEventLoopContext()) {
188-
listenContext = context;
188+
listenContext = context.unwrap();
189189
} else {
190190
listenContext = context.toBuilder()
191191
.withThreadingModel(ThreadingModel.EVENT_LOOP)
@@ -197,7 +197,7 @@ public synchronized Future<HttpServer> listen(SocketAddress address) {
197197
server.exceptionHandler(exceptionHandler);
198198
server.connectHandler(so -> {
199199
NetSocketImpl soi = (NetSocketImpl) so;
200-
Supplier<ContextInternal> streamContextSupplier = context::duplicate;
200+
Supplier<ContextInternal> streamContextSupplier = context.unwrap()::duplicate;
201201
String host = address.isInetSocket() ? address.host() : "localhost";
202202
int port = address.port();
203203
String serverOrigin = (tcpOptions.isSsl() ? "https" : "http") + "://" + host + ":" + port;

vertx-core/src/test/java/io/vertx/tests/http/HttpTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7049,4 +7049,27 @@ public void testHttpServerResponseWriteHead() throws Exception {
70497049
await();
70507050
}
70517051

7052+
@Test
7053+
public void testServerStartedFromDuplicatedContext() throws Exception {
7054+
ContextInternal context = (ContextInternal) vertx.getOrCreateContext();
7055+
ContextInternal duplicated = context.duplicate();
7056+
ContextInternal.LOCAL_MAP.get(duplicated, ConcurrentHashMap::new).put("foo", "bar");
7057+
7058+
server.requestHandler(req -> {
7059+
ContextInternal current = ContextInternal.current();
7060+
assertTrue("Not a duplicated context", current.isDuplicate());
7061+
assertNotSame("Request should be handled on a different duplicated context", duplicated, current);
7062+
ConcurrentMap<Object, Object> localMap = ContextInternal.LOCAL_MAP.get(current, ConcurrentHashMap::new);
7063+
assertFalse("Local map shouldn't have an entry for the key 'foo'", localMap.containsKey("foo"));
7064+
req.response().end();
7065+
});
7066+
startServer(duplicated);
7067+
7068+
client.request(requestOptions)
7069+
.compose(HttpClientRequest::send)
7070+
.expecting(HttpResponseExpectation.SC_OK)
7071+
.onComplete(onSuccess(v -> testComplete()));
7072+
7073+
await();
7074+
}
70527075
}

0 commit comments

Comments
 (0)