Skip to content

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

Open
dlanov wants to merge 1 commit into
docker:mainfrom
dlanov:fix-4568-think-tool-race
Open

dlanov wants to merge 1 commit into
docker:mainfrom
dlanov:fix-4568-think-tool-race

Conversation

@dlanov

@dlanov dlanov commented Oct 11, 2026

Copy link
Copy Markdown

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

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

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