Skip to content

webview: fix resource loading cancellation listener leaks - #338306

Merged
Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/fix-unhandled-error-listener-leak
Sep 28, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 1 commit into
mainfrom
agents/fix-unhandled-error-listener-leak

Conversation

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Fixes #319468

Summary

  • Scope cancellation and cleanup to each webview resource request instead of attaching every read to one shared cancellation emitter. Keep the request alive until streaming finishes, fails, is cancelled, or the webview is disposed.
  • Propagate cancellation of an Electron transferable response stream to the underlying file read without destroying the file stream's end/error cleanup.
  • Dispose the file service's linked cancellation source when stream setup throws synchronously.
  • Add regression coverage for parallel reads, repeated read failures, cancellation, disposal, sibling buffer views, and file-handle closure.

Reproduction

Before the fix, 200 parallel resource loads reproduced the exact potential listener LEAK detected, popular warning and call stack from the issue. Five synchronous read failures retained 1, 2, 3, 4, and 5 listeners; cancelling a transferred response left its file read running. These cases now pass, and three batches of 200 reads return to zero active requests after each batch.

The parallel-load warning is caused by shared-token fan-out; the failed/aborted-read cases also exposed actual cleanup problems. This does not claim to resolve the separate high-CPU/freeze symptoms reported in the issue comments.

Validation

  • npm run gulp compile-client: passed, zero errors.
  • Full client type-check, targeted ESLint, and git diff --check: passed.
  • Code OSS Electron stream/cancellation, file-service, text-file I/O, webview, and panel suites: 165 passed, 4 platform-specific skips.
  • Real disk-I/O integration tests: 97 passed, 3 platform-specific skips.
  • WebKit fallback-streaming and related suites: 93 passed, 7 platform/transferable-stream-specific skips.
  • Real Code OSS webview API stress test: 3 consecutive fresh-window runs passed, each fetching 625 files x 128 KiB through a webview and its service worker (3-4 seconds per run).
  • Re-ran the two changed suites against the compiled client output: 33 passed.

Historical regression coverage includes #326272/#326092 (resource concurrency), #321291/#316841 (shared-buffer detachment), #114428/#114024 (handle closure on cancellation/errors), #138805/#132771 (binary-read cancellation), #337018 (pending stream writes), and #312367 (listener/controller cleanup).

Native validation was on Windows; Linux's FD ceiling and the reported macOS CPU spin were not directly exercised. The partially built development worktree logged unrelated missing built-in extension outputs during full-window tests; the webview tests passed, with no listener-leak, EMFILE, or unresponsive-renderer reports in their logs.

Scope cancellation to each resource request and release its lifetime when streaming completes, fails, or is cancelled. Cancel the underlying file read without destroying stream cleanup, and dispose linked cancellation sources when read setup throws.

Add regression coverage for parallel loads, cancellation, disposal, shared buffer views, and handle closure after read failures.

Fixes #319468

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings September 28, 2026 08:17

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.

Copilot review overview

🟢 Approval recommended

The lifecycle changes are internally consistent and covered across the relevant success, failure, cancellation, and disposal paths.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes webview resource-loading cancellation listener leaks by scoping cancellation and cleanup per request.

Changes:

  • Tracks each webview resource stream through completion, cancellation, failure, or disposal.
  • Cleans up file-service cancellation sources on synchronous setup failures.
  • Adds regression tests for concurrency, cancellation, errors, buffer views, and handle closure.
File Description
webviewElement.ts Adds request-scoped stream lifecycle management.
webviewElement.test.ts Covers resource-stream cleanup and concurrency.
fileService.ts Disposes cancellation state on setup failure.
fileService.test.ts Covers listener and file-handle cleanup.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dmitrivMS Dmitriy Vasyura (dmitrivMS) added freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues webview Webview issues labels Sep 28, 2026
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) marked this pull request as ready for review September 28, 2026 08:28
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 2f1fe6e into main Sep 28, 2026
36 checks passed
@dmitrivMS
Dmitriy Vasyura (dmitrivMS) deleted the agents/fix-unhandled-error-listener-leak branch September 28, 2026 08:39
@vs-code-engineering vs-code-engineering Bot added this to the 1.141.0 milestone Sep 28, 2026
Abdon Morales (abdonmorales) pushed a commit to abdonmorales/vscode-utcs that referenced this pull request Oct 1, 2026
…338306)

Scope cancellation to each resource request and release its lifetime when streaming completes, fails, or is cancelled. Cancel the underlying file read without destroying stream cleanup, and dispose linked cancellation sources when read setup throws.

Add regression coverage for parallel loads, cancellation, disposal, shared buffer views, and handle closure after read failures.

Fixes microsoft#319468

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 2f1fe6e)
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues webview Webview issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] unhandlederror-potential listener LEAK detected, popular

4 participants