fix: serialize concurrent RunSession calls to prevent tool_use/tool_result mismatch#2375
Merged
dgageot merged 1 commit intodocker:mainfrom Apr 10, 2026
Conversation
…esult mismatch When two HTTP requests target the same session concurrently, the second can inject user messages while the first is mid-tool-call, producing a tool_use without a matching tool_result that causes Anthropic API errors. Add a per-session streaming mutex to activeRuntimes. RunSession uses TryLock to fail fast with ErrSessionBusy (HTTP 409) when the session is already streaming. Message addition is deferred until after the lock is acquired, so a rejected request never mutates the session. TryLock is called on the calling goroutine inside RunSession; Unlock is deferred in the background goroutine after RunStream completes. The lock is held continuously from before message addition through the entire stream including all tool-call processing. Assisted-By: docker-agent
gtardif
approved these changes
Apr 10, 2026
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.
When two HTTP requests target the same session concurrently, the second
can inject user messages while the first is mid-tool-call, producing a
tool_use without a matching tool_result that causes Anthropic API errors.
Add a per-session streaming mutex to
activeRuntimes.RunSessionusesTryLockto fail fast withErrSessionBusy(HTTP 409) when the session isalready streaming. Message addition is deferred until after the lock is
acquired, so a rejected request never mutates the session.
TryLockis called on the calling goroutine insideRunSession;Unlockisdeferred in the background goroutine after
RunStreamcompletes. The lockis held continuously from before message addition through the entire
stream including all tool-call processing.