From 3a7515679ddffb6df1ee16128af0a86514385cd2 Mon Sep 17 00:00:00 2001 From: "a.sulyz" Date: Mon, 16 Nov 2026 15:51:26 +0100 Subject: [PATCH 1/4] Fix race in Nexus operation middleware for concurrent operations TemporalInterceptorMiddleware is shared by all Nexus tasks of a worker, but intercept() kept the per-request root interceptor in an instance field. A concurrent call could overwrite it between the write and the read, so a request was executed by the handler of another operation. Use a local variable instead. Co-Authored-By: Claude Sonnet 5.5 --- .../nexus/TemporalInterceptorMiddleware.java | 4 +- .../TemporalInterceptorMiddlewareTest.java | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index be76b8a91a..6987138743 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -7,7 +7,6 @@ public class TemporalInterceptorMiddleware implements OperationMiddleware { private final WorkerInterceptor[] interceptors; - RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor; public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) { this.interceptors = interceptors; @@ -16,7 +15,8 @@ public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) { @Override public OperationHandler intercept( OperationContext context, OperationHandler operationHandler) { - rootInboundCallsInterceptor = new RootNexusOperationInboundCallsInterceptor(operationHandler); + RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor = + new RootNexusOperationInboundCallsInterceptor(operationHandler); NexusOperationInboundCallsInterceptor inboundCallsInterceptor = rootInboundCallsInterceptor; for (WorkerInterceptor interceptor : interceptors) { inboundCallsInterceptor = diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java new file mode 100644 index 0000000000..0e065e6db6 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -0,0 +1,62 @@ +package io.temporal.internal.nexus; + +import static org.mockito.Mockito.mock; + +import com.uber.m3.tally.NoopScope; +import io.nexusrpc.handler.OperationContext; +import io.nexusrpc.handler.OperationHandler; +import io.nexusrpc.handler.OperationStartDetails; +import io.temporal.client.WorkflowClient; +import io.temporal.common.interceptors.WorkerInterceptor; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.junit.Assert; +import org.junit.Test; + +public class TemporalInterceptorMiddlewareTest { + + // NexusTaskHandlerImpl shares one middleware between all concurrent Nexus tasks of a worker, so + // each intercept call must wrap its own handler, not the handler of a concurrent call. + @Test + public void concurrentInterceptKeepsOwnHandler() throws Exception { + TemporalInterceptorMiddleware middleware = + new TemporalInterceptorMiddleware(new WorkerInterceptor[] {}); + OperationContext context = mock(OperationContext.class); + OperationStartDetails details = mock(OperationStartDetails.class); + WorkflowClient client = mock(WorkflowClient.class); + + ExecutorService executor = Executors.newFixedThreadPool(4); + List> misrouted = new ArrayList<>(); + for (int t = 0; t < 4; t++) { + String operation = "operation" + t; + OperationHandler handler = OperationHandler.sync((c, d, i) -> operation); + misrouted.add( + executor.submit( + () -> { + CurrentNexusOperationContext.set( + new InternalNexusOperationContext( + "namespace", "taskQueue", "endpoint", new NoopScope(), client)); + int count = 0; + for (int i = 0; i < 10_000; i++) { + Object result = + middleware + .intercept(context, handler) + .start(context, details, null) + .getSyncResult(); + if (!operation.equals(result)) { + count++; + } + } + CurrentNexusOperationContext.unset(); + return count; + })); + } + for (Future f : misrouted) { + Assert.assertEquals(0, (int) f.get()); + } + executor.shutdown(); + } +} From dea5dbc27a84499cc8c1e2502cb80eed0c12747d Mon Sep 17 00:00:00 2001 From: Sulyz Andrei Date: Tue, 17 Nov 2026 19:08:31 +0100 Subject: [PATCH 2/4] Potential fix for pull request finding 'Shut down the executor in a finally block' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../nexus/TemporalInterceptorMiddlewareTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 0e065e6db6..bc71af534c 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -54,9 +54,12 @@ public void concurrentInterceptKeepsOwnHandler() throws Exception { return count; })); } - for (Future f : misrouted) { - Assert.assertEquals(0, (int) f.get()); + try { + for (Future f : misrouted) { + Assert.assertEquals(0, (int) f.get()); + } + } finally { + executor.shutdownNow(); } - executor.shutdown(); } } From c744569db4b1d4c6a34f8a0673e40c846afd5eaa Mon Sep 17 00:00:00 2001 From: "a.sulyz" Date: Thu, 19 Nov 2026 10:02:06 +0100 Subject: [PATCH 3/4] Add changelog entry for Nexus middleware race fix Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b330567376..0ae81e61da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,6 +139,9 @@ to docs, or any other relevant information. specific to the Temporal CLI dev server implementation and may no longer be valid if that implementation changes. ### Fixed +- Concurrent Nexus operations on the same worker no longer risk being executed by another + operation's handler. The shared Nexus interceptor middleware kept each request's root + interceptor in an instance field, which a concurrent request could overwrite. - Skipped `Workflow.mutableSideEffect` calls made with the new recovery flag enabled no longer leave invalid marker commands after cache eviction or a worker restart. The flag remains disabled by default in this release to preserve replay compatibility during SDK rollout. From 1f22662c6d4655a0ca89e094a1e2f91cdaf7e4f4 Mon Sep 17 00:00:00 2001 From: "a.sulyz" Date: Thu, 19 Nov 2026 10:02:45 +0100 Subject: [PATCH 4/4] Move Nexus middleware changelog entry to Unreleased Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ae81e61da..71344a1439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,11 @@ to docs, or any other relevant information. - Updated Cloud API bindings to v0.27.0, including opt-in user group memberships, projects, project-scoped custom roles, and caller rate limits. +### Fixed +- Concurrent Nexus operations on the same worker no longer risk being executed by another + operation's handler. The shared Nexus interceptor middleware kept each request's root + interceptor in an instance field, which a concurrent request could overwrite. + ## [1.41.1] - 2026-11-16 ### Fixed @@ -139,9 +144,6 @@ to docs, or any other relevant information. specific to the Temporal CLI dev server implementation and may no longer be valid if that implementation changes. ### Fixed -- Concurrent Nexus operations on the same worker no longer risk being executed by another - operation's handler. The shared Nexus interceptor middleware kept each request's root - interceptor in an instance field, which a concurrent request could overwrite. - Skipped `Workflow.mutableSideEffect` calls made with the new recovery flag enabled no longer leave invalid marker commands after cache eviction or a worker restart. The flag remains disabled by default in this release to preserve replay compatibility during SDK rollout.