fix: move registerDefaultTools to constructor to prevent concurrent map writes#2041
Conversation
There was a problem hiding this comment.
Review Summary
Assessment: 🟢 APPROVE
This is a correct concurrency fix. Moving registerDefaultTools() from the RunStream() goroutine to the NewLocalRuntime() constructor ensures that the toolMap is fully populated during single-threaded construction, preventing the fatal error: concurrent map writes panic when multiple goroutines call RunStream() concurrently.
What was verified:
- The
r.bgAgentsfield is already initialized beforeregisterDefaultTools()is called in the constructor - The change maintains the correct initialization order
- All subsequent accesses to
r.toolMapinprocessToolCalls()are read-only, which is safe for concurrent goroutines - No other code writes to
r.toolMapafter construction
Concurrency safety:
✅ Single-threaded write during construction
✅ Concurrent read-only access during runtime
✅ No data races introduced
No issues found in the changed code.
|
|
||
| // Register runtime-managed tool handlers once during construction. | ||
| // This avoids concurrent map writes when multiple goroutines call | ||
| // RunStream on the same runtime (e.g. background agent sessions). |
There was a problem hiding this comment.
Background agent sessions are using the same runtime??
There was a problem hiding this comment.
As far as I understand, yes. I'm still trying to improve the code
b565b8b to
0f2ab92
Compare
…ap writes registerDefaultTools() was called inside the RunStream() goroutine, which writes to the plain map r.toolMap. When multiple goroutines call RunStream on the same LocalRuntime concurrently (e.g. background agent sessions), this causes a fatal 'concurrent map writes' panic. Move the call to NewLocalRuntime() so the map is fully populated before any concurrent access. All subsequent reads in processToolCalls are safe without synchronization. Assisted-By: docker-agent
0f2ab92 to
af3b056
Compare
Problem
registerDefaultTools()was called inside theRunStream()goroutine, which writes to the plainmap[string]ToolHandlerFuncfieldr.toolMap. When multiple goroutines callRunStreamon the sameLocalRuntimeconcurrently (e.g. background agent sessions), this causes afatal error: concurrent map writespanic.Fix
Move the
r.registerDefaultTools()call fromRunStream()toNewLocalRuntime(), so the map is fully populated during single-threaded construction. All subsequent accesses inprocessToolCallsare read-only, which is safe for concurrent goroutines.Why this is safe
*LocalRuntimeand never change.r.bgAgentsis already initialized beforeregisterDefaultTools()is called.r.toolMapafter construction.