Clone shared runOptions before appending per-run options#555
Open
PratikDhanave wants to merge 1 commit into
Open
Clone shared runOptions before appending per-run options#555PratikDhanave wants to merge 1 commit into
PratikDhanave wants to merge 1 commit into
Conversation
Contributor
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.
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.
PratikDhanave
force-pushed
the
fix-agent-runoptions-shared-slice-race
branch
from
July 23, 2026 08:44
f2e9a79 to
ca4688e
Compare
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.
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.