Skip to content

Fix memory leak in CapturingToken correlation map#3475

Merged
zhichli merged 2 commits intozhichli/fixExportfrom
copilot/sub-pr-3474
Feb 5, 2026
Merged

Fix memory leak in CapturingToken correlation map#3475
zhichli merged 2 commits intozhichli/fixExportfrom
copilot/sub-pr-3474

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

The capturingTokenCorrelationMap was leaking entries when requests failed before reaching BYOK providers or when non-BYOK models were used.

Changes

  • Added finally block in ExtensionContributedChatEndpoint.makeChatRequest2() to ensure correlation map cleanup in all code paths
  • Cleanup is idempotent: if BYOK providers already retrieved the token, the finally block safely returns undefined
try {
  const response = await this.languageModel.sendRequest(...);
  // ... process response
} catch (e) {
  // ... error handling
} finally {
  // Clean up correlation map entry to prevent memory leak.
  // If the request reached a BYOK provider, they already retrieved and removed this.
  // If not (e.g., request failed early or model isn't BYOK), we need to clean it up here.
  retrieveCapturingTokenByCorrelation(ourRequestId);
}

Addresses feedback from #3474: #3474 (comment)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Add finally block to ensure correlation ID is cleaned up from the
capturingTokenCorrelationMap even when requests fail before reaching
BYOK providers. This prevents memory leaks when:
- languageModel.sendRequest throws immediately
- The model is not a BYOK provider
- Any other failure occurs before BYOK provider retrieval

The retrieveCapturingTokenByCorrelation call is idempotent - if BYOK
providers already retrieved the token, this will return undefined.

Co-authored-by: zhichli <57812115+zhichli@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix feedback on BYOK request logging improvements Fix memory leak in CapturingToken correlation map Feb 5, 2026
Copilot AI requested a review from zhichli February 5, 2026 08:29
@zhichli zhichli marked this pull request as ready for review February 5, 2026 17:22
@zhichli zhichli merged commit ee75622 into zhichli/fixExport Feb 5, 2026
1 of 2 checks passed
@zhichli zhichli deleted the copilot/sub-pr-3474 branch February 5, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants