Skip to content

agentHost: do not schedule a Copilot model refresh retry after teardown - #342299

Merged
Alexandru Dima (alexdima) merged 1 commit into
mainfrom
agents/copilot-agent-no-model-refresh-retry-after-teardown
Oct 17, 2026
Merged

Alexandru Dima (alexdima) merged 1 commit into
mainfrom
agents/copilot-agent-no-model-refresh-retry-after-teardown

Conversation

@alexdima

@alexdima Alexandru Dima (alexdima) commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

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 CopilotAgent model 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):

Error: There are 1 undisposed disposables!
  at disposableTimeout (async.js)
  at CopilotAgent._refreshModels (copilotAgent.js:2621)

Root cause

_refreshModels already avoids work once teardown has begun: it returns early when _shutdownPromise is 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:

  • after shutdown(), the timer shutdown() meant to clear is created anyway, and fires later just to return;
  • after dispose(), it is assigned to the already disposed _modelRefreshRetry MutableDisposable, which ignores it, so the timer leaks.

Fix

After _handleClientOperationFailure, the refresh returns if teardown has begun (_shutdownPromise or 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's disposeAgent, and then failure handling finishes.

Result
main + the new test (before) 3/3 failing: There are 1 undisposed disposables! with the leak at disposableTimeout ← CopilotAgent._refreshModels (compiled line 2621, the same as in CI)
This PR (after) 3/3 passing

Validation

Command Result
./scripts/test.sh --run src/vs/platform/agentHost/test/node/copilotAgent.test.ts 811 passing, 1 pending, 1 failing*
npm run test-node -- --run src/vs/platform/agentHost/test/node/copilotAgent.test.ts 811 passing, 1 pending, 1 failing*
npm run eslint -- src/vs/platform/agentHost/node/copilot/copilotAgent.ts src/vs/platform/agentHost/test/node/copilotAgent.test.ts Passed
npm run typecheck-client Passed

* CopilotAgent › scoped credentials › isolates peer runtimes, preserves local adoption identity and refuses revoked prepared work also fails on unchanged main on my machine. It passes in CI and is unrelated.

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>
Copilot AI balanced review requested due to automatic review settings October 17, 2026 16:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@alexdima
Alexandru Dima (alexdima) merged commit 9cfaeb5 into main Oct 17, 2026
50 checks passed
@alexdima
Alexandru Dima (alexdima) deleted the agents/copilot-agent-no-model-refresh-retry-after-teardown branch October 17, 2026 17:20
@vs-code-engineering vs-code-engineering Bot added this to the 1.143.0 milestone Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants