Skip to content

script, media: Close audio ctx & media elements on doc unload and tab close - #49504

Open
sabbCodes wants to merge 2 commits into
servo:mainfrom
sabbCodes:issue-49450
Open

sabbCodes wants to merge 2 commits into
servo:mainfrom
sabbCodes:issue-49450

Conversation

@sabbCodes

Copy link
Copy Markdown
Contributor

Transitioned the GStreamer audio sink pipeline to State::Null on drop to terminate GStreamer background streaming threads. Allowed tracking of active BaseAudioContext instances on Document and close them during unloading document cleanup steps.

Now cleans up active media elements and audio contexts when a pipeline exits in ScriptThread::handle_exit_pipeline_msg.
Added a test to ensure AudioContext is closed on document unload and that it does not leak the document.

Testing: ./mach test-wpt tests/wpt/mozilla/tests/memory/audiocontext_closed_on_unload.html passed cleanly w/

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

Fixes: #49450

@sabbCodes
sabbCodes requested a review from gterzian as a code owner November 1, 2026 17:44
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 1, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

For some reasons, I couldn't run a manual test case as described by @webbedspace in the issue description.

Got this error;

* Tone.js r7 * 
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value

I'm running on Ubuntu 24.04 LTS
Perhaps someone could help try it on their end.


/// <https://webaudio.github.io/web-audio-api/#unloading-a-document>
/// Audio contexts that need to be closed when the document unloads.
audio_contexts: DomRefCell<FxHashSet<Dom<BaseAudioContext>>>,

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.

I would expect to see spec text steps in this implementation ?

2.9. Unloading a document
Additional unloading document cleanup steps are defined for documents that use BaseAudioContext:
Reject all the promises of [[pending promises]] with InvalidStateError, for each AudioContext and OfflineAudioContext whose relevant global object is the same as the document’s associated Window.

Stop all decoding threads.

Queue a control message to close() the AudioContext or OfflineAudioContext.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks ser

Will add them spec steps now.

@servo-highfive servo-highfive added the S-needs-rebase There are merge conflict errors. label Nov 2, 2026
@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Nov 2, 2026
@sabbCodes
sabbCodes force-pushed the issue-49450 branch 2 times, most recently from f478ca0 to deaa8d0 Compare November 2, 2026 21:47
@webbedspace

Copy link
Copy Markdown

For some reasons, I couldn't run a manual test case as described by @webbedspace in the issue description.

Got this error;

* Tone.js r7 * 
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value

I'm running on Ubuntu 24.04 LTS Perhaps someone could help try it on their end.

This error is mostly irrelevant. The given page should produce audio regardless of this.

@sabbCodes

Copy link
Copy Markdown
Contributor Author

For some reasons, I couldn't run a manual test case as described by @webbedspace in the issue description.
Got this error;

* Tone.js r7 * 
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:7256 this.context.createWaveShaper is not a function
Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value
[2026-11-01T17:28:38Z ERROR servoshell::desktop::headed_window] Error at https://dryad.technology/lib/tone-0.7.1/Tone.min.js:7:20833 this argument is not a finite floating-point value

I'm running on Ubuntu 24.04 LTS Perhaps someone could help try it on their end.

This error is mostly irrelevant. The given page should produce audio regardless of this.

I'm not sure why it wasn't producing the sound.

I even tried a custom test file with sound.
Maybe it's a local thing, wondering if you could help try on your end?

@sabbCodes

Copy link
Copy Markdown
Contributor Author

So I've been able to detect why this wasn't working on my machine: the sounds was not playing because this.context.createWaveShaper is not a function stopped Tone.js from building the audio graph.

So I tried writing a new file to test this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Servo WebAudio Tab Close Test</title>
</head>
<body>
  <h1>WebAudio Tone Test</h1>
  <button id="start">Start Continuous Tone</button>

  <script>
    window.audioCtx = null;
    window.osc = null;

    function playContinuousTone() {
      if (!window.audioCtx) {
        window.audioCtx = new AudioContext();
      }
      if (window.audioCtx.state === 'suspended') {
        window.audioCtx.resume();
      }

      if (window.osc) {
        try { window.osc.stop(); } catch(e) {}
      }

      window.osc = window.audioCtx.createOscillator();
      window.osc.type = "sine";
      window.osc.frequency.setValueAtTime(440, window.audioCtx.currentTime);

      window.osc.connect(window.audioCtx.destination);
      window.osc.start();

    document.getElementById("start").addEventListener("click", playContinuousTone);

    // Auto-play on load
    try {
      playContinuousTone();
    } catch(e) {
      console.log("Autoplay waiting for user gesture:", e);
    }
  </script>
</body>
</html>

And it's working - closing the tab stops the sound comlpletly.

Comment on lines +3309 to +3310
document.clean_up_media_elements(cx.no_gc());
document.clean_up_audio_contexts(cx);

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.

I think this needs to be after

self.clean_up_media_elements(cx.no_gc());
where we already do this. It would be surprising to me if we don't call these steps when we exit a pipeline.

I think the call stack should be:

  1. https://html.spec.whatwg.org/multipage/speculative-loading.html#nav-traversal-ui:close-a-top-level-traversable
  2. https://html.spec.whatwg.org/multipage/document-sequences.html#garbage-collection-and-browsing-contexts:definitely-close-a-top-level-traversable
  3. https://html.spec.whatwg.org/multipage/document-sequences.html#garbage-collection-and-browsing-contexts:unload-a-document-and-its-descendants

Basically instead of the code you have here, we should call document.close which would then call document.definitely_close. That said, I don't actually know how much would break if we were to do that.

Let me make a separate PR to make these changes and see what the damage is.

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.

#50097 but it doesn't work, so I asked for guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebAudio in Servoshell tab continues after you close the tab

5 participants