runite 0.1.0

An event-loop-per-thread async runtime built on io_uring (Linux), kqueue (macOS), and IOCP (Windows)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! runite driver for Linux.
//!
//!

use std::any::Any;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::HashMap;
use std::io;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use super::uring::{IORING_OP_ASYNC_CANCEL, IoUring, IoUringCqe, IoUringSqe, SupportedOps};
use crate::platform::runtime_shared::{DriverBackend, Notifier};
use crate::trace_targets;

pub use crate::platform::runtime_shared::ReadyEvents;

const WAKE_TARGET_TOKEN: u64 = 1;
const TOKEN_KIND_SHIFT: u64 = 56;
const TOKEN_KIND_MASK: u64 = 0xff << TOKEN_KIND_SHIFT;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
enum CompletionKind {
    Timer = 1,
    TimerRemove = 2,
    NotifySend = 3,
    Operation = 4,
    OperationCancel = 5,
    /// CQE produced by the IORING_OP_LINK_TIMEOUT SQE that accompanies a
    /// linked operation.  We register it so the token range is claimed, but
    /// the completion itself carries no useful information and is discarded.
    LinkedTimeout = 6,
}

type CompletionHandler = Box<dyn FnOnce(IoUringCqe) + Send + 'static>;
type CancelGuard = Box<dyn Any + Send + 'static>;

struct NotifierInner {
    /// A **dup** of the target driver's ring fd, owned by the notifier.
    ///
    /// Holding an [`OwnedFd`] (rather than a bare `RawFd` copy) closes a TOCTOU
    /// hazard: after the target [`Driver`] drops and closes its own ring fd, the
    /// raw number could be recycled by the kernel for an unrelated resource, and
    /// an in-flight `notify` that already passed the `closed` check would then
    /// target that resource. The dup keeps the fd number reserved (and the ring
    /// object alive) for as long as any notifier exists, so a racing `notify`
    /// can only ever reach the original — now draining — ring, never a
    /// recycled fd. The kernel ring is fully released once the last notifier
    /// (i.e. the last cross-thread handle) drops.
    ring_fd: OwnedFd,
    closed: AtomicBool,
}

impl NotifierInner {
    fn notify(&self) -> io::Result<()> {
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::DRIVER,
            event = "notify",
            ring_fd = self.ring_fd.as_raw_fd(),
            "sending cross-thread driver wake"
        );
        if self.closed.load(Ordering::Acquire) {
            return Err(io::Error::new(
                io::ErrorKind::BrokenPipe,
                "target runtime ring is closed",
            ));
        }

        IoUring::with_submitter(|ring| {
            ring.submit_msg_ring(
                self.ring_fd.as_raw_fd(),
                WAKE_TARGET_TOKEN,
                1,
                make_token(CompletionKind::NotifySend, 0),
            )
        })
    }
}

/// Duplicates `fd` with `O_CLOEXEC`, returning an owned handle to the copy.
fn dup_cloexec(fd: RawFd) -> io::Result<OwnedFd> {
    // F_DUPFD_CLOEXEC yields the lowest-numbered free fd >= 0, with the
    // close-on-exec flag already set on the new descriptor.
    let duplicated = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0) };
    if duplicated < 0 {
        return Err(io::Error::last_os_error());
    }
    // SAFETY: `duplicated` is a fresh, exclusively-owned fd just returned by
    // `fcntl(F_DUPFD_CLOEXEC)`; wrapping it transfers ownership to the `OwnedFd`.
    Ok(unsafe { OwnedFd::from_raw_fd(duplicated) })
}

#[derive(Clone)]
/// Cross-thread notifier for a runtime thread's driver.
pub struct ThreadNotifier {
    inner: Arc<NotifierInner>,
}

impl Notifier for ThreadNotifier {
    fn notify(&self) -> io::Result<()> {
        self.inner.notify()
    }
}

/// Low-level Linux runtime driver backed by `io_uring`.
pub struct Driver {
    /// The `io_uring` instance driving this runtime thread.
    ring: IoUring,
    /// Process-wide io_uring opcode support snapshot used by this driver.
    supported_ops: SupportedOps,
    /// Shared notifier that other threads can use to wake this runtime thread.
    notifier: Arc<NotifierInner>,
    /// Next sequence number for generated completion tokens.
    next_token: Cell<u64>,
    /// The token of the currently active timer, if any timer is armed.
    active_timer_token: Cell<Option<u64>>,
    /// Accumulated count of pending wake notifications that have not yet been triggered.
    pending_wakes: Cell<u64>,
    /// Accumulated count of pending timer expirations that have not yet been triggered.
    pending_timers: Cell<u64>,
    /// Map of active completion tokens to associated handlers. When a CQE is received with a token in this map, the
    /// corresponding handler will be invoked with the CQE and removed from the map. This is the core mechanism by which
    /// async operations are tracked and dispatched to their continuations.
    completions: RefCell<HashMap<u64, CompletionHandler>>,
    /// Guards detached when a future is dropped while its SQE may still touch
    /// memory owned by that future. Entries are keyed by the original operation
    /// token and dropped when either the original CQE or cancel CQE arrives.
    pending_cancel_buffers: RefCell<HashMap<u64, Vec<CancelGuard>>>,
    pending_cancel_tokens: RefCell<HashMap<u64, u64>>,
}

/// Creates a new driver and its paired [`ThreadNotifier`].
pub fn create_driver() -> io::Result<(Driver, ThreadNotifier)> {
    let ring = IoUring::new(64)?;
    tracing::debug!(
        target: trace_targets::DRIVER,
        event = "create_driver",
        ring_fd = ring.ring_fd(),
        "created runtime driver"
    );
    let supported_ops = ring.supported_ops();
    let notifier = Arc::new(NotifierInner {
        ring_fd: dup_cloexec(ring.ring_fd())?,
        closed: AtomicBool::new(false),
    });

    Ok((
        Driver {
            ring,
            supported_ops,
            notifier: Arc::clone(&notifier),
            next_token: Cell::new(1),
            active_timer_token: Cell::new(None),
            pending_wakes: Cell::new(0),
            pending_timers: Cell::new(0),
            completions: RefCell::new(HashMap::new()),
            pending_cancel_buffers: RefCell::new(HashMap::new()),
            pending_cancel_tokens: RefCell::new(HashMap::new()),
        },
        ThreadNotifier { inner: notifier },
    ))
}

impl Driver {
    pub(crate) fn bind_current_thread(&self) {
        self.ring.bind_current_thread();
    }

    pub(crate) fn unbind_current_thread(&self) {
        self.ring.unbind_current_thread();
    }

    /// Polls the driver without blocking.
    pub fn poll(&self) -> io::Result<Option<ReadyEvents>> {
        let mut ready = ReadyEvents::default();
        let saw_any = self
            .ring
            .drain_completions(|cqe| self.process_cqe(cqe, &mut ready));
        #[cfg(debug_assertions)]
        if saw_any {
            tracing::trace!(
                target: trace_targets::DRIVER,
                event = "poll_ready",
                timer_ready = ready.timer,
                wake_ready = ready.wake,
                "driver poll produced ready events"
            );
        }
        if saw_any { Ok(Some(ready)) } else { Ok(None) }
    }

    /// Blocks until at least one completion is available.
    pub fn wait(&self) -> io::Result<()> {
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::DRIVER,
            event = "wait",
            "waiting for driver completion"
        );
        self.ring.wait_for_cqe()
    }

    /// Updates the currently armed timer deadline.
    ///
    /// Passing `None` removes any active timer.
    pub fn rearm_timer(&self, deadline: Option<Duration>) -> io::Result<()> {
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::TIMER,
            event = "rearm_timer",
            deadline_ns = deadline.map(|value| value.as_nanos() as u64),
            "rearming driver timer"
        );
        match (self.active_timer_token.get(), deadline) {
            (Some(active), Some(deadline)) => {
                self.ring.submit_timeout_update(
                    active,
                    deadline,
                    self.next_token(CompletionKind::TimerRemove),
                )?;
            }
            (Some(active), None) => {
                self.active_timer_token.set(None);
                self.ring
                    .submit_timeout_remove(active, self.next_token(CompletionKind::TimerRemove))?;
            }
            (None, Some(deadline)) => {
                let token = self.next_token(CompletionKind::Timer);
                self.active_timer_token.set(Some(token));
                self.ring.submit_timeout(token, deadline)?;
            }
            (None, None) => {}
        }

        Ok(())
    }

    pub(crate) fn submit_operation(
        &self,
        fill: impl FnOnce(&mut IoUringSqe),
        on_complete: impl FnOnce(IoUringCqe) + Send + 'static,
    ) -> io::Result<u64> {
        let mut prepared = IoUringSqe::default();
        fill(&mut prepared);
        self.validate_opcode(prepared.opcode)?;

        let token = self.next_token(CompletionKind::Operation);
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::ASYNC,
            event = "submit_operation",
            token,
            "submitting async driver operation"
        );
        self.completions
            .borrow_mut()
            .insert(token, Box::new(on_complete));

        if let Err(error) = self.ring.submit_with_token(token, |sqe| *sqe = prepared) {
            let _ = self.completions.borrow_mut().remove(&token);
            return Err(error);
        }

        Ok(token)
    }

    /// Submits a main operation linked to a timeout.
    ///
    /// Internally two SQEs are enqueued atomically: the main op (with
    /// `IOSQE_IO_LINK`) and an `IORING_OP_LINK_TIMEOUT` SQE.  If `timeout`
    /// elapses before the main op completes, the kernel cancels the main op and
    /// its CQE will carry `-ECANCELED`.  The timeout's own CQE is silently
    /// discarded in `process_cqe`.
    ///
    /// Returns the token for the main operation, which can be used with
    /// `cancel_operation` to cancel it early.
    pub(crate) fn submit_operation_with_linked_timeout(
        &self,
        fill: impl FnOnce(&mut IoUringSqe),
        timeout: Duration,
        on_complete: impl FnOnce(IoUringCqe) + Send + 'static,
    ) -> io::Result<u64> {
        let mut prepared = IoUringSqe::default();
        fill(&mut prepared);
        self.validate_opcode(prepared.opcode)?;

        let main_token = self.next_token(CompletionKind::Operation);
        let timeout_token = self.next_token(CompletionKind::LinkedTimeout);
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::ASYNC,
            event = "submit_operation_with_linked_timeout",
            main_token,
            timeout_token,
            timeout_ns = timeout.as_nanos() as u64,
            "submitting async driver operation with linked timeout"
        );
        self.completions
            .borrow_mut()
            .insert(main_token, Box::new(on_complete));

        if let Err(error) = self.ring.submit_linked_with_timeout(
            main_token,
            |sqe| *sqe = prepared,
            timeout_token,
            timeout,
        ) {
            let mut completions = self.completions.borrow_mut();
            let _ = completions.remove(&main_token);
            return Err(error);
        }

        Ok(main_token)
    }

    pub(crate) fn cancel_operation(&self, token: u64) -> io::Result<()> {
        self.cancel_operation_with_guard(token, None)
    }

    pub(crate) fn cancel_operation_with_guard(
        &self,
        token: u64,
        guard: Option<CancelGuard>,
    ) -> io::Result<()> {
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::ASYNC,
            event = "cancel_operation",
            token,
            "submitting async driver cancellation"
        );
        if let Some(guard) = guard {
            self.pending_cancel_buffers
                .borrow_mut()
                .entry(token)
                .or_default()
                .push(guard);
        }
        let cancel_token = self.next_token(CompletionKind::OperationCancel);
        self.pending_cancel_tokens
            .borrow_mut()
            .insert(cancel_token, token);
        match self.ring.submit_with_token(cancel_token, |sqe| {
            sqe.opcode = IORING_OP_ASYNC_CANCEL;
            sqe.fd = -1;
            sqe.addr = token;
        }) {
            Ok(()) => Ok(()),
            Err(error) => {
                self.pending_cancel_tokens
                    .borrow_mut()
                    .remove(&cancel_token);
                let _ = self.pending_cancel_buffers.borrow_mut().remove(&token);
                Err(error)
            }
        }
    }

    /// Drains the accumulated wake notification count.
    ///
    /// Returns `Some(count)` with the number of wake notifications collected
    /// since the previous call, or `None` if no wake completions were pending.
    pub fn drain_wake(&self) -> Option<u64> {
        let wakes = self.pending_wakes.replace(0);
        if wakes == 0 { None } else { Some(wakes) }
    }

    /// Drains the accumulated timer-expiration count.
    ///
    /// Returns `Some(count)` with the number of timer expirations collected
    /// since the previous call, or `None` if no timer completions were pending.
    pub fn drain_timer(&self) -> Option<u64> {
        let timers = self.pending_timers.replace(0);
        if timers == 0 { None } else { Some(timers) }
    }

    fn process_cqe(&self, cqe: IoUringCqe, ready: &mut ReadyEvents) {
        #[cfg(debug_assertions)]
        tracing::trace!(
            target: trace_targets::DRIVER,
            event = "process_cqe",
            user_data = cqe.user_data,
            result = cqe.res,
            "processing io_uring completion"
        );
        if cqe.user_data == WAKE_TARGET_TOKEN {
            ready.wake = true;
            let wakes = cqe.res.max(1) as u64;
            self.pending_wakes
                .set(self.pending_wakes.get().saturating_add(wakes));
            return;
        }

        match decode_token_kind(cqe.user_data) {
            Some(CompletionKind::Timer) => {
                if self.active_timer_token.get() == Some(cqe.user_data) {
                    self.active_timer_token.set(None);
                }
                if cqe.res == -libc::ETIME {
                    ready.timer = true;
                    self.pending_timers
                        .set(self.pending_timers.get().saturating_add(1));
                }
            }
            Some(CompletionKind::Operation) => {
                let _guards = self
                    .pending_cancel_buffers
                    .borrow_mut()
                    .remove(&cqe.user_data);
                if let Some(callback) = self.completions.borrow_mut().remove(&cqe.user_data) {
                    callback(cqe);
                }
            }
            Some(CompletionKind::OperationCancel) => {
                // A cancel completion only signals that the ASYNC_CANCEL
                // request itself finished; it does NOT prove the original
                // request has released the guarded user buffers. In particular
                // `IORING_OP_ASYNC_CANCEL` can report `-EALREADY` (the target op
                // was already executing, could not be stopped, and will still
                // complete later — potentially writing into those buffers). The
                // buffer guards are therefore released exclusively by the
                // original operation's own completion (the
                // `CompletionKind::Operation` arm above), which the kernel
                // always posts exactly once. Here we only discard the
                // cancel-token bookkeeping.
                self.pending_cancel_tokens
                    .borrow_mut()
                    .remove(&cqe.user_data);
            }
            Some(CompletionKind::TimerRemove)
            | Some(CompletionKind::NotifySend)
            | Some(CompletionKind::LinkedTimeout)
            | None => {}
        }
    }

    fn validate_opcode(&self, opcode: u8) -> io::Result<()> {
        if self.supported_ops.supports(opcode) {
            Ok(())
        } else {
            Err(IoUring::unsupported_opcode_error(opcode))
        }
    }

    #[cfg(test)]
    fn supported_ops(&self) -> SupportedOps {
        self.supported_ops
    }

    fn next_token(&self, kind: CompletionKind) -> u64 {
        let seq = self.next_token.get();
        self.next_token.set(seq.wrapping_add(1));
        make_token(kind, seq)
    }
}

impl Drop for Driver {
    fn drop(&mut self) {
        tracing::debug!(
            target: trace_targets::DRIVER,
            event = "drop_driver",
            "dropping runtime driver"
        );
        self.notifier.closed.store(true, Ordering::Release);
    }
}

impl DriverBackend for Driver {
    fn poll(&self) -> io::Result<Option<ReadyEvents>> {
        Driver::poll(self)
    }

    fn wait(&self) -> io::Result<()> {
        Driver::wait(self)
    }

    fn rearm_timer(&self, deadline: Option<Duration>) -> io::Result<()> {
        Driver::rearm_timer(self, deadline)
    }

    fn drain_wake(&self) -> Option<u64> {
        Driver::drain_wake(self)
    }

    fn drain_timer(&self) -> Option<u64> {
        Driver::drain_timer(self)
    }

    fn bind_current_thread(&self) {
        Driver::bind_current_thread(self)
    }

    fn unbind_current_thread(&self) {
        Driver::unbind_current_thread(self)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Returns the current monotonic time used by the runtime timer system.
pub fn monotonic_now() -> io::Result<Duration> {
    let mut now = std::mem::MaybeUninit::<libc::timespec>::uninit();
    // SAFETY: `now.as_mut_ptr()` points to writable, properly aligned
    // `timespec` storage for the duration of the syscall.
    let result = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, now.as_mut_ptr()) };
    if result == -1 {
        return Err(io::Error::last_os_error());
    }

    // SAFETY: `clock_gettime` returned success, which initializes the
    // `timespec` value in `now`.
    let now = unsafe { now.assume_init() };
    Ok(Duration::new(now.tv_sec as u64, now.tv_nsec as u32))
}

fn make_token(kind: CompletionKind, seq: u64) -> u64 {
    ((kind as u64) << TOKEN_KIND_SHIFT) | (seq & !TOKEN_KIND_MASK)
}

fn decode_token_kind(token: u64) -> Option<CompletionKind> {
    match ((token & TOKEN_KIND_MASK) >> TOKEN_KIND_SHIFT) as u8 {
        1 => Some(CompletionKind::Timer),
        2 => Some(CompletionKind::TimerRemove),
        3 => Some(CompletionKind::NotifySend),
        4 => Some(CompletionKind::Operation),
        5 => Some(CompletionKind::OperationCancel),
        6 => Some(CompletionKind::LinkedTimeout),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::super::uring::{IORING_OP_NOP, SupportedOps, override_supported_ops};
    use super::{Notifier as _, create_driver, monotonic_now};
    use std::io;
    use std::os::fd::AsRawFd;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::thread;
    use std::time::Duration;

    #[test]
    fn probe_runs_and_returns_bitmap() {
        let (driver, _notifier) = create_driver().expect("driver should initialize");
        let ops = driver.supported_ops();

        assert!(
            ops.probe_unavailable() || (ops.probe_supported() && ops.supports(IORING_OP_NOP)),
            "probe should either report NOP support or mark probing unavailable"
        );
    }

    #[test]
    fn unsupported_op_returns_unsupported_error() {
        let _override = override_supported_ops(SupportedOps::only([IORING_OP_NOP]));
        let (driver, _notifier) = create_driver().expect("driver should initialize");

        let completed = Arc::new(AtomicBool::new(false));
        let completed_for_callback = Arc::clone(&completed);
        let token = driver
            .submit_operation(
                |sqe| {
                    sqe.opcode = IORING_OP_NOP;
                    sqe.fd = -1;
                },
                move |_| {
                    completed_for_callback.store(true, Ordering::Release);
                },
            )
            .expect("supported NOP should submit");
        assert_ne!(token, 0);

        for _ in 0..100 {
            let _ = driver.poll().expect("poll should succeed");
            if completed.load(Ordering::Acquire) {
                break;
            }
            thread::sleep(Duration::from_millis(1));
        }
        assert!(completed.load(Ordering::Acquire));

        let error = driver
            .submit_operation(
                |sqe| {
                    sqe.opcode = 250;
                    sqe.fd = -1;
                },
                |_| {},
            )
            .expect_err("unsupported opcode should be rejected before submission");

        assert_eq!(error.kind(), io::ErrorKind::Unsupported);
        assert!(error.to_string().contains("opcode 250"));
    }

    #[test]
    fn notifier_wakes_target_ring() {
        let (sender, _) = create_driver().expect("sender driver should initialize");
        sender.bind_current_thread();

        let (target, notifier) = create_driver().expect("target driver should initialize");
        notifier.notify().expect("notify should succeed");

        let ready = loop {
            if let Some(ready) = target.poll().expect("poll should succeed") {
                break ready;
            }
            thread::sleep(Duration::from_millis(1));
        };

        assert!(ready.wake);
        assert!(!ready.timer);
        assert_eq!(target.drain_wake(), Some(1));
        sender.unbind_current_thread();
    }

    #[test]
    fn notifier_fd_survives_target_driver_drop() {
        let (target, notifier) = create_driver().expect("target driver should initialize");
        let dup_fd = notifier.inner.ring_fd.as_raw_fd();

        // Dropping the target closes its own ring fd and marks the notifier
        // closed. With a bare `RawFd` copy the notifier's number would now be
        // dangling (recyclable); the owned dup keeps it reserved and the ring
        // object alive.
        drop(target);

        let flags = unsafe { libc::fcntl(dup_fd, libc::F_GETFD) };
        assert!(
            flags >= 0,
            "notifier dup fd must remain valid after the target driver drops \
             (got errno {})",
            io::Error::last_os_error()
        );

        // notify() short-circuits on the closed flag rather than submitting to
        // a stale/recycled fd.
        assert_eq!(
            notifier
                .notify()
                .expect_err("notify after drop should fail")
                .kind(),
            io::ErrorKind::BrokenPipe,
        );
    }

    #[test]
    fn notifier_wakes_target_ring_from_plain_thread() {
        let (target, notifier) = create_driver().expect("target driver should initialize");

        thread::spawn(move || {
            notifier.notify().expect("notify should succeed");
        })
        .join()
        .expect("notifier thread should exit cleanly");

        let ready = loop {
            if let Some(ready) = target.poll().expect("poll should succeed") {
                break ready;
            }
            thread::sleep(Duration::from_millis(1));
        };

        assert!(ready.wake);
        assert!(!ready.timer);
        assert_eq!(target.drain_wake(), Some(1));
    }

    #[test]
    fn timeout_reports_deadlines() {
        let (driver, _notifier) = create_driver().expect("driver should initialize");
        let deadline = monotonic_now().expect("clock should work") + Duration::from_millis(20);
        driver
            .rearm_timer(Some(deadline))
            .expect("timer should arm");

        let ready = loop {
            if let Some(ready) = driver.poll().expect("poll should succeed") {
                break ready;
            }
            thread::sleep(Duration::from_millis(5));
        };

        assert!(ready.timer);
        assert!(!ready.wake);
        assert_eq!(driver.drain_timer(), Some(1));
    }
}