From 02b864877b2ac20563ca9eb8a7cad90e4faa6bd5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 13:23:30 -0700 Subject: [PATCH] fix(moq-c): hold the wait so an end-gap test sees the gap every run consume_announced_delivers_the_instance_again_after_an_end flaked about one run in six. The C wait follows on its own task, so it could take the route above the path before the test unannounced the path. The cursor then knew that route stood before the path's own route left, which is no gap, and reported an update: the instance rides on and correctly is not delivered again. The test timed out waiting for a delivery it never earned. Hold the wait inside its first delivery until both changes are made, so they are on hand together and the cursor reports the end and the start the test is about. The follow loop is unchanged; the moq-ffi twin already makes both changes between available() calls, when nothing is following. Co-Authored-By: Claude Opus 5.5 --- rs/moq-c/src/test.rs | 46 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/rs/moq-c/src/test.rs b/rs/moq-c/src/test.rs index 5dbd9aa1963..e5eb93515ab 100644 --- a/rs/moq-c/src/test.rs +++ b/rs/moq-c/src/test.rs @@ -3246,24 +3246,58 @@ fn consume_announced(origin: u32, path: &[u8], cb: &Callback) -> u32 { }) } +/// Forwards each code through `tx`, holding the caller inside its first delivery until `hold` +/// fires. +struct Gate { + tx: mpsc::Sender, + hold: std::sync::Mutex>>, +} + +extern "C" fn gated_callback(user_data: *mut c_void, code: i32) { + let gate = unsafe { &*(user_data as *const Gate) }; + let _ = gate.tx.send(code); + let hold = gate.hold.lock().unwrap().take(); + if let Some(hold) = hold { + let _ = hold.recv(); + } +} + /// An end is a gap that ended every request on the old route, even when a route above the path /// with the same epoch carries the instance on. So the instance serving the path afterwards is /// delivered again, though the caller still holds its handle. #[test] fn consume_announced_delivers_the_instance_again_after_an_end() { let origin = id(moq_origin_create()); - let cb = Callback::new(); - let task = consume_announced(origin, b"live", &cb); + let (tx, rx) = mpsc::channel(); + let (release, hold) = mpsc::channel(); + let gate = Box::new(Gate { + tx, + hold: std::sync::Mutex::new(Some(hold)), + }); + let path = b"live"; + let task = id(unsafe { + moq_origin_announced_broadcast( + origin, + path.as_ptr() as *const c_char, + path.len(), + Some(gated_callback), + &*gate as *const Gate as *mut c_void, + ) + }); + let recv = || rx.recv_timeout(TIMEOUT).expect("callback timed out"); let route = moq_net::origin::Route::default().with_epoch(moq_net::Epoch::mint()); let run = publish(origin, "live", route.clone()); - let first = id(cb.recv()); + let first = id(recv()); + // Hold the wait inside that delivery so both changes are on hand when it next looks. Seeing + // the route above stand before the path's own route left would prove no gap: an update. let dynamic = serve_dynamic(origin, "", route); { let _runtime = crate::ffi::RUNTIME.enter(); run.unannounce(); } + release.send(()).unwrap(); let request = crate::ffi::RUNTIME.block_on(dynamic.requested_broadcast()).unwrap(); let served = { let _runtime = crate::ffi::RUNTIME.enter(); @@ -3271,13 +3305,13 @@ fn consume_announced_delivers_the_instance_again_after_an_end() { request.accept(served.consume()); served }; - let again = id(cb.recv()); + let again = id(recv()); assert_eq!(moq_consume_close(first), 0); assert_eq!(moq_consume_close(again), 0); assert_eq!(moq_origin_announced_broadcast_cancel(task), 0); - assert_eq!(cb.recv_terminal(), 0, "cancel delivers terminal 0"); - drop((run, dynamic, served)); + assert_eq!(recv(), 0, "cancel delivers terminal 0"); + drop((run, dynamic, served, gate)); assert_eq!(moq_origin_close(origin), 0); }