Repository navigation
agentHost: do not schedule a Copilot model refresh retry after teardown - #342299
Merged
Alexandru Dima (alexdima) merged 1 commit intoOct 17, 2026
Conversation
Shutdown clears a pending model refresh retry, but does not wait for a refresh that is handling a failure, which can take a while when a closed connection is recovered. Such a refresh then scheduled a retry after teardown had begun. On a disposed agent the retry timer leaked, and the test leak check reported it in whichever test ran next. The refresh now stops after handling the failure if teardown has begun, as it already does before. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused guard addresses the race, the regression test covers its ordering, and no actionable issues remain; tests were not rerun during review.
No open issues 🎉
What changed in this PR
Prevents Copilot model-refresh retry timers from outliving agent teardown, addressing a leak exposed by parallel unit tests.
Changes:
- Rechecks shutdown and disposal state after asynchronous failure handling.
- Adds a regression test that pauses failure handling while tearing down the agent.
| File | Description |
|---|---|
| src/vs/platform/agentHost/test/node/copilotAgent.test.ts | Tests teardown during model-refresh failure handling. |
| src/vs/platform/agentHost/node/copilot/copilotAgent.ts | Prevents retry scheduling after teardown. |
Review effort: Balanced
Alexandru Dima (alexdima)
marked this pull request as ready for review
October 17, 2026 17:16
Alexandru Dima (alexdima)
enabled auto-merge (squash)
October 17, 2026 17:17
Ladislau Szomoru (lszomoru)
approved these changes
Oct 17, 2026
Alexandru Dima (alexdima)
deleted the
agents/copilot-agent-no-model-refresh-retry-after-teardown
branch
October 17, 2026 17:20
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.
cc Don Jayamanne (@DonJayamanne) roblourens
Found while running the unit tests in parallel worker processes (#342237). Follow-up to #342267, which fixed the test cleanup order behind the same leak report; this fixes the product side.
Problem
A
CopilotAgentmodel refresh can schedule its retry timer after the agent was torn down. On a disposed agent the timer leaks. The unit tests report it in whichever test runs next (CI):Root cause
_refreshModelsalready avoids work once teardown has begun: it returns early when_shutdownPromiseis set, both at its start and right after a failed request.shutdown()clears a pending retry (_modelRefreshRetry.clear()).But on a failure, the refresh first awaits
_handleClientOperationFailure, which can take a while (for example while a closed connection is recovered).shutdown()does not wait for an in-flight refresh. If teardown begins during that await, the refresh then resumes and schedules a new retry:shutdown(), the timershutdown()meant to clear is created anyway, and fires later just to return;dispose(), it is assigned to the already disposed_modelRefreshRetryMutableDisposable, which ignores it, so the timer leaks.Fix
After
_handleClientOperationFailure, the refresh returns if teardown has begun (_shutdownPromiseor a disposed store), as it already does before the await.Proof
New regression test:
does not schedule a model refresh retry when the agent is torn down while a refresh failure is handled. The first model request fails, failure handling is held open, the agent is torn down with the test suite'sdisposeAgent, and then failure handling finishes.main+ the new test (before)There are 1 undisposed disposables!with the leak atdisposableTimeout←CopilotAgent._refreshModels(compiled line 2621, the same as in CI)Validation
./scripts/test.sh --run src/vs/platform/agentHost/test/node/copilotAgent.test.tsnpm run test-node -- --run src/vs/platform/agentHost/test/node/copilotAgent.test.tsnpm run eslint -- src/vs/platform/agentHost/node/copilot/copilotAgent.ts src/vs/platform/agentHost/test/node/copilotAgent.test.tsnpm run typecheck-client*
CopilotAgent › scoped credentials › isolates peer runtimes, preserves local adoption identity and refuses revoked prepared workalso fails on unchangedmainon my machine. It passes in CI and is unrelated.