fix(select): no preemption between arm registration and park
try_select / try_select_timeout registered their arms under the current park-epoch, then did more work (allocating the timeout target, inserting the timer) before park_current. A preemption in that window leaves the actor Queued, and StateWord::unpark treats a Queued actor as a no-op: an fd arm's one-shot registration consumed by the epoll thread there is a lost wake, and the actor sleeps until its timeout (or forever, in try_select). wait_fd already guards the same window with NoPreempt. Seen in urus under CPU pressure: ~1 in 10^4 keep-alive requests stalled for the full keep-alive timeout with the request unread in the socket (wait_readable_timeout -> try_select_timeout). The new test forces the interleaving deterministically (alloc_interval 1, 1-cycle timeslice, one scheduler, writer holding the thread): both select paths lost the wake every time before; all pass now, as does the rest of the suite.
This commit is contained in:
@@ -660,6 +660,9 @@ pub fn try_select(arms: &[&dyn Selectable]) -> std::io::Result<usize> {
|
|||||||
None => panic!("smarm: select() called outside an actor"),
|
None => panic!("smarm: select() called outside an actor"),
|
||||||
};
|
};
|
||||||
loop {
|
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();
|
let epoch = crate::scheduler::begin_wait();
|
||||||
if let Some(i) = register_arms(me, epoch, arms)? {
|
if let Some(i) = register_arms(me, epoch, arms)? {
|
||||||
return Ok(i);
|
return Ok(i);
|
||||||
@@ -815,6 +818,9 @@ pub fn try_select_timeout(
|
|||||||
Some(me) => me,
|
Some(me) => me,
|
||||||
None => panic!("smarm: select_timeout() called outside an actor"),
|
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();
|
let epoch = crate::scheduler::begin_wait();
|
||||||
if let Some(i) = register_arms(me, epoch, arms)? {
|
if let Some(i) = register_arms(me, epoch, arms)? {
|
||||||
return Ok(Some(i)); // ready now: the timer was never armed
|
return Ok(Some(i)); // ready now: the timer was never armed
|
||||||
|
|||||||
@@ -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
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user