fix: prevent concurrent LoadWithConfig calls from sharing working dir - #3842
Merged
Conversation
WithWorkingDir now clones the RuntimeConfig when the supplied directory differs from the caller's, so concurrent API-server sessions can't clobber each other's shell/filesystem/git toolset roots. Assisted-By: Claude
Sayt-0
approved these changes
Jul 27, 2026
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.
pkg/teamloader.LoadWithConfigpreviously mutated the caller's*config.RuntimeConfigdirectly — writingrunConfig.WorkingDirto the value supplied byWithWorkingDir, then restoring the original in adefer. The API server holds a single sharedsm.runConfigand passes it toLoadWithConfigfor every session. Because sessions load concurrently, two simultaneousRunSessioncalls could observe each other's working directory inside that mutate-and-restore window. Toolsets (shell,filesystem,git,backgroundjobs,mcp,lsp) readrunConfig.WorkingDirat creation time, so a session's tools could be rooted in another session's directory — a genuine isolation break, not just a cosmetic race.When
WithWorkingDirsupplies a directory that differs fromrunConfig.WorkingDir,LoadWithConfignow clones theRuntimeConfigand works on the copy. The caller's config is never touched, so there is no window to race on. Callers that do not passWithWorkingDir(CLI, MCP, A2A, ACP, and others) keep the existing behaviour, including theModels/Providers/ProviderRegistrywrite-back;pkg/serverreads model info fromLoadResultrather than from the config, so nothing depends on the write-back on that path.TestLoadWithConfig_WithWorkingDirIsConcurrencySafereproduces the bug: 8 concurrent loads share oneRuntimeConfig, each with its ownWithWorkingDir, and a recording toolset registry captures which directory each toolset was built with. Against the old code all 8 sessions got the same directory; the test passes cleanly with the fix, including under-race -count=3.