Skip to content

[Error] unhandlederror-potential listener LEAK detected, popular — webview/webviewElement #312367

Closed

Description

@vs-code-engineering

Error Message

unhandlederror-potential listener LEAK detected, popular

Stack Trace

    at a.create (./src/vs/base/common/event.ts:1068:14)
    at _Ie._event (./src/vs/base/common/event.ts:1272:33)
    at Object.start (./src/vs/workbench/contrib/webview/browser/webviewElement.ts:804:0)
    at r$e.loadResource (./src/vs/workbench/contrib/webview/browser/webviewElement.ts:794:0)

Raw Stack Trace (minified)
Error     at a.create (out/vs/workbench/workbench.desktop.main.js:403:18436)     at _Ie._event (out/vs/workbench/workbench.desktop.main.js:405:2009)     at Object.start (out/vs/workbench/workbench.desktop.main.js:5205:27934)     at r$e.loadResource (out/vs/workbench/workbench.desktop.main.js:5205:27837)

Details

Property Value
Bucket ID 9c81d6ca-bb53-db80-91a5-89fe3ad53386
Version 1.118.0-insider
Commit 659d444a
Last Seen Thu Apr 23 2026 23:58:09 GMT+0000 (Coordinated Universal Time)
Platforms Mac, Windows, Linux
Product VSCode

This issue was automatically created from the VS Code Errors Dashboard

Activity

  1. added
    bug
    error-telemetry
    on Apr 24, 2026
  2. changed the title [-][Error] unhandlederror-potential listener LEAK detected, popular[/-] [+][Error] unhandlederror-potential listener LEAK detected, popular — webview/webviewElement[/+] on Apr 24, 2026
  3. vs-code-engineering commented on Apr 24, 2026

    @vs-code-engineering
    ContributorAuthor

    Regression Details

    Deminified Stack Summary

    The listener leak originates from webviewElement.ts:loadResource (line 794/804). The subscription chain is:

    1. event.ts → leak monitoring fires
    2. webviewElement.ts → loadResource subscribes to an event

    This is a popular leak — no single subscriber dominates; the webview element's event emitter has accumulated too many listeners across sessions.

    Generated by errors-regression-scan

    Generated by errors-regression-scan · ● 10.1M · ◷

  4. vs-code-engineering commented on Apr 24, 2026

    @vs-code-engineering
    ContributorAuthor

    🔍 Error Investigation

    Summary

    PR #311844 ("Use proper streams for loading webview resources") introduced a ReadableStream-based path for streaming webview resources. Inside the start callback of each ReadableStream, a listener is added to token.onCancellationRequested (line 804 of webviewElement.ts). Since all resource loads share a single _resourceLoadingCts token per WebviewElement instance, loading many resources concurrently (e.g., a markdown preview with many images or videos) causes the listener count on the token's emitter to exceed the leak detection threshold, triggering this "popular" leak warning. The error affects all platforms (Mac, Windows, Linux) and has impacted ~1,247 users across 1,793 hits.

    Culprit Commit

    Field Value
    Commit b980ef34
    Author @mjbvz
    PR #311844
    Message Use proper streams for loading webview resources
    Why Introduced a new ReadableStream code path where each resource load subscribes to token.onCancellationRequested(close) on a shared CancellationTokenSource. Concurrent resource loads accumulate listeners on the same emitter, exceeding the leak detection threshold.

    Code Flow

    sequenceDiagram
        participant WV as Webview iframe
        participant WE as WebviewElement
        participant RS as ReadableStream (per resource)
        participant CT as CancellationToken (_resourceLoadingCts)
        participant EM as Emitter (leak monitor)
    
        WV->>WE: load-resource (×N concurrent)
        loop For each resource
            WE->>WE: loadResource(id, uri, opts, token)
            WE->>RS: new ReadableStream({ start })
            RS->>CT: token.onCancellationRequested(close)
            Note over CT: Listener count += 1
        end
        Note over EM: ⚠️ Root cause:<br/>Listener count exceeds threshold<br/>(N concurrent loads on same token)
        EM-->>EM: 💥 ListenerLeakError thrown:<br/>"potential listener LEAK detected, popular"
    
    Loading

    Affected Files

    File Role
    src/vs/workbench/contrib/webview/browser/webviewElement.ts Crash site & root cause — loadResource() (line 794) creates ReadableStream whose start callback subscribes to shared cancellation token (line 804)
    src/vs/base/common/event.ts Leak detection infrastructure — LeakageMonitor fires ListenerLeakError when threshold exceeded (line 1068, 1272)
    src/vs/base/common/cancellation.ts CancellationTokenSource creates an Emitter<void> without a custom leak threshold (line 84)

    Repro Steps

    1. Open VS Code Insiders (1.118.0+)
    2. Create a Markdown file that references many local resources (e.g., 30+ images or videos)
    3. Open the Markdown Preview
    4. Observe the "potential listener LEAK detected, popular" error in telemetry/developer tools — the shared _resourceLoadingCts.token accumulates one listener per concurrent resource load

    Fix Proposal

    Fix location: src/vs/workbench/contrib/webview/browser/webviewElement.ts → loadResource()

    Approach: The core issue is that all concurrent resource loads share a single _resourceLoadingCts token and each adds a listener via token.onCancellationRequested(close) inside the ReadableStream.start callback. Two approaches:

    1. Per-resource linked token (preferred): Create a child CancellationTokenSource linked to _resourceLoadingCts for each loadResource call. This isolates each resource's listener to its own token emitter. Dispose the child token when the stream completes or errors.

    2. Raise the leak threshold: Create the _resourceLoadingCts emitter with a higher leakWarningThreshold to tolerate many concurrent resource loads. This is simpler but masks genuine leaks.

    Recommended Owner

    @mjbvz — culprit commit author (PR #311844) and webview area owner

    Generated by errors-investigate · ● 8.4M · ◷

  5. added this to the 1.118.0 milestone on Apr 24, 2026
  6. mjbvz commented on Apr 24, 2026

    @mjbvz
    Contributor

    Likely not an actual leak since we dispose these but lets try to fix this

  7. added a commit that references this issue on Apr 25, 2026
    38c9d8e
  8. added
    unreleased
    insiders-released
    and removed
    unreleased
    on Apr 25, 2026
  9. added
    ai-generated
    ai-analyzed
    on Apr 28, 2026
  10. locked and limited conversation to collaborators on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai-analyzedai-generatedbugerror-telemetryinsiders-releasedrecent-regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions