Skip to content

script: GC media elements after playback ends - #49367

Merged
jdm merged 1 commit into
servo:mainfrom
sabbCodes:issue-49351
Oct 28, 2026
Merged

jdm merged 1 commit into
servo:mainfrom
sabbCodes:issue-49351

Conversation

@sabbCodes

Copy link
Copy Markdown
Contributor

Allowed the GC to collect unreferenced elements by calling update_keep_alive() after firing the 'ended' event in reaches_the_end_steps. Moved tests/wpt/mozilla/tests/mozilla/playing_media_no_gc.html into tests/wpt/mozilla/tests/memory directory.

Testing: ./mach test-wpt tests/wpt/mozilla/tests/memory/playing_media_no_gc.html passed cleanly w/o errors;

web-platform-test
~~~~~~~~~~~~~~~~~
Ran 2 checks (1 subtests, 1 tests)
Expected results: 2
Unexpected results: 0
OK

Fixes: #49351

@sabbCodes
sabbCodes requested a review from gterzian as a code owner October 28, 2026 13:01
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 28, 2026

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Oct 28, 2026
@jdm
jdm added this pull request to the merge queue Oct 28, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 28, 2026
Merged via the queue into servo:main with commit 812d247 Oct 28, 2026
38 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Oct 28, 2026
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Oct 30, 2026
Upstream servo#48979 keeps a media element alive through its owner document
while it plays or is eligible for autoplay, and servo#49367 lets it go after
it has ended. An element that delays the load event or seeks is still
collected at the next garbage collection once script drops it, which
stops it and lets the load event fire early, and the keep-alive is only
updated at a few points of the media element's algorithms.

Like Gecko, and Blink apart from autoplay, also keep a media element
alive while it delays the load event and, with a resource and no error,
while it seeks. Don't keep an element without a resource alive, and
keep one that is eligible for autoplay only until it has enough data:
then it either plays or never will. Update the keep-alive whenever the
network state, paused, seeking, the can autoplay flag, the error, the
ready state or the delaying-the-load-event flag changes.

Fork note: meant for upstream; found with the QuickJS backend, which
frees such elements as soon as script drops them, but SpiderMonkey loses
them too once a GC runs. Until the 2026-10-29 sync this patch had its
own keep-alive set; it now extends upstream's. Besides the upstream
change, this drops the QuickJS conditions in the expectations of
autoplay-disabled-by-permissions-policy; patch 49 dropped the others.
Drop once upstream has it.

Testing: Adds a WPT test that unreferenced media elements keep playing
across a garbage collection.

Validation: Release builds of upstream f040186 with SpiderMonkey, with and without the series, `./mach test-wpt --processes 4` of html/semantics/embedded-content/media-elements, the-video-element, the-audio-element, infrastructure/assumptions/allowed-to-play.html, webaudio/the-audio-api/the-mediaelementaudiosourcenode-interface and 2 _mozilla media tests: the new test passes 7 of 7 (harness timeout without), autoplay-disabled-by-permissions-policy and played-loop as in the updated expectations (autoplay-disabled timed out intermittently before, upstream issues servo#46012 and servo#46804); the remaining differences are track timeouts and track-cues-sorted-before-dispatch failures that both builds show intermittently, and one seek-to-max-value failure in 7 runs with the series (`currentTime` 0 right after setting it), not reproduced in 6 reruns, apart from load-removes-queued-error-event, which now hangs whenever GStreamer 1.24.2's playbin3 stalls on a `<video>` with an audio-only MP3: 4 of 6 runs on this host without the series too; the stalled element delays the load event, and before, only a garbage collection that freed it let the load event fire (upstream's intermittent tracker has 9 unexpected results for that test in total). The three tests that fail on QuickJS CI pass with forced GCs every 20 ms too. With logging of the keep-alive updates, every element in direct checks stopped being kept alive once it had been loaded and never played, played to the end, paused, replayed after the end without a seekable range, or played backwards to the start. An independent agent review found no blockers; its two should-fix findings for this patch (ended but unpaused elements were kept alive; changes of the error went through no hook) are fixed. QuickJS release build of this stack: the new tests and the media tests that had QuickJS expectations (autoplay.html, allowed-to-play.html, resource-selection-candidate-moved.html and the three autoplay permissions policy tests) give SpiderMonkey's results in 2 of 2 runs; without the series they time out as the dropped expectations said. 2026-10-29 sync, reworked onto upstream's keep-alive: release builds of this sync's stack and of plain upstream ed6a924, `./mach test-wpt --release` of the media-elements, the-video-element and the-audio-element directories with fetch, xhr, eventsource, cors, subresource-integrity, resource-timing, canvas image drawing and _mozilla/memory (2,247 tests), then 3 reruns on both of every test whose results differed: the same results on both except for intermittent ones, gc-unreferenced-media-element and upstream's playing_media_no_gc pass; a playing element's back/forward cache restore fails on both (upstream servo#48979 pauses it at unload). DAYU200 (OpenHarmony 7.0), headless release builds with SpiderMonkey and with QuickJS, `./mach test-wpt --ohos --processes 1 --timeout-multiplier 2` of the media series' tests, loading-the-media-resource, error-codes and playing-the-media-resource (75 tests): all as expected but the two autoplay permissions policy tests that pass where a timeout is expected, on both engines; no crash.
AI-assisted: Yes (agent session; root-caused, written and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution: the `HTMLMediaElement` import next to upstream's `TracedScriptResult` (servo#48855), and the patch's +8 bytes of `HTMLVideoElement` applied to upstream's new sizes (servo#48082))
AI-assisted: Yes (agent session; 2026-10-29 sync -- upstream servo#48979 and servo#49367 landed a keep-alive set of their own: the patch now extends upstream's `should_keep_alive` with its rules (load event delay, seeking, resource and HAVE_ENOUGH_DATA conditions) and keeps its setter hooks; its own set, flag and duplicate hooks are gone, as is its +8 bytes of `HTMLVideoElement`; subject and message rewritten)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
Upstream servo#48979 keeps a media element alive through its owner document
while it plays or is eligible for autoplay, and servo#49367 lets it go after
it has ended. An element that delays the load event or seeks is still
collected at the next garbage collection once script drops it, which
stops it and lets the load event fire early, and the keep-alive is only
updated at a few points of the media element's algorithms.

Like Gecko, and Blink apart from autoplay, also keep a media element
alive while it delays the load event and, with a resource and no error,
while it seeks. Don't keep an element without a resource alive, and
keep one that is eligible for autoplay only until it has enough data:
then it either plays or never will. Update the keep-alive whenever the
network state, paused, seeking, the can autoplay flag, the error, the
ready state or the delaying-the-load-event flag changes.

Fork note: meant for upstream; found with the QuickJS backend, which
frees such elements as soon as script drops them, but SpiderMonkey loses
them too once a GC runs. Until the 2026-10-29 sync this patch had its
own keep-alive set; it now extends upstream's. Besides the upstream
change, this drops the QuickJS conditions in the expectations of
autoplay-disabled-by-permissions-policy; patch 49 dropped the others.
Drop once upstream has it.

Testing: Adds a WPT test that unreferenced media elements keep playing
across a garbage collection.

Validation: Release builds of upstream f040186 with SpiderMonkey, with and without the series, `./mach test-wpt --processes 4` of html/semantics/embedded-content/media-elements, the-video-element, the-audio-element, infrastructure/assumptions/allowed-to-play.html, webaudio/the-audio-api/the-mediaelementaudiosourcenode-interface and 2 _mozilla media tests: the new test passes 7 of 7 (harness timeout without), autoplay-disabled-by-permissions-policy and played-loop as in the updated expectations (autoplay-disabled timed out intermittently before, upstream issues servo#46012 and servo#46804); the remaining differences are track timeouts and track-cues-sorted-before-dispatch failures that both builds show intermittently, and one seek-to-max-value failure in 7 runs with the series (`currentTime` 0 right after setting it), not reproduced in 6 reruns, apart from load-removes-queued-error-event, which now hangs whenever GStreamer 1.24.2's playbin3 stalls on a `<video>` with an audio-only MP3: 4 of 6 runs on this host without the series too; the stalled element delays the load event, and before, only a garbage collection that freed it let the load event fire (upstream's intermittent tracker has 9 unexpected results for that test in total). The three tests that fail on QuickJS CI pass with forced GCs every 20 ms too. With logging of the keep-alive updates, every element in direct checks stopped being kept alive once it had been loaded and never played, played to the end, paused, replayed after the end without a seekable range, or played backwards to the start. An independent agent review found no blockers; its two should-fix findings for this patch (ended but unpaused elements were kept alive; changes of the error went through no hook) are fixed. QuickJS release build of this stack: the new tests and the media tests that had QuickJS expectations (autoplay.html, allowed-to-play.html, resource-selection-candidate-moved.html and the three autoplay permissions policy tests) give SpiderMonkey's results in 2 of 2 runs; without the series they time out as the dropped expectations said. 2026-10-29 sync, reworked onto upstream's keep-alive: release builds of this sync's stack and of plain upstream ed6a924, `./mach test-wpt --release` of the media-elements, the-video-element and the-audio-element directories with fetch, xhr, eventsource, cors, subresource-integrity, resource-timing, canvas image drawing and _mozilla/memory (2,247 tests), then 3 reruns on both of every test whose results differed: the same results on both except for intermittent ones, gc-unreferenced-media-element and upstream's playing_media_no_gc pass; a playing element's back/forward cache restore fails on both (upstream servo#48979 pauses it at unload). DAYU200 (OpenHarmony 7.0), headless release builds with SpiderMonkey and with QuickJS, `./mach test-wpt --ohos --processes 1 --timeout-multiplier 2` of the media series' tests, loading-the-media-resource, error-codes and playing-the-media-resource (75 tests): all as expected but the two autoplay permissions policy tests that pass where a timeout is expected, on both engines; no crash.
AI-assisted: Yes (agent session; root-caused, written and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution: the `HTMLMediaElement` import next to upstream's `TracedScriptResult` (servo#48855), and the patch's +8 bytes of `HTMLVideoElement` applied to upstream's new sizes (servo#48082))
AI-assisted: Yes (agent session; 2026-10-29 sync -- upstream servo#48979 and servo#49367 landed a keep-alive set of their own: the patch now extends upstream's `should_keep_alive` with its rules (load event delay, seeking, resource and HAVE_ENOUGH_DATA conditions) and keeps its setter hooks; its own set, flag and duplicate hooks are gone, as is its +8 bytes of `HTMLVideoElement`; subject and message rewritten)
AI-assisted: Yes (agent session; 2026-11-09 sync -- upstream's WPT sync (servo#49485) now expects what this patch made autoplay-disabled-by-permissions-policy.https.sub.html do (FAIL instead of TIMEOUT), so of this patch's hunk only the removal of the QuickJS conditions remains)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 10, 2026
Upstream servo#48979 keeps a media element alive through its owner document
while it plays or is eligible for autoplay, and servo#49367 lets it go after
it has ended. An element that delays the load event or seeks is still
collected at the next garbage collection once script drops it, which
stops it and lets the load event fire early, and the keep-alive is only
updated at a few points of the media element's algorithms.

Like Gecko, and Blink apart from autoplay, also keep a media element
alive while it delays the load event and, with a resource and no error,
while it seeks. Don't keep an element without a resource alive, and
keep one that is eligible for autoplay only until it has enough data:
then it either plays or never will. Update the keep-alive whenever the
network state, paused, seeking, the can autoplay flag, the error, the
ready state or the delaying-the-load-event flag changes.

Fork note: meant for upstream; found with the QuickJS backend, which
frees such elements as soon as script drops them, but SpiderMonkey loses
them too once a GC runs. Until the 2026-10-29 sync this patch had its
own keep-alive set; it now extends upstream's. Besides the upstream
change, this drops the QuickJS conditions in the expectations of
autoplay-disabled-by-permissions-policy; patch 49 dropped the others.
Drop once upstream has it.

Testing: Adds a WPT test that unreferenced media elements keep playing
across a garbage collection.

Validation: Release builds of upstream f040186 with SpiderMonkey, with and without the series, `./mach test-wpt --processes 4` of html/semantics/embedded-content/media-elements, the-video-element, the-audio-element, infrastructure/assumptions/allowed-to-play.html, webaudio/the-audio-api/the-mediaelementaudiosourcenode-interface and 2 _mozilla media tests: the new test passes 7 of 7 (harness timeout without), autoplay-disabled-by-permissions-policy and played-loop as in the updated expectations (autoplay-disabled timed out intermittently before, upstream issues servo#46012 and servo#46804); the remaining differences are track timeouts and track-cues-sorted-before-dispatch failures that both builds show intermittently, and one seek-to-max-value failure in 7 runs with the series (`currentTime` 0 right after setting it), not reproduced in 6 reruns, apart from load-removes-queued-error-event, which now hangs whenever GStreamer 1.24.2's playbin3 stalls on a `<video>` with an audio-only MP3: 4 of 6 runs on this host without the series too; the stalled element delays the load event, and before, only a garbage collection that freed it let the load event fire (upstream's intermittent tracker has 9 unexpected results for that test in total). The three tests that fail on QuickJS CI pass with forced GCs every 20 ms too. With logging of the keep-alive updates, every element in direct checks stopped being kept alive once it had been loaded and never played, played to the end, paused, replayed after the end without a seekable range, or played backwards to the start. An independent agent review found no blockers; its two should-fix findings for this patch (ended but unpaused elements were kept alive; changes of the error went through no hook) are fixed. QuickJS release build of this stack: the new tests and the media tests that had QuickJS expectations (autoplay.html, allowed-to-play.html, resource-selection-candidate-moved.html and the three autoplay permissions policy tests) give SpiderMonkey's results in 2 of 2 runs; without the series they time out as the dropped expectations said. 2026-10-29 sync, reworked onto upstream's keep-alive: release builds of this sync's stack and of plain upstream ed6a924, `./mach test-wpt --release` of the media-elements, the-video-element and the-audio-element directories with fetch, xhr, eventsource, cors, subresource-integrity, resource-timing, canvas image drawing and _mozilla/memory (2,247 tests), then 3 reruns on both of every test whose results differed: the same results on both except for intermittent ones, gc-unreferenced-media-element and upstream's playing_media_no_gc pass; a playing element's back/forward cache restore fails on both (upstream servo#48979 pauses it at unload). DAYU200 (OpenHarmony 7.0), headless release builds with SpiderMonkey and with QuickJS, `./mach test-wpt --ohos --processes 1 --timeout-multiplier 2` of the media series' tests, loading-the-media-resource, error-codes and playing-the-media-resource (75 tests): all as expected but the two autoplay permissions policy tests that pass where a timeout is expected, on both engines; no crash.
AI-assisted: Yes (agent session; root-caused, written and tested by the agent)
AI-assisted: Yes (agent session; 2026-10-22 sync -- mechanical conflict resolution: the `HTMLMediaElement` import next to upstream's `TracedScriptResult` (servo#48855), and the patch's +8 bytes of `HTMLVideoElement` applied to upstream's new sizes (servo#48082))
AI-assisted: Yes (agent session; 2026-10-29 sync -- upstream servo#48979 and servo#49367 landed a keep-alive set of their own: the patch now extends upstream's `should_keep_alive` with its rules (load event delay, seeking, resource and HAVE_ENOUGH_DATA conditions) and keeps its setter hooks; its own set, flag and duplicate hooks are gone, as is its +8 bytes of `HTMLVideoElement`; subject and message rewritten)
AI-assisted: Yes (agent session; 2026-11-09 sync -- upstream's WPT sync (servo#49485) now expects what this patch made autoplay-disabled-by-permissions-policy.https.sub.html do (FAIL instead of TIMEOUT), so of this patch's hunk only the removal of the QuickJS conditions remains)
Signed-off-by: Jonathan Schwender <[email protected]>
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.

media elements that are played to the end and have no other references should be GCed

3 participants