From 71f8611a8865ef5852a052d431be9f274fda8f58 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Fri, 30 Oct 2026 23:37:33 +0800 Subject: [PATCH] test(xhr): cover expired upload timers without narrow network races Add a controlled native regression for upload microtasks cancelling or extending an already-ready timeout. Keep the Window and worker HTTP matrix with scheduling margins suitable for parallel execution. Validation: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast. Checks cover the integrated workspace with 32-way concurrency and no automatic retries. --- .../src/script_vm/tests/dom_xhr/xhr.rs | 107 ++++++++++++++++++ moli/tests/fetch_cli/xhr_upload_timeout.html | 9 +- 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs index e10fe393028..22fb766e877 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs @@ -5359,3 +5359,110 @@ xhr.send('payload'); "loadstart:1|abort-ready:4|reopened:1|loadstart:1|abort:1|after-abort:1|load:4:replacement" ); } + +#[test] +fn xhr_upload_microtask_can_replace_an_already_ready_timeout() { + for replacement in [0, 10_000] { + let mut vm = new_storage_test_vm("https://xhr-upload-ready-timeout.test/"); + vm.set_fetch_subresource_interception( + crate::types::SubresourceInterceptionClasses::for_resource_type( + crate::types::SubresourceResourceType::Xhr, + ), + ); + vm.eval(&format!( + r#" +globalThis.xhr = new XMLHttpRequest(); +globalThis.events = []; +for (const type of ['progress', 'load', 'loadend']) {{ + xhr.upload.addEventListener(type, () => events.push(type)); +}} +xhr.upload.addEventListener('loadend', () => Promise.resolve().then(() => {{ + xhr.timeout = {replacement}; + events.push('microtask'); +}})); +xhr.ontimeout = () => events.push('timeout'); +xhr.open('POST', '/held'); +xhr.send('Test Message'); +"# + )) + .expect("intercept upload without depending on network scheduling"); + let pending = vm.take_pending_subresource_fetch_infos(); + assert_eq!(pending.len(), 1); + let internal_id = pending[0].internal_id; + + let context = vm.current_main_world().unwrap().context.clone(); + vm.renderer_document_isolate + .with_entered_renderer_document_isolate(|isolate| { + let scope = std::pin::pin!(v8::HandleScope::new(isolate)); + let scope = &mut scope.init(); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let xhr = context + .global(scope) + .get(scope, v8str(scope, "xhr").into()) + .unwrap(); + let xhr = v8::Local::::try_from(xhr).unwrap(); + // Backdate only the request's native start timestamp. The public + // setter must schedule a genuinely ready timer, without sleeping. + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis() as f64; + crate::network_host::set_xhr_state_number( + scope, + xhr, + crate::network_host::XHR_TIMEOUT_START_MS_SLOT, + now - 100.0, + ); + let timeout = v8::Number::new(scope, 30.0); + assert_eq!( + xhr.set(scope, v8str(scope, "timeout").into(), timeout.into()), + Some(true) + ); + Ok(()) + }) + .expect("schedule expired timeout through the public setter"); + assert!( + vm.has_ready_timeout(), + "the old timeout must already be due" + ); + + let context = vm.current_main_world().unwrap().context.clone(); + vm.renderer_document_isolate + .with_entered_renderer_document_isolate(|isolate| { + let scope = std::pin::pin!(v8::HandleScope::new(isolate)); + let scope = &mut scope.init(); + let context = v8::Local::new(scope, &context); + let scope = &mut v8::ContextScope::new(scope, context); + let xhr = context + .global(scope) + .get(scope, v8str(scope, "xhr").into()) + .unwrap(); + let xhr = v8::Local::::try_from(xhr).unwrap(); + assert!(crate::network_host::apply_xhr_upload_event( + scope, + xhr, + internal_id, + moli_fetch::UploadEvent::Complete { + loaded: 12, + total: 12 + }, + )); + scope.perform_microtask_checkpoint(); + Ok(()) + }) + .expect("apply an admitted upload task and its microtask checkpoint"); + assert!( + !vm.has_ready_timeout(), + "the reaction must retire the ready timer" + ); + assert_eq!(vm.ms_to_next_timeout().is_some(), replacement != 0); + assert_eq!( + vm.eval("JSON.stringify([xhr.timeout, xhr.readyState, events])") + .unwrap(), + format!(r#"[{replacement},1,["progress","load","loadend","microtask"]]"#), + ); + vm.eval("xhr.abort()") + .expect("retire the intercepted request"); + } +} diff --git a/moli/tests/fetch_cli/xhr_upload_timeout.html b/moli/tests/fetch_cli/xhr_upload_timeout.html index 62cfda5cc28..4490ea81410 100644 --- a/moli/tests/fetch_cli/xhr_upload_timeout.html +++ b/moli/tests/fetch_cli/xhr_upload_timeout.html @@ -16,9 +16,12 @@ async function probeUploads(scope, base, pendingOrigin) { {name: 'absent body success', bytes: null, timeout: 0, head: 150, expected: 'load'}, {name: 'disable elapsed timeout', timeout: 30, head: 300, busy: 150, after: 0, complete: true, expected: 'load'}, {name: 'author abort discards queued completion', timeout: 0, head: 500, busy: 150, abort: true, expected: 'abort'}, - {name: 'upload microtask disables timeout after headers', timeout: 30, head: 20, body: 400, busy: 150, afterUpload: 0, complete: true, expected: 'load'}, - {name: 'upload microtask disables timeout after periodic report', timeout: 30, head: 500, busy: 150, afterUpload: 0, complete: true, expected: 'load'}, - {name: 'upload microtask extends timeout', timeout: 30, head: 20, body: 400, busy: 150, afterUpload: 1000, complete: true, expected: 'load'} + // Do not require a real HTTP response to win a 10 ms scheduling window. + // The native upload/timeout regression covers an already-expired timer; + // transport tests separately gate response-start and periodic reports. + {name: 'upload microtask disables timeout with early headers', timeout: 500, head: 20, body: 1500, busy: 800, afterUpload: 0, complete: true, expected: 'load'}, + {name: 'upload microtask disables timeout while response is held', timeout: 500, head: 1500, busy: 800, afterUpload: 0, complete: true, expected: 'load'}, + {name: 'upload microtask extends timeout', timeout: 500, head: 20, body: 1500, busy: 800, afterUpload: 5000, complete: true, expected: 'load'} ]; const facts = []; for (const config of cases) {