Skip to content

Truly independent web browser

LibWeb: Leave keepalive fetches running when aborting a document

Shannon Booth4dbfb5ad2f8c
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 5f960afb737..d96aebbf73b 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -7000,8 +7000,14 @@ void Document::abort()
// AD-HOC: The fetch group belongs to the document's Window. Once the Window is associated with another document,
// as when the initial about:blank document is replaced, its fetches are not in the context of this document.
if (auto window = this->window(); window && window->associated_document_if_any().ptr() == this) {
- for (auto& fetch_record : relevant_settings_object().fetch_group())
- cancel_fetch(fetch_record.fetch_controller());
+ for (auto& fetch_record : relevant_settings_object().fetch_group()) {
+ auto controller = fetch_record.fetch_controller();
+ // AD-HOC: A keepalive fetch outlives the document, as terminating a fetch group leaves its keepalive
+ // fetches running.
+ if (controller && controller->is_keepalive())
+ continue;
+ cancel_fetch(controller);
+ }
for (auto& controller : HTML::relevant_agent(*this).fetch_controller_registry.controllers()) {
if (!controller->task_destination().is_for_document(*this))
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 359de172a77..e9e9efd0267 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -3,6 +3,7 @@ Layout/input/pdf-viewer.pdf
Text/input/PDF/jbig2-image.html
[LoadFromHttpServer]
+Text/input/HTML/unload-beacon-outlives-its-document.html
Text/input/wpt-import/console/console-log-large-array.any.worker.html
; A data: iframe stays in this process only under an http: page.
Text/input/HTML/cross-origin-rejected-promise-realm.html
diff --git a/Tests/LibWeb/Text/expected/HTML/unload-beacon-outlives-its-document.txt b/Tests/LibWeb/Text/expected/HTML/unload-beacon-outlives-its-document.txt
new file mode 100644
index 00000000000..b1d1fe7ecc6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/unload-beacon-outlives-its-document.txt
@@ -0,0 +1,3 @@
+Window replaced: true
+Beacon queued: true
+Beacon followed its redirect
diff --git a/Tests/LibWeb/Text/input/HTML/unload-beacon-outlives-its-document.html b/Tests/LibWeb/Text/input/HTML/unload-beacon-outlives-its-document.html
new file mode 100644
index 00000000000..0236022488b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/unload-beacon-outlives-its-document.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A beacon a document sends as it unloads follows its redirect to the end, even though navigating away aborts the
+ // document and gives the navigable a new Window.
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const token = `unload-beacon-${crypto.randomUUID()}`;
+ const destinationURL = await server.createEcho("POST", `/${token}/destination`, {
+ status: 200,
+ body: "",
+ });
+ const beaconURL = await server.createEcho("POST", `/${token}/beacon`, {
+ status: 307,
+ headers: { Location: destinationURL },
+ body: "",
+ wait_for_unblock: token,
+ });
+
+ const iframe = document.createElement("iframe");
+ iframe.src = "../../../Assets/blank.html";
+ document.body.append(iframe);
+ await new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+
+ const frameWindow = iframe.contentWindow;
+ frameWindow.marker = true;
+ let beaconQueued = false;
+ frameWindow.addEventListener("unload", () => {
+ beaconQueued = frameWindow.navigator.sendBeacon(beaconURL, "unload");
+ });
+
+ iframe.src = "../../../Assets/blank.html?navigated";
+ await new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+
+ await server.waitForRequest(beaconURL);
+ await fetch(new URL(`/unblock/${token}`, server.baseURL));
+ await server.waitForRequest(destinationURL);
+
+ println(`Window replaced: ${frameWindow.marker === undefined}`);
+ println(`Beacon queued: ${beaconQueued}`);
+ println("Beacon followed its redirect");
+ done();
+ });
+</script>