Clone shared runOptions before appending per-run options - #555
Clone shared runOptions before appending per-run options#555PratikDhanave wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a concurrency/data-race bug where per-run options could be appended into the Agent’s shared runOptions backing array due to spare slice capacity, causing cross-run option corruption under concurrent runs.
Changes:
- Clone
a.runOptionsinprepareRunbefore prepending run-scoped options to avoid mutating shared backing storage. - Add a regression test that runs many concurrent agent runs (with configured tools to ensure spare capacity) and relies on
-raceto detect the prior data race.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| agent/agent.go | Clones shared runOptions before append to prevent concurrent mutation of the shared slice backing array. |
| agent/agent_test.go | Adds a -race regression test covering concurrent runs to ensure options aren’t shared/mutated across runs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ca4688e to
342317c
Compare
This comment has been minimized.
This comment has been minimized.
342317c to
60c9a6c
Compare
This comment has been minimized.
This comment has been minimized.
prepareRun prepended the agent-configured run options with append(a.runOptions, options...). a.runOptions is shared across every run of the Agent and is built in New with slices.Clone followed by a per-tool append, so it routinely carries spare capacity. Appending onto it wrote each run's per-run options (the synthesized WithSession/noSessionProvided, or a caller's WithServiceID) into that shared backing array. Under concurrent runs of one Agent this is a data race, and one run can observe another run's clobbered session/service ID. Clone runOptions before appending so each run gets a private slice.
60c9a6c to
69d4677
Compare
Parity Review: No Issues FoundThis PR fixes an internal data race in Scope: Internal unexported logic only — Cross-repo parity: Not applicable. This is a Go-internal concurrency correctness fix with no equivalent API surface in the upstream .NET or Python implementations to compare against. Label actions: Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "awmgmcpg"See Network Configuration for more information.
|
In
prepareRun, the agent-configured run options were prepended with:a.runOptionsis a field shared across every run of theAgent, andNewbuilds it withslices.Clone(cfg.RunOptions)followed by a per-toolappend(..., WithTool(tool)), which routinely leaves the slice with spare capacity. Because of that spare capacity,append(a.runOptions, options...)writes each run’s per-run options — the synthesizedWithSession(...)/noSessionProvided(true), or a caller’sWithServiceID— intoa.runOptions[len:cap], memory shared across all runs.When a single
*Agent(with ≥1 configured tool, the normal case) serves concurrent runs — the natural usage for an agent framework — this is a data race, and one run can observe another run’s clobbered session/service ID reaching the provider. Sequential runs mask it because length-bounded reads never seerunOptions[len:].Every other analogous spot in the package already clones before appending a shared slice (
NewclonesRunOptions,Middlewares,prov.Middlewares;invokeclonesoptions); this prepend site missed it.Fix
Test
TestAgent_ConcurrentRunsDoNotShareRunOptionsbuilds an agent with tools (sorunOptionshas spare capacity) and issues 64 concurrent runs, each with its ownWithServiceID/WithSession. Under-raceit reports a DATA RACE inprepareRunon the old code and passes with the fix.