From e8a5f01cfc335b9df515cc36f48966229d2c3b34 Mon Sep 17 00:00:00 2001 From: Clement Escoffier Date: Thu, 3 Nov 2022 14:06:49 +0100 Subject: [PATCH 1/2] When propagating the duplicated context, drop the request scope. Fix #29017 --- .../vertx/core/runtime/VertxCoreRecorder.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java index 05d88e50dbd35..f2347ec69aa02 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java @@ -17,6 +17,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadFactory; @@ -42,6 +43,7 @@ import io.quarkus.vertx.core.runtime.config.ClusterConfiguration; import io.quarkus.vertx.core.runtime.config.EventBusConfiguration; import io.quarkus.vertx.core.runtime.config.VertxConfiguration; +import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle; import io.quarkus.vertx.mdc.provider.LateBoundMDCProvider; import io.vertx.core.AsyncResult; import io.vertx.core.Context; @@ -558,7 +560,25 @@ public void runWith(Runnable task, Object context) { ContextInternal currentContext = (ContextInternal) Vertx.currentContext(); if (context != null && context != currentContext) { // Only do context handling if it's non-null - final ContextInternal vertxContext = (ContextInternal) context; + ContextInternal vertxContext = (ContextInternal) context; + // The request scope must not be propagated + Object requestScope = null; + ConcurrentMap local = vertxContext.localContextData(); + for (Object k : local.keySet()) { + if (k.getClass().getName() + .equals("io.quarkus.vertx.runtime.VertxCurrentContextFactory$VertxCurrentContext")) { + requestScope = k; + break; + } + } + if (requestScope != null) { + // Duplicate the context, copy the data, remove the request scope + vertxContext = vertxContext.duplicate(); + vertxContext.localContextData().putAll(local); + vertxContext.localContextData().remove(requestScope); + VertxContextSafetyToggle.setContextSafe(vertxContext, true); + } + vertxContext.beginDispatch(); try { task.run(); From 4293f3797365134f84280834167399d37e4c6e5a Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 3 Nov 2022 17:29:06 +0100 Subject: [PATCH 2/2] VertxCurrentContextFactory - use a string constant as a key --- .../vertx/core/runtime/VertxCoreRecorder.java | 18 +++++------------- .../runtime/VertxCurrentContextFactory.java | 8 +++++--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java index f2347ec69aa02..5ac0b5769c9d4 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java @@ -45,6 +45,7 @@ import io.quarkus.vertx.core.runtime.config.VertxConfiguration; import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle; import io.quarkus.vertx.mdc.provider.LateBoundMDCProvider; +import io.quarkus.vertx.runtime.VertxCurrentContextFactory; import io.vertx.core.AsyncResult; import io.vertx.core.Context; import io.vertx.core.Handler; @@ -561,24 +562,15 @@ public void runWith(Runnable task, Object context) { if (context != null && context != currentContext) { // Only do context handling if it's non-null ContextInternal vertxContext = (ContextInternal) context; - // The request scope must not be propagated - Object requestScope = null; + // The CDI request context must not be propagated ConcurrentMap local = vertxContext.localContextData(); - for (Object k : local.keySet()) { - if (k.getClass().getName() - .equals("io.quarkus.vertx.runtime.VertxCurrentContextFactory$VertxCurrentContext")) { - requestScope = k; - break; - } - } - if (requestScope != null) { - // Duplicate the context, copy the data, remove the request scope + if (local.containsKey(VertxCurrentContextFactory.LOCAL_KEY)) { + // Duplicate the context, copy the data, remove the request context vertxContext = vertxContext.duplicate(); vertxContext.localContextData().putAll(local); - vertxContext.localContextData().remove(requestScope); + vertxContext.localContextData().remove(VertxCurrentContextFactory.LOCAL_KEY); VertxContextSafetyToggle.setContextSafe(vertxContext, true); } - vertxContext.beginDispatch(); try { task.run(); diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java index a2806ebc62899..88db266deac81 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxCurrentContextFactory.java @@ -14,6 +14,8 @@ public class VertxCurrentContextFactory implements CurrentContextFactory { + public static final String LOCAL_KEY = "io.quarkus.vertx.cdi-current-context"; + @Override public CurrentContext create(Class scope) { return new VertxCurrentContext<>(); @@ -27,7 +29,7 @@ private static final class VertxCurrentContext implement public T get() { Context context = Vertx.currentContext(); if (context != null && VertxContext.isDuplicatedContext(context)) { - return context.getLocal(this); + return context.getLocal(LOCAL_KEY); } return fallback.get(); } @@ -37,7 +39,7 @@ public void set(T state) { Context context = Vertx.currentContext(); if (context != null && VertxContext.isDuplicatedContext(context)) { VertxContextSafetyToggle.setContextSafe(context, true); - context.putLocal(this, state); + context.putLocal(LOCAL_KEY, state); } else { fallback.set(state); } @@ -48,7 +50,7 @@ public void remove() { Context context = Vertx.currentContext(); if (context != null && VertxContext.isDuplicatedContext(context)) { // NOOP - the DC should not be shared. - // context.removeLocal(this); + // context.removeLocal(LOCAL_KEY); } else { fallback.remove(); }