From 2c98ca3ba850fc9ccd5dc9384ac602e91316dd21 Mon Sep 17 00:00:00 2001 From: Gleb Linnik Date: Sat, 14 Nov 2026 06:28:49 +0300 Subject: [PATCH] Net: bound a never-pausing peer's cancel latency by what the transport controls `a_peer_that_never_pauses_cannot_hold_off_a_cancel` asserted that `run` returned within one second of the cancel while its own callback slept 200 us per message. The pass in flight when the cancel is raised reads up to `PASS_BUDGET` (1 MiB, about 1045 of these frames) and calls the callback for each, so the measured time was the test's own cost: ~0.3 s where the sleep takes 250 us, but over a second where a coarse timer turns 200 us into 1+ ms. It failed on every run of the daily macOS bench's unit suite from 2026-11-10 (the first run after #1283), a 3-core virtualised runner. With the callback at 1.5 ms the unchanged assertion fails here too (2.3 s) while the transport behaves identically. The test now asserts the two things the transport controls: the messages delivered after the cancel are at most one pass (`PASS_BUDGET` plus the chunk that crossed it, over the frame size; a peer written for ten seconds would deliver tens of thousands), and the time spent outside the callback between the cancel and the return, the going-away close included, is under a second. Verified: passes at a 200 us and at a 1.5 ms callback; fails (after 10 s, on the message count) with `PASS_BUDGET` raised to 1 << 40. Co-Authored-By: Claude Opus 5.5 --- rust-modules/net/src/ws/transport_tests.rs | 28 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/rust-modules/net/src/ws/transport_tests.rs b/rust-modules/net/src/ws/transport_tests.rs index f1b855bf54..7d39cbb225 100755 --- a/rust-modules/net/src/ws/transport_tests.rs +++ b/rust-modules/net/src/ws/transport_tests.rs @@ -413,16 +413,26 @@ fn an_address_keeps_its_query_and_gains_a_root_path() { assert_eq!(split("ws://relay.example/v1/ws?x=1"), Some((false, "relay.example", "/v1/ws?x=1".to_owned()))); } +/// A peer that writes without a pause keeps the socket readable, so the pass that reads it must end +/// by its byte budget ([`PASS_BUDGET`]) for the loop to see a cancel at all. The bound is stated in +/// what the transport controls, not in wall time: the callback below is the test's own cost (a +/// 200 us sleep per message, which a coarse timer, such as a virtualised macOS runner's, stretches to +/// over a millisecond: the pass in flight then took longer than the second the old assertion +/// allowed, on every run, with the transport doing exactly what it should). So it asserts (a) how +/// many messages arrive after the cancel was raised, which a missing budget would leave at tens of +/// thousands (the peer writes for ten seconds), and (b) that the time the transport spent outside +/// the callback between the cancel and its return, the going-away close included, is under a second. #[test] fn a_peer_that_never_pauses_cannot_hold_off_a_cancel() { let _serial = plx_base::testlock::serial(); if !curl_ready() { return; } + const FRAME: usize = 1000 + 4; // a 1000-byte text frame has a four-byte header let (url, server) = serve_ws(|mut peer, _| { let mut burst = Vec::new(); while burst.len() < 1 << 16 { - encode(Opcode::Text, true, "x".repeat(1000).as_bytes(), None, &mut burst); + encode(Opcode::Text, true, "x".repeat(FRAME - 4).as_bytes(), None, &mut burst); } let until = Instant::now() + Duration::from_secs(10); while Instant::now() < until && peer.io.write_all(&burst).is_ok() {} @@ -430,19 +440,29 @@ fn a_peer_that_never_pauses_cannot_hold_off_a_cancel() { let cancel = AtomicBool::new(false); let (_outbox, inbox) = channel().unwrap(); let mut first = None; + let mut after_cancel = 0usize; + let mut in_callback = Duration::ZERO; let end = run(&OffFrame::for_test(), plain(&url), &cancel, inbox, &mut |event| { if matches!(event, Event::Text(_)) { - thread::sleep(Duration::from_micros(200)); + let entered = Instant::now(); if first.is_none() { - first = Some(Instant::now()); + first = Some(entered); cancel.store(true, Ordering::Release); + } else { + after_cancel += 1; } + thread::sleep(Duration::from_micros(200)); + in_callback += entered.elapsed(); } }); let took = first.unwrap().elapsed(); server.join().unwrap(); assert_eq!(end, End::Cancelled); - assert!(took < Duration::from_secs(1), "{took:?}"); + // the pass in flight stops once it has read its budget, plus the chunk that crossed it + let one_pass = (PASS_BUDGET + READ_CHUNK) / FRAME + 2; + assert!(after_cancel <= one_pass, "{after_cancel} messages after the cancel, one pass is at most {one_pass}"); + let transport = took.saturating_sub(in_callback); + assert!(transport < Duration::from_secs(1), "{transport:?} in the transport of {took:?}"); } #[test]