Skip to content

Truly independent web browser

LibWeb: Stop an aborted or terminated fetch’s network request

sideshowbarkerf0631b65cacb
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
index f57e0f643d6..90302c67923 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
@@ -118,6 +118,8 @@ void FetchController::abort(JS::Realm& realm, Optional<JS::Value> error)
: serialized_value_or_error.value();
};
m_serialized_abort_reason = structured_serialize(error.value(), fallback_error_value);
+
+ stop_request();
}
// https://fetch.spec.whatwg.org/#fetch-controller-terminate
@@ -125,6 +127,8 @@ void FetchController::terminate()
{
// To terminate a fetch controller controller, set controller’s state to "terminated".
m_state = State::Terminated;
+
+ stop_request();
}
void FetchController::stop_fetch()
@@ -159,9 +163,15 @@ void FetchController::stop_fetch()
}
}
+// Stops the network request of a fetch that is no longer ongoing. HTTP-network fetch reads a response's body in steps
+// that "abort when fetchParams is canceled" (step 18.1) — and once aborted, "the user agent should close connection
+// unless it would be bad for performance to do so" (step 18.2.4). So a canceled fetch's request has to stop as well as
+// a stopped one's: otherwise it streams on until the server ends the response, and its callbacks root the fetch's realm
+// for all that time.
+// https://fetch.spec.whatwg.org/#concept-http-network-fetch
void FetchController::stop_request()
{
- VERIFY(m_state == State::Stopped);
+ VERIFY(m_state != State::Ongoing);
if (m_pending_request) {
m_pending_request->stop();
m_pending_request = nullptr;
diff --git a/Tests/LibWeb/Text/expected/Fetch/document-with-aborted-fetches-is-collectable.txt b/Tests/LibWeb/Text/expected/Fetch/document-with-aborted-fetches-is-collectable.txt
new file mode 100644
index 00000000000..8d36401c8ca
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/document-with-aborted-fetches-is-collectable.txt
@@ -0,0 +1 @@
+collected 5 of 5 documents with aborted fetches
diff --git a/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html
new file mode 100644
index 00000000000..aa4e39d8a35
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html
@@ -0,0 +1,100 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Aborting a fetch through its AbortController or through XMLHttpRequest.abort(), and terminating one through
+ // XMLHttpRequest.open(), have to stop its network request. A request that streams on until the server ends the
+ // response keeps its callbacks, and those root the fetch's realm. And aborting the document as it unloads stops
+ // only the fetches that are still ongoing — so a page that aborts its long-lived requests and starts new ones (as a
+ // page that polls its server does) would otherwise stay alive after a navigation away from it, for as long as the
+ // server kept the aborted responses open.
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const token = `document-with-aborted-fetches-${crypto.randomUUID()}`;
+
+ // The response's first byte arrives at once, and the rest only once the token is unblocked — which never
+ // happens.
+ const blockedURL = await server.createEcho("GET", "/document-with-aborted-fetches-blocked", {
+ status: 200,
+ headers: { "Content-Type": "text/plain", "Cache-Control": "no-store" },
+ body: "xx",
+ });
+
+ // The page starts two XMLHttpRequests and a fetch of that response. Once all three have their headers, it
+ // aborts the fetch and one XMLHttpRequest, opens the other XMLHttpRequest again (which terminates its fetch),
+ // and tells its parent.
+ const pageURL = await server.createEcho("GET", "/document-with-aborted-fetches", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html>
+<script>
+ const url = ${JSON.stringify(blockedURL)} + "?chunks=1&chunk_unblock_tokens=${token}&request=";
+ const headersReceived = xhr =>
+ new Promise(resolve => {
+ xhr.onreadystatechange = () => {
+ if (xhr.readyState >= XMLHttpRequest.HEADERS_RECEIVED)
+ resolve();
+ };
+ });
+ const aborter = new AbortController();
+ const abortedXHR = new XMLHttpRequest();
+ const reopenedXHR = new XMLHttpRequest();
+ const started = [
+ fetch(url + "fetch", { signal: aborter.signal }),
+ headersReceived(abortedXHR),
+ headersReceived(reopenedXHR),
+ ];
+ abortedXHR.open("GET", url + "aborted-xhr");
+ abortedXHR.send();
+ reopenedXHR.open("GET", url + "reopened-xhr");
+ reopenedXHR.send();
+ Promise.all(started).then(() => {
+ aborter.abort();
+ abortedXHR.abort();
+ reopenedXHR.onreadystatechange = null;
+ reopenedXHR.open("GET", url + "never-sent");
+ parent.postMessage("requests canceled", "*");
+ });
+<\/script>`,
+ });
+
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+
+ const navigate = url =>
+ new Promise(resolve => {
+ window.addEventListener("message", resolve, { once: true });
+ iframe.src = url;
+ });
+
+ // Record each document, then navigate away from it. An expando preserves the document's wrapper for as long as
+ // the document itself lives, so the weak reference tracks the document rather than a wrapper that JS may drop
+ // early. The recording starts with the second document: the first one reuses the initial about:blank
+ // document's window, whose realm the navigable's WindowProxy keeps alive for as long as the iframe lives. And
+ // the recording runs in a nested function, so that no document lingers in a register of this function's frame
+ // while the documents are expected to be collected.
+ const weakDocuments = await (async () => {
+ const weakDocuments = [];
+ await navigate(`${pageURL}?0`);
+ await navigate(`${pageURL}?1`);
+ for (let i = 2; i <= 6; i++) {
+ const unloadedDocument = iframe.contentDocument;
+ unloadedDocument.preserveWrapper = true;
+ weakDocuments.push(new WeakRef(unloadedDocument));
+ await navigate(`${pageURL}?${i}`);
+ }
+ return weakDocuments;
+ })();
+
+ const maximumGcRoundsWhileRequestTeardownTasksDrain = 100;
+ for (let round = 0; round < maximumGcRoundsWhileRequestTeardownTasksDrain; ++round) {
+ await timeout(0);
+ await internals.gcAsync();
+ if (weakDocuments.every(weak => weak.deref() === undefined))
+ break;
+ }
+
+ const collected = weakDocuments.filter(weak => weak.deref() === undefined).length;
+ println(`collected ${collected} of ${weakDocuments.length} documents with aborted fetches`);
+ iframe.remove();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html.headers b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/document-with-aborted-fetches-is-collectable.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html