Skip to content

Commit 7a51c2b

Browse files
committed
Avoid 3s PerformanceObserver fallback stalls when aborted script fetches are cancelled
1 parent c99e5a0 commit 7a51c2b

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

‎domparsing/stream-html-parser-blocking-script.html‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,29 @@
130130
}
131131

132132
function waitForResource(url) {
133-
const name = new URL(url, location.href).href;
133+
const parsed = new URL(url, location.href);
134+
const name = parsed.href;
135+
// Implementations may cancel an in-flight script fetch on parser abort, in
136+
// which case no PerformanceResourceTiming entry is emitted. Race against a
137+
// slightly slower un-aborted control request so cancelled fetches settle
138+
// promptly without waiting for the 3s fallback timeout.
139+
const controlUrl = new URL(parsed);
140+
controlUrl.search = controlUrl.search.replace("trickle(d0.2)", "trickle(d0.3)");
141+
controlUrl.searchParams.set("control_probe", Math.random());
142+
const controlDone = fetch(controlUrl).then(r => r.text(), () => {});
134143
let observer;
135-
return withTimeout(new Promise(resolve => {
136-
observer = new PerformanceObserver(list => {
137-
if (list.getEntries().some(e => e.name === name)) {
138-
observer.disconnect();
139-
resolve();
140-
}
141-
});
142-
observer.observe({ type: "resource", buffered: true });
143-
})).finally(() => observer?.disconnect());
144+
return withTimeout(Promise.race([
145+
controlDone,
146+
new Promise(resolve => {
147+
observer = new PerformanceObserver(list => {
148+
if (list.getEntries().some(e => e.name === name)) {
149+
observer.disconnect();
150+
resolve();
151+
}
152+
});
153+
observer.observe({ type: "resource", buffered: true });
154+
}),
155+
])).finally(() => observer?.disconnect());
144156
}
145157

146158
promise_test(async (t) => {
@@ -567,6 +579,7 @@
567579
t.add_cleanup(() => { delete window.iframe_blocked_ran; });
568580

569581
const iframe = document.createElement("iframe");
582+
t.add_cleanup(() => iframe.remove());
570583
iframe.srcdoc = "<!doctype html><body><div id='target'></div></body>";
571584
await new Promise(resolve => {
572585
iframe.addEventListener("load", resolve, { once: true });
@@ -576,6 +589,9 @@
576589
iframe.contentWindow.notifyParent = () => {
577590
window.iframe_blocked_ran = true;
578591
};
592+
const controlDone = fetch("resources/slow.js?pipe=trickle(d0.3)&f12_control=1")
593+
.then(r => r.text())
594+
.then(() => new Promise(r => t.step_timeout(r, 0)));
579595
const target = iframe.contentDocument.getElementById("target");
580596
const writer = target.streamHTMLUnsafe({ runScripts: true }).getWriter();
581597
await writer.write(
@@ -584,7 +600,7 @@
584600
);
585601

586602
iframe.remove();
587-
await new Promise(r => t.step_timeout(r, 350));
603+
await controlDone;
588604
assert_false(window.iframe_blocked_ran, "Scripts in removed iframe must not execute");
589605
}, "Removing iframe while stream is blocked on external script does not crash or execute scripts");
590606

0 commit comments

Comments
 (0)