Bug 313389

Summary: WorkerMessagingProxy objects leak after calling worker.terminate() from JavaScript
Product: WebKit Reporter: David Kilzer (:ddkilzer) <ddkilzer>
Component: DOMAssignee: David Kilzer (:ddkilzer) <ddkilzer>
Status: RESOLVED FIXED    
Severity: Normal CC: webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on: 22723    
Bug Blocks:    

David Kilzer (:ddkilzer)
Reported 2026-04-26 22:58:25 PDT
`WorkerMessagingProxy` objects leak after calling `worker.terminate()` from JavaScript. The proxy ref-counting has a two-phase cleanup: `terminateWorkerGlobalScope()` shuts down the worker thread (phase 1), then `workerObjectDestroyed()` releases the initial ref when the `Worker` DOM object is destroyed (phase 2). The bug is that phase 1 clears `m_scriptExecutionContextIdentifier`, which phase 2 needs to post its cleanup task. Specifically, `workerGlobalScopeDestroyedInternal()` runs with `m_mayBeDestroyed == false` (set only by `workerObjectDestroyed()`), so it skips `deref()` and sets `m_scriptExecutionContextIdentifier = std::nullopt`. When `Worker::~Worker()` later calls `workerObjectDestroyed()`, the nullopt identifier causes an early return, and `deref()` is never called. A secondary retain cycle exists when messages are queued in `m_queuedEarlyTasks` before the worker thread is created. The queued task lambdas capture `Ref { *this }`, creating a cycle through the proxy's own member vector that prevents destruction even if `deref()` were called. Observed as ROOT LEAK (957 instances of `WorkerMessagingProxy`) and ROOT CYCLE (88 retain cycles with `WorkerMessagingProxy` and `CallableWrapper`) across 20 of 103 WebContent process memgraphs from a full `run-webkit-tests --leaks --debug` run. Tests that exercise the bug include: - workers/worker-terminate-crash.html - workers/bomb.html - workers/worker-set-delete-terminate-crash.html
Attachments
Radar WebKit Bug Importer
Comment 1 2026-04-26 22:58:33 PDT
David Kilzer (:ddkilzer)
Comment 2 2026-04-26 23:34:03 PDT
EWS
Comment 3 2026-04-28 19:52:14 PDT
Committed 312256@main (80cc99fb32cb): <https://commits.webkit.org/312256@main> Reviewed commits have been landed. Closing PR #63665 and removing active labels.
Note You need to log in before you can comment on or make changes to this bug.