fix: serialize sends on shared listener gRPC streams - #4491
Merged
Conversation
Concurrent Workflow.Run waiters share one SubscribeToWorkflowRuns stream and called Send from multiple goroutines, violating grpc-go's single-sender contract. A sendMu in reconnectingStream now serializes SendMsg and CloseSend on published clients. This covers both WorkflowRunsListener and DurableEventsListener since both route through reconnectingStream.retrySend.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
|
Contributor
|
|
igor-kupczynski
marked this pull request as ready for review
July 23, 2026 16:07
Contributor
|
|
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.
Description
Problem
Concurrent
Workflow.Runwaits in the Go SDK share a singleSubscribeToWorkflowRunsbidirectional gRPC stream, and each waiter sent its subscription message from its own goroutine with no serialization. grpc-go permits only one concurrent sender per stream (and forbidsCloseSendconcurrent withSendMsg), so concurrent waits raced on the shared stream. The same pattern affected the legacyDurableEventsListener. Reported by a customer investigating a lost child-workflow result.Fix
reconnectingStream(the shared layer both listeners route sends through) gains asendMuthat serializes everySendMsgandCloseSendon published clients. It is held only around an individual snapshot+send attempt (or a CloseSend), never across reconnect, backoff, or error classification, so a slow send cannot wedge reconnection or shutdown. Reconnect replay needs no lock: it runs on a not-yet-published client inside the connect singleflight. Never-published clients (connect error paths) are likewise exempt.This is similar to
DurableTaskListenerand the Python SDK's pooled listener; although there we have queues, here a mutex suffices because callers expect synchronous send results.Discussion: queue vs mutex
Two ways to serialize senders on a shared stream: a mutex around
Send, or a single writer goroutine fed by a queue (whatDurableTaskListenerand the Python SDK do). This PR uses the mutex.-
retrySendis synchronous and a queue would make sends async.Workflow.Runcalls take turns, but the subscribe messages are tiny and should clear in <1ms.Sendstuck on flow control holds the lock and everyone stalls until it errors or the stream dies. But then I'm not sure if this is a problem as the queue has the same worst case (a stuck single writer stalls the whole queue), but it just hides the wait from the caller.CloseSendgoes under the same lock (grpc forbidsCloseSendconcurrent withSendMsg).Type of change
What's Changed
sendMutoreconnectingStreamthat serializesSendMsgandCloseSendon published clients (pkg/client/reconnecting_stream.go)SubscribeToWorkflowRunssends through the serialized path (pkg/client/stream_listen.go)TestRetrySend_ConcurrentSafetyto detect overlapping sends (pkg/client/reconnecting_stream_test.go)Checklist
Changes have been:
Testing
TestRetrySend_ConcurrentSafetywas strengthened to detect overlapping sends via an atomic in-flight flag with a start barrier. Run under-race; verified to fail against the pre-fix code.🤖 AI Disclosure
I acknowledge that an LLM was used in the creation of this Pull Request, in accordance with Hatchet's AI_POLICY.md.
Details: Implementation and tests drafted with an LLM coding agent (Cursor); reviewed and hand-edited by the author.