diff --git a/CHANGELOG.md b/CHANGELOG.md index 806390cfae..9d58ec7da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ to docs, or any other relevant information. credentials set through `setHeaders` no longer appear in logs. - The JavaDoc of `WorkflowClient.startUpdate` and `WorkflowClient.startUpdateWithStart` no longer describes these calls as asynchronous. +- Activity worker termination waits returning before heartbeat shutdown initiated the final poller shutdown, which could + leave workflow-only workers reporting that shutdown was incomplete. ## [1.41.1] - 2026-11-16 diff --git a/temporal-sdk/src/main/java/io/temporal/internal/worker/NoopWorker.java b/temporal-sdk/src/main/java/io/temporal/internal/worker/NoopWorker.java index 8d836293b5..271f60ea14 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/worker/NoopWorker.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/worker/NoopWorker.java @@ -1,8 +1,8 @@ package io.temporal.internal.worker; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; /** * Helper class that is used instead of null for non initialized worker. This eliminates needs for @@ -10,7 +10,7 @@ */ class NoopWorker implements SuspendableWorker { - private final AtomicBoolean shutdown = new AtomicBoolean(); + private final CountDownLatch shutdown = new CountDownLatch(1); @Override public boolean start() { @@ -19,12 +19,19 @@ public boolean start() { @Override public CompletableFuture shutdown(ShutdownManager shutdownManager, boolean interruptTasks) { - shutdown.set(true); + shutdown.countDown(); return CompletableFuture.completedFuture(null); } @Override - public void awaitTermination(long timeout, TimeUnit unit) {} + public void awaitTermination(long timeout, TimeUnit unit) { + try { + // Shutdown may be initiated by a continuation after another worker component terminates. + shutdown.await(timeout, unit); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } @Override public void suspendPolling() {} @@ -39,12 +46,12 @@ public boolean isSuspended() { @Override public boolean isShutdown() { - return shutdown.get(); + return shutdown.getCount() == 0; } @Override public boolean isTerminated() { - return shutdown.get(); + return shutdown.getCount() == 0; } @Override diff --git a/temporal-sdk/src/test/java/io/temporal/internal/worker/NoopWorkerTest.java b/temporal-sdk/src/test/java/io/temporal/internal/worker/NoopWorkerTest.java new file mode 100644 index 0000000000..ede5677d94 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/internal/worker/NoopWorkerTest.java @@ -0,0 +1,65 @@ +package io.temporal.internal.worker; + +import static org.junit.Assert.*; + +import io.temporal.testUtils.Eventually; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import org.junit.Test; + +public class NoopWorkerTest { + @Test + public void awaitTerminationWaitsForShutdown() throws Exception { + NoopWorker worker = new NoopWorker(); + CompletableFuture awaitResult = new CompletableFuture<>(); + Thread waiter = + new Thread( + () -> { + worker.awaitTermination(5, TimeUnit.SECONDS); + awaitResult.complete(worker.isTerminated()); + }); + waiter.start(); + try { + Eventually.assertEventually( + Duration.ofSeconds(5), + () -> + assertTrue(awaitResult.isDone() || waiter.getState() == Thread.State.TIMED_WAITING)); + assertFalse("awaitTermination returned before shutdown", awaitResult.isDone()); + assertFalse(worker.isShutdown()); + assertFalse(worker.isTerminated()); + } finally { + worker.shutdown(null, true).join(); + waiter.join(5_000); + } + assertTrue(awaitResult.get(5, TimeUnit.SECONDS)); + assertTrue(worker.isShutdown()); + // Repeated shutdown and waits after termination remain harmless. + worker.shutdown(null, false).join(); + worker.awaitTermination(0, TimeUnit.SECONDS); + assertTrue(worker.isTerminated()); + } + + @Test(timeout = 5_000) + public void awaitTerminationRespectsTimeoutWithoutShuttingDown() { + NoopWorker worker = new NoopWorker(); + worker.awaitTermination(0, TimeUnit.SECONDS); + worker.awaitTermination(20, TimeUnit.MILLISECONDS); + assertFalse(worker.isShutdown()); + assertFalse(worker.isTerminated()); + } + + @Test + public void awaitTerminationPreservesInterruption() { + NoopWorker worker = new NoopWorker(); + Thread.currentThread().interrupt(); + try { + worker.awaitTermination(5, TimeUnit.SECONDS); + assertTrue(Thread.currentThread().isInterrupted()); + assertFalse(worker.isShutdown()); + assertFalse(worker.isTerminated()); + } finally { + Thread.interrupted(); + } + } +} diff --git a/temporal-sdk/src/test/java/io/temporal/internal/worker/SyncActivityWorkerTest.java b/temporal-sdk/src/test/java/io/temporal/internal/worker/SyncActivityWorkerTest.java new file mode 100644 index 0000000000..0f326a3803 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/internal/worker/SyncActivityWorkerTest.java @@ -0,0 +1,135 @@ +package io.temporal.internal.worker; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import io.temporal.client.WorkflowClient; +import io.temporal.common.interceptors.WorkerInterceptor; +import io.temporal.serviceclient.WorkflowServiceStubs; +import io.temporal.testUtils.Eventually; +import io.temporal.worker.tuning.FixedSizeSlotSupplier; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import org.junit.Test; + +public class SyncActivityWorkerTest { + @Test + public void awaitTerminationWaitsForHeartbeatShutdownContinuation() throws Exception { + assertWaitsForHeartbeatShutdownContinuation(false); + } + + @Test + public void awaitTerminationBeforeShutdownWaitsForHeartbeatShutdownContinuation() + throws Exception { + assertWaitsForHeartbeatShutdownContinuation(true); + } + + private static SyncActivityWorker newIdleWorker() { + WorkflowClient client = mock(WorkflowClient.class); + when(client.getWorkflowServiceStubs()).thenReturn(mock(WorkflowServiceStubs.class)); + SyncActivityWorker worker = + new SyncActivityWorker( + client, + "namespace", + "task-queue", + 0, + SingleWorkerOptions.newBuilder() + .setIdentity("identity") + .setWorkerInterceptors(new WorkerInterceptor[0]) + .setMaxHeartbeatThrottleInterval(Duration.ofSeconds(1)) + .setDefaultHeartbeatThrottleInterval(Duration.ofSeconds(1)) + .build(), + new FixedSizeSlotSupplier<>(1), + new NamespaceCapabilities()); + // A workflow-only worker still owns an activity worker, with no registered activity types. + assertFalse(worker.start()); + return worker; + } + + private static void assertWaitsForHeartbeatShutdownContinuation(boolean awaitBeforeShutdown) + throws Exception { + SyncActivityWorker worker = newIdleWorker(); + CompletableFuture awaitResult = new CompletableFuture<>(); + try (GatedShutdownManager manager = new GatedShutdownManager()) { + CompletableFuture shutdown = null; + if (!awaitBeforeShutdown) { + shutdown = worker.shutdown(manager, true); + assertTrue(manager.heartbeatTerminated.await(5, TimeUnit.SECONDS)); + assertFalse(worker.isTerminated()); + } + Thread waiter = + new Thread( + () -> { + try { + worker.awaitTermination(5, TimeUnit.SECONDS); + awaitResult.complete(worker.isTerminated()); + } catch (Throwable e) { + awaitResult.completeExceptionally(e); + } + }); + waiter.start(); + try { + // Observe the await itself blocking, rather than signalling before it has been entered. + Eventually.assertEventually( + Duration.ofSeconds(5), + () -> + assertTrue( + awaitResult.isDone() || waiter.getState() == Thread.State.TIMED_WAITING)); + assertFalse("awaitTermination returned before activity shutdown", awaitResult.isDone()); + if (awaitBeforeShutdown) { + shutdown = worker.shutdown(manager, true); + assertTrue(manager.heartbeatTerminated.await(5, TimeUnit.SECONDS)); + // The heartbeat executor has stopped, but its shutdown continuation is still held. + Eventually.assertEventually( + Duration.ofSeconds(5), + () -> assertTrue(awaitResult.isDone() || isWaitingForIdlePoller(waiter))); + assertFalse("awaitTermination returned before activity shutdown", awaitResult.isDone()); + } + } finally { + manager.continueShutdown.complete(null); + waiter.join(5_000); + } + assertTrue(awaitResult.get(5, TimeUnit.SECONDS)); + shutdown.get(5, TimeUnit.SECONDS); + } + } + + private static boolean isWaitingForIdlePoller(Thread waiter) { + if (waiter.getState() != Thread.State.TIMED_WAITING) { + return false; + } + for (StackTraceElement frame : waiter.getStackTrace()) { + if (frame.getClassName().equals(NoopWorker.class.getName()) + && frame.getMethodName().equals("awaitTermination")) { + return true; + } + } + return false; + } + + private static class GatedShutdownManager extends ShutdownManager { + private final CompletableFuture continueShutdown = new CompletableFuture<>(); + private final CountDownLatch heartbeatTerminated = new CountDownLatch(1); + + @Override + public CompletableFuture shutdownExecutor( + ExecutorService executor, String name, Duration timeout) { + return super.shutdownExecutor(executor, name, timeout) + .thenCompose( + ignored -> { + assertTrue(executor.isTerminated()); + heartbeatTerminated.countDown(); + return continueShutdown; + }); + } + + @Override + public void close() { + continueShutdown.complete(null); + super.close(); + } + } +}