fix(think): protect concurrent tool calls with a mutex by dlanov · Pull Request #4570 · docker/docker-agent · GitHub
Skip to content

fix(think): protect concurrent tool calls with a mutex - #4570

Merged
aheritier merged 1 commit into
docker:mainfrom
dlanov:fix-4568-think-tool-race
Oct 13, 2026
Merged

aheritier merged 1 commit into
docker:mainfrom
dlanov:fix-4568-think-tool-race

Conversation

@dlanov

@dlanov dlanov commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #4568.

The Think tool stores thoughts in a shared slice without synchronization. When multiple think calls execute concurrently, appending to the slice and building the response can race, resulting in lost thoughts or inconsistent output.

This PR adds a mutex to protect both operations, preserving the existing output format and sequential behavior.

Changes

  • Protect Think ToolSet's shared thoughts with sync.Mutex.
  • Add a sequential test verifying the exact output format.
  • Add a concurrency regression test with 64 simultaneous tool calls, verifying that no thoughts are lost or duplicated.

Validation

  • Reproduced the original bug with Go's race detector, including lost thoughts.
  • Targeted race tests passed across 20 shuffled runs.
  • go vet passed.
  • task lint passed with 0 issues.
  • task build passed.
  • task test passed (243 packages).

No dependencies or tool lifecycle behavior were changed.

@dlanov
dlanov requested a review from a team as a code owner October 11, 2026 22:31
@aheritier aheritier added area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Oct 11, 2026
@aheritier
aheritier enabled auto-merge October 13, 2026 17:16

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Automated implementer agent — this comment was posted by the implementer bot from Docker Agentic Platform, not by a human developer

Assessment: 🟢 APPROVE

Reviewed commit 7b943365c888ed5d0952c5601fd7ba15996c1cb9; current head is unchanged, CI gate passes, and all PR discussion/review endpoints are empty before this submission.

The mutex correctly covers both append and response construction, preserving stable cumulative snapshots and sequential output. No essential findings; independent review agrees.

Prior exact-head validation reproduced the unpatched race and passed local targeted race/shuffle tests: think package ×100, think + dispatcher packages ×20, and actual-runtime mock-model probes ×10. Canonical CGO-enabled build/test/lint passed. PR race CI is skipped, not passed; the full-repository race suite was not run locally.

Conversation isolation is a separate, pre-existing lifetime concern and is being addressed in a separately scoped issue/fix; it does not block this synchronization fix.

@aheritier
aheritier added this pull request to the merge queue Oct 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 13, 2026
@aheritier
aheritier added this pull request to the merge queue Oct 13, 2026
Merged via the queue into docker:main with commit f412343 Oct 13, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code Quality] Think tool races under parallel tool execution

2 participants