Skip to content

Commit 2143789

Browse files
committed
fix(McpHub): register the oauth watcher before installing the cancellation callback
The previous TDZ fix made cancellationDisposable safe under synchronous callback firing, but the _oauthWatchers.set(watcherKey, ...) call after the onCancellationRequested(...) registration is still racy. If the token is already cancelled at registration time and the callback fires synchronously, cleanup() runs (which tries to _oauthWatchers.delete the entry) before the entry has been added, leaving an orphan watcher in the map after the flow has otherwise torn itself down. Moved the watcher registration above the onCancellationRequested call so the entry exists in the map before the callback can fire. A sync-firing cleanup() now finds and deletes it cleanly. Extended the existing already-cancelled-token regression test to assert _oauthWatchers is empty after the flow resolves.
1 parent efc8678 commit 2143789

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎src/services/mcp/McpHub.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,14 @@ export class McpHub {
11861186
resolve()
11871187
}, OAUTH_FLOW_TIMEOUT_MS)
11881188

1189+
// Register in _oauthWatchers so deleteConnection() and dispose() can clean up.
1190+
// This must happen BEFORE installing the cancellation callback below: if the
1191+
// token is already cancelled at registration time, VS Code fires the callback
1192+
// synchronously inside onCancellationRequested(...), and cleanup() must find
1193+
// the watcher entry in the map in order to delete it. Registering after the
1194+
// callback would leave an orphan entry that survives disposal.
1195+
this._oauthWatchers.set(watcherKey, { unsubscribe, abortHandle: timeoutHandle })
1196+
11891197
// --- Cancellation (progress bar's Cancel button) ---
11901198
const cancellationDisposable = cancellationToken.onCancellationRequested(() => {
11911199
if (disposed) return
@@ -1207,9 +1215,6 @@ export class McpHub {
12071215
resolve()
12081216
})
12091217

1210-
// Register in _oauthWatchers so deleteConnection() and dispose() can clean up
1211-
this._oauthWatchers.set(watcherKey, { unsubscribe, abortHandle: timeoutHandle })
1212-
12131218
// In test mode, skip user interaction and proceed directly to complete the OAuth flow.
12141219
if (process.env.MCP_OAUTH_TEST_MODE === "true") {
12151220
void (async () => {

‎src/services/mcp/__tests__/McpHub.spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,15 @@ describe("McpHub", () => {
26182618
expect(mockConnection.server.status).toBe("disconnected")
26192619
expect((mcpHub as any).appendErrorMessage).toHaveBeenCalled()
26202620
expect(mockAuthProvider.close).toHaveBeenCalled()
2621+
2622+
// Pin the watcher-after-cleanup race: when the cancellation callback fires
2623+
// synchronously during onCancellationRequested(...) registration, cleanup()
2624+
// runs before the _oauthWatchers.set() that would have added the entry.
2625+
// If the registration happens AFTER the callback is installed, the entry
2626+
// is added after disposal and remains orphaned in the map. Registering the
2627+
// watcher BEFORE installing the callback ensures cleanup() finds and
2628+
// deletes it cleanly.
2629+
expect((mcpHub as any)._oauthWatchers.has(`${serverName}:${source}`)).toBe(false)
26212630
})
26222631

26232632
it("should disconnect and flag error when OAuth flow times out", async () => {

0 commit comments

Comments
 (0)