Skip to content

Window load events fire too quickly for auxiliary browsing contexts #43149

Closed

Description

@jdm

Some test WPT page:

<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
  function waitForWindowOpen(url, t) {
    const w = window.open(url);
    t.add_cleanup(() => w.close());
    w.addEventListener("load", () => { console.log("window load event"); t.done(); }, { once: true });
  }

async_test(t => {
    console.log("opening window");
  waitForWindowOpen("iframe.html", t);
</script>

iframe.html:

<!DOCTYPE html>
<iframe src="/common/blank.html?pipe=trickle(d3)"></iframe>

Loading this test in Firefox will wait for 3 seconds before the test completes. In Servo this completes instantly. I believe this is caused by the framed document's initial about:blank triggering the conditions to dispatch the load event.

Activity

  1. added
    A-content/script
    on Mar 10, 2026
  2. jdm commented on Mar 10, 2026

    @jdm
    MemberAuthor

    This does not happen when this page is loaded in an iframe instead of using window.open. I actually think this is triggered by the initial about:blank that is loaded in the new auxiliary browsing context before the actual loaded URL.

  3. jdm commented on Mar 10, 2026

    @jdm
    MemberAuthor

    Even easier to see:

    <!DOCTYPE html>
    <meta charset="utf-8">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script>
    "use strict";
      
    async_test(t => {
        console.log("opening window");
        const w = window.open("/common/blank.html?pipe=trickle(d3)");
        t.add_cleanup(() => w.close());
        w.addEventListener("load", () => { console.log("window load event"); t.done(); }, { once: true });
    });
    </script>

    No need for any nested browsing contexts.

  4. changed the title [-]Window load events fire too quickly for pages containing iframes[/-] [+]Window load events fire too quickly for auxiliary browsing contexts[/+] on Mar 10, 2026
  5. jdm commented on Mar 10, 2026

    @jdm
    MemberAuthor

    I think one possible solution here is implementing document replacement: https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-document-window . I'm having a hard time finding the actual spec step where that replacement is supposed to occur.

  6. TimvdLippe commented on Mar 10, 2026

    @TimvdLippe
    Contributor

    With the following diff:

    diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs
    index 79d8d054485..adfa43217dd 100644
    --- a/components/script/dom/document.rs
    +++ b/components/script/dom/document.rs
    @@ -2449,6 +2449,8 @@ impl Document {
                 return;
             }
     
    +        println!("Starting load for {}: {}", self.url(), std::backtrace::Backtrace::force_capture());
    +
             self.loader.borrow_mut().inhibit_events();
     
             // The rest will ever run only once per document.

    Output:

     0:03.90 pid:9869 Starting load for about:blank:    0: script::dom::document::Document::maybe_queue_document_completion
     0:03.90 pid:9869              at ./components/script/dom/document.rs:2452:58
     0:03.90 pid:9869    1: script::script_thread::ScriptThread::handle_msgs
     0:03.90 pid:9869              at ./components/script/script_thread.rs:1521:26
     0:03.90 pid:9869    2: script::script_thread::ScriptThread::start
     0:03.90 pid:9869              at ./components/script/script_thread.rs:1081:20
    

    That's this code:

    {
    // https://html.spec.whatwg.org/multipage/#the-end step 6
    let mut docs = self.docs_with_no_blocking_loads.borrow_mut();
    for document in docs.iter() {
    let _realm = enter_auto_realm(cx, &**document);
    document.maybe_queue_document_completion();
    }
    docs.clear();
    }

    Note that we never log /common/blank.html?pipe=trickle(d3) as a URL. The URLs we log (in order):

    1. about:blank
    2. http://web-platform.test:8000/testharness_runner.html
    3. about:blank
    4. http://web-platform.test:8000/html/test.html
    5. about:blank
  7. TimvdLippe commented on Mar 10, 2026

    @TimvdLippe
    Contributor

    #42446 was supposed to fix that quirk, but had other issues with flakiness and such 😢

  8. TimvdLippe commented on Mar 10, 2026

    @TimvdLippe
    Contributor

    On https://github.com/TimvdLippe/servo/tree/reintroduce-end-of-loading-phases with

    diff --git a/components/script/dom/servoparser/mod.rs b/components/script/dom/servoparser/mod.rs
    index be369d1d67e..54b07d09f04 100644
    --- a/components/script/dom/servoparser/mod.rs
    +++ b/components/script/dom/servoparser/mod.rs
    @@ -763,6 +763,7 @@ impl ServoParser {
             self.document.set_current_parser(None);
     
             // Step 5. While the list of scripts that will execute when the document has finished parsing is not empty:
    +        println!("Finished parsing for {}: {}", self.document.url(), std::backtrace::Backtrace::force_capture());
             self.document.start_the_end_loading_phase();
             let url = self.tokenizer.url().clone();
             self.document.finish_load(LoadType::PageSource(url), cx);

    I see the following:

     0:03.36 pid:42748 opening window
     0:03.42 pid:42748 Finished parsing for about:blank:    0: script::dom::servoparser::ServoParser::finish
     0:03.42 pid:42748              at ./components/script/dom/servoparser/mod.rs:766:70
     0:03.42 pid:42748    1: script::dom::servoparser::ServoParser::parse_sync
     0:03.42 pid:42748              at ./components/script/dom/servoparser/mod.rs:676:18
     0:03.42 pid:42748    2: <script::dom::servoparser::ParserContext as script::network_listener::FetchResponseListener>::process_response_eof
     0:03.42 pid:42748              at ./components/script/dom/servoparser/mod.rs:1459:20
     0:03.42 pid:42748    3: script::script_thread::ScriptThread::start_page_load_about_blank
     0:03.42 pid:42748              at ./components/script/script_thread.rs:4025:17
     0:03.42 pid:42748    4: script::script_thread::ScriptThread::pre_page_load
     0:03.43 pid:42748              at ./components/script/script_thread.rs:3793:18
     0:03.43 pid:42748    5: script::script_thread::ScriptThread::spawn_pipeline::{{closure}}
     0:03.43 pid:42748              at ./components/script/script_thread.rs:2757:22
     0:03.43 pid:42748    6: script::script_thread::ScriptThread::profile_event
     0:03.43 pid:42748              at ./components/script/script_thread.rs:1692:13
     0:03.43 pid:42748    7: script::script_thread::ScriptThread::spawn_pipeline
     0:03.43 pid:42748              at ./components/script/script_thread.rs:2726:14
     0:03.43 pid:42748    8: script::dom::windowproxy::WindowProxy::create_auxiliary_browsing_context::{{closure}}
     0:03.43 pid:42748              at ./components/script/dom/windowproxy.rs:358:27
     0:03.43 pid:42748    9: core::option::Option<T>::map
     0:03.43 pid:42748              at /home/tvanderlippe/.rustup/toolchains/1.92.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1164:29
     0:03.43 pid:42748   10: script::script_thread::with_script_thread::{{closure}}
     0:03.43 pid:42748              at ./components/script/script_thread.rs:180:63
     0:03.43 pid:42748   11: script::script_thread::with_optional_script_thread::{{closure}}
     0:03.43 pid:42748              at ./components/script/script_thread.rs:173:9
     0:03.43 pid:42748   12: std::thread::local::LocalKey<T>::try_with
     0:03.43 pid:42748              at /home/tvanderlippe/.rustup/toolchains/1.92.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:508:12
     0:03.43 pid:42748   13: std::thread::local::LocalKey<T>::with
     0:03.43 pid:42748              at /home/tvanderlippe/.rustup/toolchains/1.92.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:472:20
     0:03.43 pid:42748   14: script::script_thread::with_optional_script_thread
     0:03.43 pid:42748              at ./components/script/script_thread.rs:172:24
     0:03.43 pid:42748   15: script::script_thread::with_script_thread
     0:03.43 pid:42748              at ./components/script/script_thread.rs:180:5
     0:03.43 pid:42748   16: script::dom::windowproxy::WindowProxy::create_auxiliary_browsing_context
     0:03.43 pid:42748              at ./components/script/dom/windowproxy.rs:357:9
     0:03.43 pid:42748   17: script::dom::windowproxy::WindowProxy::choose_browsing_context
     0:03.43 pid:42748              at ./components/script/dom/windowproxy.rs:619:31
     0:03.43 pid:42748   18: script::dom::windowproxy::WindowProxy::open
     0:03.43 pid:42748              at ./components/script/dom/windowproxy.rs:511:40
    
  9. TimvdLippe commented on Mar 10, 2026

    @TimvdLippe
    Contributor

    So because in start_page_load_about_blank we pretend to have a fetch request, the parser "encounters" the EOF character and we inadvertently trigger the loading sequence (https://html.spec.whatwg.org/multipage/parsing.html#the-end). Ideally we would know that when creating the auxiliary context, we are going to immediately do something with it or not. In the second case, we shouldn't fire.

    To me, that seems similar to the logic in iframe_load_event_steps where we check the src attribute if it exists. To unify these two, should we add a bool to LoadData that essentially says "I am the first one, but a second one is coming, so don't think I am finished loading yet"? Then we can use that for both iframes as well as auxiliary windows.

  10. jdm commented on Mar 10, 2026

    @jdm
    MemberAuthor

    I'm nervous about adding that information to LoadData, but I'll need to think about why. My idea with document replacement is that right now, the only thing linking the about:blank document/window with the subsequent navigation is a browsing context id. If we implement document replacement (see relevant commits from #33087) then it's easier to check "is this document the associated document of the window" and ignore tasks for the old document.

  11. jdm commented on Mar 10, 2026

    @jdm
    MemberAuthor

    With the following diff:

    diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs
    index 79d8d054485..adfa43217dd 100644
    --- a/components/script/dom/document.rs
    +++ b/components/script/dom/document.rs
    @@ -2449,6 +2449,8 @@ impl Document {
    return;
    }

    •    println!("Starting load for {}: {}", self.url(), std::backtrace::Backtrace::force_capture());
      
    •    self.loader.borrow_mut().inhibit_events();
      
         // The rest will ever run only once per document.
      

    Output:

     0:03.90 pid:9869 Starting load for about:blank:    0: script::dom::document::Document::maybe_queue_document_completion
     0:03.90 pid:9869              at ./components/script/dom/document.rs:2452:58
     0:03.90 pid:9869    1: script::script_thread::ScriptThread::handle_msgs
     0:03.90 pid:9869              at ./components/script/script_thread.rs:1521:26
     0:03.90 pid:9869    2: script::script_thread::ScriptThread::start
     0:03.90 pid:9869              at ./components/script/script_thread.rs:1081:20
    

    That's this code:

    servo/components/script/script_thread.rs

    Lines 1516 to 1524 in 4ef22ed
    {
    // https://html.spec.whatwg.org/multipage/#the-end step 6
    let mut docs = self.docs_with_no_blocking_loads.borrow_mut();
    for document in docs.iter() {
    let _realm = enter_auto_realm(cx, &**document);
    document.maybe_queue_document_completion();
    }
    docs.clear();
    }

    Note that we never log /common/blank.html?pipe=trickle(d3) as a URL. The URLs we log (in order):

    1. about:blank
    
    2. http://web-platform.test:8000/testharness_runner.html
    
    3. about:blank
    
    4. http://web-platform.test:8000/html/test.html
    
    5. about:blank
    

    This result doesn't surprise me, because the test calls w.close() after receiving the load event, so the navigation never has a chance to complete.

  12. jdm commented on Mar 11, 2026

    @jdm
    MemberAuthor
  13. TimvdLippe commented on Mar 11, 2026

    @TimvdLippe
    Contributor

    We already have a method for that in the servoparser, but it's mostly empty still. The one tricky bit is that if we create the document value last, we can't eagerly add nodes to it. So it complicates the parser further. Still, I think that's worth it.

  14. jdm commented on Mar 14, 2026

    @jdm
    MemberAuthor
  15. 16 remaining items

  16. jdm commented on Aug 6, 2026

    @jdm
    MemberAuthor

    Given the ongoing issues with intermittent test results for #46975, I'm trying a detour into making about:blank really synchronous: https://github.com/servo/servo/compare/main...jdm:servo:sync-about-blank?expand=1

  17. jdm commented on Aug 21, 2026

    @jdm
    MemberAuthor

    This was fixed by the combination of the various PRs that mentioned this issue. Any specific behaviour issues with window.open should be filed as new issues!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/script

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions