WebCodecs decoders and encoders dropped without close() are never collected
Categories
(Core :: Audio/Video: Web Codecs, defect, P2)
Tracking
()
People
(Reporter: nicfio, Assigned: nicfio, NeedInfo)
References
(Blocks 1 open bug)
Details
Attachments
(2 external files, 2 obsolete files)
The callbacks that DecoderTemplate and EncoderTemplate register on the shutdown blocker promise and on the StrongWorkerRef capture a strong reference to the template. A configured decoder or encoder that the page drops without calling close() is therefore never collected, and its underlying platform decoder or encoder stays allocated until shutdown. This affects all platforms; it matters most on Android, where only a few MediaCodec instances can exist at once.
Steps to reproduce:
- In a page (or a worker), create and configure several VideoDecoders in a loop without calling close().
- Drop the references and force a GC/CC.
- The decoders stay alive. On the Android x86_64 emulator, whose H.264 decoder allows 4 instances, the next configure() fails.
Proposed fix (patches will follow on Phabricator):
- Part 1: capture a WeakPtr in those callbacks and check it on entry; release the agent from new destructors. The cycle-collected classes use NS_IMPL_CYCLE_COLLECTION_WEAK_PTR_INHERITED.
- Part 2: a mochitest, test_abandoned_codecs.html. For VideoDecoder, AudioDecoder, VideoEncoder and AudioEncoder (VP8 and Opus, software on every platform), on the main thread and in a worker, it configures a codec, drops it without close() and checks with a WeakRef that it is collected after GC/CC. Without Part 1 it fails 8 times out of 8 (four classes, main thread and worker); with Part 1 it passes. The WebCodecs WPT on the Android emulator, worker variants included, show no regression.
Question for reviewers: with this fix, abandoned decoders are released at the next natural GC, which in the WPT videoDecoder-codec-specific comes too late for the emulator's limit of 4 instances, so some of those subtests still time out there. The clean answer seems to be the "codec reclamation" that the specification allows. Would you accept work in that direction, or prefer to keep that limit as an expected failure on the emulator?
See also bug 2026835, which touches the same code for the close() case.
AI assistance: these patches were developed with AI coding assistants: the code, its review and the test runs were done by AI agents under my direction, on my own devices (an Android emulator and a Samsung Galaxy S23+). I take responsibility for submitting them and will answer review comments, with the same tools.
| Assignee | ||
Comment 1•20 days ago
|
||
I'm attaching the two patches as files because I can't use Phabricator at the moment. They are git format-patch output against mozilla-central bf75471716 and apply in order (Part 1, then Part 2). If someone could import them into Phabricator, or tell me the preferred way to proceed, I'd be grateful. Suggested reviewers: #media-playback-reviewers.
| Assignee | ||
Comment 2•20 days ago
|
||
Comment 3•20 days ago
|
||
| gecko-triage | ||
Thank you for the report and the patches.
Analysis
The current tree confirms the cause. In CreateDecoderAgent, the resolve and reject callbacks on the shutdown blocker promise capture self = RefPtr{this} (DecoderTemplate.cpp). CreateEncoderAgent does the same (EncoderTemplate.cpp). The TicketBlocker on the global AsyncShutdown barrier owns that promise (MediaUtils.cpp). The promise resolves only at shutdown. It rejects only when DestroyDecoderAgentIfAny or DestroyEncoderAgentIfAny destroys the ticket, and these run only on reset(), close(), an error or a new configure().
In a worker, the StrongWorkerRef callback also captures RefPtr{this} (DecoderTemplate.cpp, EncoderTemplate.cpp). The template keeps that worker ref in mWorkerRef, so the cycle collector cannot see this cycle. The worker ref releases the callback only when the worker shuts down. The cycle collection macros of the four classes traverse only the JS callbacks.
This cause explains each observation in the report. It applies to all four classes, on the main thread and in a worker. The platform decoder or encoder stays allocated until shutdown, so the leak checks in CI do not find it. This is not a regression: the strong captures come from the first landing of each template.
Fix direction
The attached Part 1 follows the correct direction. The callbacks capture a WeakPtr, and a new destructor calls the Destroy*AgentIfAny() function. The destructor is necessary: it destroys the ticket, which rejects the promise and releases the ref captures. Part 2 can show the fix on every platform, because VP8 and Opus use software codecs. Part 1 applies to the current tree with a small line offset.
Bug 2026835 changes the same template code for the close() case. This bug blocks the WebCodecs meta bug 1746557.
Next steps
Nicola is working on a fix (comment 1 and comment 2). The contributor quick reference shows how to submit patches to Phabricator with moz-phab.
The open design question is the WPT timeout on the Android emulator after the fix. Firefox has no codec reclamation code in dom/media/webcodecs today. C.M. Chang, could you please answer the question in comment 0? The question is whether to work toward codec reclamation or to keep the emulator limit as an expected failure.
If you'd like to provide feedback on this comment, please use the 👍 or 👎 reaction.
| Assignee | ||
Comment 4•20 days ago
|
||
The callbacks registered on the shutdown blocker promise and on the
StrongWorkerRef captured a strong reference to the DecoderTemplate or
EncoderTemplate. A configured decoder or encoder that the page dropped
without calling close() was therefore never collected, and its
underlying decoder or encoder (a MediaCodec instance on Android, where
only a few can exist at once) stayed allocated until shutdown.
Capture a WeakPtr in those callbacks and check it on entry, and release
the agent from the destructor.
The test for this is added in Part 2.
Updated•20 days ago
|
| Assignee | ||
Comment 5•20 days ago
|
||
Create and configure a VideoDecoder, AudioDecoder, VideoEncoder and
AudioEncoder, on the main thread and in a dedicated worker, drop them
without calling close(), and check with a WeakRef that GC and CC collect
them. Without the previous patch the shutdown blocker promise and the
StrongWorkerRef callbacks keep them alive until shutdown.
| Assignee | ||
Comment 6•20 days ago
|
||
Comment on attachment 9671630 [details] [diff] [review]
Part 1: Don't let the shutdown blocker and worker ref keep abandoned WebCodecs decoders and encoders alive
Thanks for linking bug 1932526. I hadn't found it before filing this one.
I now have Phabricator access, so I've submitted the patches there: Part 1 is D338350 and Part 2 is D338351. They replace attachments 9671630 and 9671631, which I've marked as obsolete.
About the approach: this is the same kind of leak that bug 1927020 fixed for VideoFrame. A shutdown callback held a strong reference that the cycle collector could not see. Bug 1927020 fixed it with ShutdownWatcher. Part 1 uses a WeakPtr instead: the callbacks of the shutdown blocker promise and of the StrongWorkerRef now hold a WeakPtr, and a new destructor shuts the agent down. I kept the ShutdownBlockingTicket on purpose: it still blocks shutdown until the underlying decoder or encoder has shut down, even after the WebCodecs object is gone. Since the ticket stays, nothing has to replace it. So the extra nsIAsyncShutdownBlocker on the main thread that bug 1932526 comment 0 mentions for RemoteMediaDataDecoder is not needed. Part 2 is a mochitest that checks that abandoned decoders and encoders are garbage collected, on the main thread and in a worker. It doesn't depend on how the fix is done.
If you'd rather use ShutdownWatcher/ShutdownConsumer, so that all the WebCodecs objects share one mechanism, I'm happy to rework Part 1 that way, now or as a follow-up. Feel free to dupe this bug to bug 1932526, or the other way round, whichever you prefer.
| Assignee | ||
Updated•20 days ago
|
| Assignee | ||
Comment 7•7 days ago
|
||
Hi Chun-Min, a gentle ping on this one. D338350 and D338351 have been in the media-playback-reviewers queue for about two weeks, with no reviewer assigned.
The question from comment 6 is still open, and it's yours to decide: should I keep the WeakPtr approach in Part 1, rework it on top of ShutdownWatcher as planned in bug 1932526, or close this bug as a duplicate? I'm happy to do any of these; I'd just rather not rework it in a direction you don't want. Part 2 (the GC test) should be useful in every case.
Description
•