diff --git a/src/channel.rs b/src/channel.rs index 24c1fc9..fbc79ee 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -660,6 +660,9 @@ pub fn try_select(arms: &[&dyn Selectable]) -> std::io::Result { None => panic!("smarm: select() called outside an actor"), }; loop { + // Register-to-park must not be a preemption point: a Queued actor drops `unpark_at`, + // so a one-shot fd registration consumed in the window is a lost wake. As in `wait_fd`. + let _np = crate::scheduler::NoPreempt::enter(); let epoch = crate::scheduler::begin_wait(); if let Some(i) = register_arms(me, epoch, arms)? { return Ok(i); @@ -815,6 +818,9 @@ pub fn try_select_timeout( Some(me) => me, None => panic!("smarm: select_timeout() called outside an actor"), }; + // Prep-to-park region (see `try_select`): the timeout target allocation and the timer + // insert below must not be preemption points while the arms are registered. + let _np = crate::scheduler::NoPreempt::enter(); let epoch = crate::scheduler::begin_wait(); if let Some(i) = register_arms(me, epoch, arms)? { return Ok(Some(i)); // ready now: the timer was never armed diff --git a/tests/select_preempt_wake.rs b/tests/select_preempt_wake.rs new file mode 100644 index 0000000..ccaae38 --- /dev/null +++ b/tests/select_preempt_wake.rs @@ -0,0 +1,72 @@ +//! Regression: a readiness wake landing while a selecting actor is preempted between +//! registering its arms and parking must not be lost. `alloc_interval(1)` and a 1-cycle +//! timeslice make every allocation preempt, so the window is hit on every run. + +use std::os::fd::RawFd; +use std::time::{Duration, Instant}; + +fn pipe() -> (RawFd, RawFd) { + let mut fds = [0; 2]; + let r = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_NONBLOCK | libc::O_CLOEXEC) }; + assert_eq!(r, 0); + (fds[0], fds[1]) +} + +fn run_case(wait: fn(RawFd) -> bool) { + let rt = smarm::init( + smarm::Config::exact(1) + .alloc_interval(1) + .timeslice_cycles(1), + ); + rt.run(move || { + let mut worst = Duration::ZERO; + for _ in 0..10 { + let (r, w) = pipe(); + let writer = smarm::spawn(move || { + let b = [1u8]; + assert_eq!(unsafe { libc::write(w, b.as_ptr() as *const _, 1) }, 1); + // Hold the only scheduler thread (an OS-level sleep, not a park) so the reader + // stays Queued while the epoll thread consumes the readiness and wakes it. + std::thread::sleep(Duration::from_millis(20)); + }); + let t0 = Instant::now(); + assert!(wait(r), "wait reported a timeout with the pipe readable"); + worst = worst.max(t0.elapsed()); + writer.join().unwrap(); + unsafe { + libc::close(r); + libc::close(w); + } + } + // A lost wake usually still returns `true`: the timeout path re-polls the fd, finds + // it readable, and reports ready. So the latency bound is the real check. + assert!( + worst < Duration::from_millis(500), + "worst wake latency {worst:?}: the readiness wake was lost (waited out the timeout)" + ); + }); +} + +#[test] +fn wait_readable_timeout_survives_preemption_before_park() { + run_case(|fd| smarm::wait_readable_timeout(fd, Duration::from_secs(2)).unwrap()); +} + +#[test] +fn select_with_fd_arm_survives_preemption_before_park() { + run_case(|fd| { + let arm = smarm::FdArm::readable(fd); + smarm::try_select_timeout(&[&arm], Duration::from_secs(2)) + .unwrap() + .is_some() + }); +} + +#[test] +fn wait_readable_survives_preemption_before_park() { + // The guarded path, as a control: wait_fd holds NoPreempt across register-to-park. + run_case(|fd| { + smarm::wait_readable(fd).unwrap(); + true + }); +}