Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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
* null checks when calling into it.
*/
class NoopWorker implements SuspendableWorker {

private final AtomicBoolean shutdown = new AtomicBoolean();
private final CountDownLatch shutdown = new CountDownLatch(1);

@Override
public boolean start() {
Expand All @@ -19,12 +19,19 @@ public boolean start() {

@Override
public CompletableFuture<Void> 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() {}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> awaitResult = new CompletableFuture<>();
try (GatedShutdownManager manager = new GatedShutdownManager()) {
CompletableFuture<Void> 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<Void> continueShutdown = new CompletableFuture<>();
private final CountDownLatch heartbeatTerminated = new CountDownLatch(1);

@Override
public CompletableFuture<Void> 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();
}
}
}
Loading