Skip to content

Wait until the Activity worker finishes shutting down - #3224

Open
litlep-nibbyt wants to merge 4 commits into
mainfrom
fix/sticky-worker-shutdown-race
Open

litlep-nibbyt wants to merge 4 commits into
mainfrom
fix/sticky-worker-shutdown-race

Conversation

@litlep-nibbyt

@litlep-nibbyt litlep-nibbyt commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What changed?

NoopWorker.awaitTermination() now waits for NoopWorker.shutdown() or the caller’s timeout. A CountDownLatch(1) records shutdown and releases any waiting threads.

Why?

A worker with no registered Activities uses NoopWorker as its Activity poller. The placeholder has no tasks or threads of its own, but the SDK still tracks whether it has been shut down.

When the Activity worker stops heartbeats first, the SDK calls NoopWorker.shutdown() in a later step. The old empty wait let WorkerFactory.awaitTermination() return between those steps, with time still left and isTerminated() still false.

Before and after

BEFORE

SDK shutdown sequence:
  [1] Heartbeat executor stops
       |
       v
  [2] ShutdownManager notices it stopped
       |
       v
  [3] SDK calls NoopWorker.shutdown()

Caller in awaitTermination():
  NoopWorker.awaitTermination() returns immediately
    -> Wait for [1]
    -> Return

FAILURE: If the caller returns between [1] and [3],
         isTerminated() can still be false with time left.


AFTER

SDK shutdown sequence:
  [1] Heartbeat executor stops
       |
       v
  [2] ShutdownManager notices it stopped
       |
       v
  [3] SDK calls NoopWorker.shutdown(), which releases the latch

Caller in awaitTermination():
  NoopWorker.awaitTermination() waits on its latch for [3]
    -> Check heartbeat executor (already stopped)
    -> Return

Earlier attempts

Previous proposals changed the workflow worker or the worker factory. Review feedback questioned whether their tests demonstrated the reported problem.

This proposal addresses a different component and tests the failure through real workers and executors.

Validation

@Test
public void testShutdownNow() {
  TestWorkflow1 workflow = testWorkflowRule.newWorkflowStub(TestWorkflow1.class); // Get a Workflow handle.
  WorkflowClient.start(workflow::execute, null);                                  // Start it asynchronously.
  testWorkflowRule.getTestEnvironment().shutdownNow();                            // Start shutdown. The idle poller is shut down later.
  testWorkflowRule.getTestEnvironment().awaitTermination(10, TimeUnit.SECONDS);   // FIX: Wait for the idle poller's shutdown signal too.
  assertTrue(testWorkflowRule.getTestEnvironment().getWorkerFactory().isTerminated()); // Used to fail if that signal was still pending.
  // Cleanup workflow that will not finish
  WorkflowStub untyped = WorkflowStub.fromTyped(workflow);                        // Get the handle with the terminate method.
  untyped.terminate("terminate");                                                 // End the Workflow execution on the server.
}

Worker.awaitTermination() waits for the Activity, Nexus, and Workflow sides in that order, using the time left from the same timeout.

Worker.awaitTermination()
  1. Activity -> SyncActivityWorker -> ActivityWorker -> NoopWorker
  2. Nexus    -> SyncNexusWorker
  3. Workflow -> SyncWorkflowWorker -> WorkflowWorker

Branch 3 still waits for the real Workflow poller and task executor.

One existing test was verified fixed: StickyWorkflowDrainShutdownTest.testShutdownNow[1], the autoscaling variant. It failed 5/5 times before the fix and passed 5/5 with the latch under the same controlled delay. The other three sticky cases also passed in CI. Cloud CI log

Three new regression tests also failed against our first implementation and passed with the latch:

  • NoopWorkerTest.awaitTerminationWaitsForShutdown
  • SyncActivityWorkerTest.awaitTerminationBeforeShutdownWaitsForHeartbeatShutdownContinuation
  • SyncActivityWorkerTest.gracefulAwaitTerminationBeforeShutdownWaitsForHeartbeatShutdownContinuation

Regression tests also cover waiting before and after shutdown, timeout, and interruption.

Across the local test runs, 78 tests passed and one skipped because the server lacked a required capability. The unchanged shutdown test passed 100 runs without the deliberate delay. The cause of the historical CI failures remains unconfirmed.

Breaking changes: None.
Documentation: Changelog entry included.
Server changes: None.

@litlep-nibbyt
litlep-nibbyt marked this pull request as ready for review November 18, 2026 01:07
@litlep-nibbyt
litlep-nibbyt requested a review from a team as a code owner November 18, 2026 01:07

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant