Drain polled tasks on shutdown#2261
Open
yuandrew wants to merge 16 commits into
Open
Conversation
Sushisource
approved these changes
Apr 1, 2026
| select { | ||
| case bw.taskQueueCh <- &polledTask{task: task, permit: slotPermit}: | ||
| didSendTask = true | ||
| case <-bw.stopCh: |
Member
There was a problem hiding this comment.
Just checking the stop channel is still used elsewhere since we removed it in two spots
Contributor
Author
There was a problem hiding this comment.
yep! still used in plenty of other places
Contributor
Author
|
recheck |
b930672 to
29b355a
Compare
This reverts commit be5c0e4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What was changed
Refactored worker shutdown to use a two-stage approach: pollers shut down first, then the task dispatcher drains any remaining tasks before exiting. This ensures tasks polled during shutdown are processed rather than silently dropped.
Key changes:
note: Graceful shutdown drain may bypass worker dispatch rate limiting for already-polled tasks, while still respecting slot concurrency. I think this is okay because the tasks are already polled by the worker, thus "owned", and this speeds up draining of tasks, decreasing likelihood of hitting
WorkerStopTimeout. I don't think users would have any issues with this.Why?
PR #2199 changed shutdown to let the server complete in-flight polls, instead of cancelling them. This exposed a pre-existing race when a poller receives a task during shutdown, Go would silently dropping the task. The dispatcher had the same issue — it could exit on stopCh before reading pending tasks from the channel.
This aligns the Go SDK's shutdown with how Core SDK handles it:
Checklist
Closes Drain polled tasks on shutdown #1197
How was this tested:
Any docs updates needed?
No
Note
Medium Risk
Changes worker shutdown semantics to drain already-polled tasks (and avoid canceling in-flight polls) which can affect stop latency and task processing order; includes new logic around channel closing and dispatcher behavior that could introduce shutdown edge cases.
Overview
Implements a two-stage worker shutdown when the server supports
worker_poll_complete_on_shutdown: pollers are allowed to finish their current long-poll, then the task dispatcher drains and processes any tasks already received instead of dropping them.This refactors
baseWorkerto track poller lifecycles separately (pollerWG), closetaskQueueChonly after all pollers exit, and update dispatch/poll paths to always enqueue polled tasks in drain mode. It also extends shutdown coordination toworkflow,activity, andnexustask processors (allow processing after stop when draining), sendsShutdownWorkerfor session worker task queues, enables the cancel-worker-polls dynamic config in build harness, and adds unit/integration tests covering drain vs legacy shutdown behavior and stop-timeout bounding.Reviewed by Cursor Bugbot for commit 3c03f1f. Bugbot is set up for automated code reviews on this repo. Configure here.