test(xhr): gate timeout-before-upload controls with preflight

The first-report timeout controls assumed that an 80ms busy task would
finish before the network service's 100ms reporting interval. Scheduler
preemption can invalidate that assumption; fixed Chromium 152 also emits
upload completion before the delayed XHR timeout when that happens.

Hold a CORS preflight on a second loopback origin for the two controls
that require no upload report. No body can be transmitted while the
preflight is pending, regardless of task overrun. Preserve every event
assertion and all 16 cases in both Window and Worker, including the normal
network's positive completion/reporting cases. Abort both owned servers
when the CLI run ends.

Independent normal and 160ms-overrun controls match Chromium and the
published-parent debug/release binaries on all 24 cases; each profile
observes six held preflights and zero POST requests to the held endpoint.
This commit is contained in:
ldm0
2026-11-22 02:02:06 +08:00
parent 46f3c20aa5
commit 9480b85751
2 changed files with 32 additions and 11 deletions
+20 -4
View File
@@ -5,7 +5,7 @@ use axum::{
body::{Body, Bytes},
extract::Query,
response::Html,
routing::{get, post},
routing::{get, options, post},
};
use std::{collections::HashMap, convert::Infallible, time::Duration};
@@ -68,15 +68,24 @@ async fn upload_response(Query(delays): Query<HashMap<String, u64>>, body: Bytes
}))
}
async fn pending_upload_preflight() -> Body {
std::future::pending().await
}
#[test]
fn xhr_upload_notifications_preserve_timeout_order_in_window_and_worker() -> Result<()> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(32)
.enable_all()
.build()?;
let (url, server) = runtime.block_on(async {
let (url, server, pending_server) = runtime.block_on(async {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let url = format!("http://{}/page", listener.local_addr()?);
let pending_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let pending_origin = format!("http://{}", pending_listener.local_addr()?);
let url = format!(
"http://{}/page?pendingOrigin={pending_origin}",
listener.local_addr()?
);
let app = Router::new()
.route(
"/page",
@@ -84,7 +93,12 @@ fn xhr_upload_notifications_preserve_timeout_order_in_window_and_worker() -> Res
)
.route("/upload", post(upload_response));
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
Ok::<_, anyhow::Error>((url, server))
// Hold the cross-origin preflight so these requests cannot upload,
// even if the script owner's busy task exceeds the reporting interval.
let pending_app = Router::new().route("/upload", options(pending_upload_preflight));
let pending_server =
tokio::spawn(async move { axum::serve(pending_listener, pending_app).await.unwrap() });
Ok::<_, anyhow::Error>((url, server, pending_server))
})?;
let result = run_fetch_cli_with_args(
&url,
@@ -96,7 +110,9 @@ fn xhr_upload_notifications_preserve_timeout_order_in_window_and_worker() -> Res
],
);
server.abort();
pending_server.abort();
assert!(runtime.block_on(server).unwrap_err().is_cancelled());
assert!(runtime.block_on(pending_server).unwrap_err().is_cancelled());
let output = result?;
let stdout = clean_output(&output.stdout);
assert!(
+12 -7
View File
@@ -1,10 +1,10 @@
<!doctype html>
<body>
<script>
async function probeUploads(scope, base) {
async function probeUploads(scope, base, pendingOrigin) {
const cases = [
{name: 'timeout before first report', timeout: 30, head: 500, expected: 'timeout'},
{name: 'busy before first report', timeout: 50, head: 500, busy: 80, expected: 'timeout'},
{name: 'timeout before first report', timeout: 30, head: 500, pending: true, expected: 'timeout'},
{name: 'busy before first report', timeout: 50, head: 500, busy: 80, pending: true, expected: 'timeout'},
{name: 'queued report after deadline while busy', timeout: 30, head: 500, busy: 150, complete: true, expected: 'timeout'},
{name: 'periodic completion before timeout', timeout: 300, head: 800, complete: true, expected: 'timeout'},
{name: 'queued completion before elapsed timeout', timeout: 300, head: 650, busy: 450, complete: true, expected: 'timeout'},
@@ -69,7 +69,10 @@ async function probeUploads(scope, base) {
resolve();
} catch (error) { reject(error); }
});
xhr.open('POST', base + '/upload?head=' + config.head + '&body=' + (config.body || 0));
// A pending preflight makes the absence of upload reports independent
// of how long a nominally short busy task takes under parallel load.
const target = config.pending ? pendingOrigin : base;
xhr.open('POST', target + '/upload?head=' + config.head + '&body=' + (config.body || 0));
xhr.timeout = config.timeout;
xhr.send(bytes === null ? null : bytes === 0 ? '' : 'Test Message');
const until = performance.now() + (config.busy || 0);
@@ -108,16 +111,18 @@ async function probeUploads(scope, base) {
}
(async () => {
const base = location.origin;
const windowResult = await probeUploads('window', base);
const pendingOrigin = new URL(location.href).searchParams.get('pendingOrigin');
if (!pendingOrigin) throw new Error('pending preflight origin missing');
const windowResult = await probeUploads('window', base, pendingOrigin);
const worker = new Worker(URL.createObjectURL(new Blob([
probeUploads.toString(),
'\nonmessage = async event => { try { postMessage(await probeUploads("worker", event.data)); } catch (error) { postMessage({error: String(error)}); } };'
'\nonmessage = async event => { try { postMessage(await probeUploads("worker", event.data.base, event.data.pendingOrigin)); } catch (error) { postMessage({error: String(error)}); } };'
], {type: 'text/javascript'})));
try {
const workerResult = await new Promise((resolve, reject) => {
worker.onmessage = event => event.data.error ? reject(new Error(event.data.error)) : resolve(event.data);
worker.onerror = event => reject(new Error(event.message));
worker.postMessage(base);
worker.postMessage({base, pendingOrigin});
});
globalThis.uploadTimeoutFacts = {window: windowResult.facts, worker: workerResult.facts};
document.body.dataset.uploadTimeouts = windowResult.result + '|' + workerResult.result;