commonware-runtime 2026.10.0

Execute asynchronous tasks with a configurable scheduler.
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
//! This module provides an io_uring-based implementation of the [crate::Network] trait,
//! offering fast, high-throughput network operations on Linux systems.
//!
//! ## Architecture
//!
//! Ring-backed operations register requests with the worker that first polls them.
//! Requests stay on that worker, with completion forwarded when their futures are
//! polled elsewhere. Connected socket halves and listeners retain descriptors and
//! buffering policy. They own no ring and can move between workers. Moving a future
//! does not keep its original worker alive.
//!
//! Accept first tries a nonblocking syscall. Only an empty accept queue registers
//! a single-shot readiness request, and cancellation cannot consume a connection.
//!
//! ## Memory Safety
//!
//! Buffers and file descriptors are owned by the active request state machine inside the io_uring
//! loop, ensuring that the memory location is valid for the duration of the operation.
//!
//! ## Linux Only
//!
//! This implementation is only available on Linux systems that support io_uring.
//! It requires Linux kernel 6.1 or newer. See [crate::iouring] for details.

use crate::{
    Buf, Error, IoBufMut, IoBufs,
    iouring::{
        operation::Operation,
        request::{ConnectRequest, PollRequest, RecvRequest, Request, RequestOutput, SendRequest},
        sockaddr::SockAddr,
    },
};
use std::{
    net::{SocketAddr, TcpListener},
    os::fd::{AsRawFd, FromRawFd, OwnedFd},
    sync::Arc,
    time::{Duration, Instant},
};
use tracing::warn;

/// Default read buffer size (64 KB).
const DEFAULT_READ_BUFFER_SIZE: usize = 64 * 1024;

/// Configuration for the io_uring network backend.
#[derive(Clone, Debug)]
pub struct Config {
    /// If Some, explicitly sets TCP_NODELAY on the socket.
    /// Otherwise uses system default.
    pub tcp_nodelay: Option<bool>,
    /// Whether to set `SO_LINGER` to zero on the socket.
    ///
    /// When enabled, causes an immediate RST on close, avoiding
    /// `TIME_WAIT` state. This is useful in adversarial environments to
    /// reclaim socket resources immediately when closing connections to
    /// misbehaving peers.
    pub zero_linger: bool,
    /// Timeout for establishing an outbound TCP connection.
    ///
    /// If the timeout expires, `Network::dial` returns [`Error::Timeout`].
    pub connect_timeout: Duration,
    /// Timeout budget applied to each top-level send/recv call.
    ///
    /// The owning runner validates its timeout wheel against this policy.
    /// Moving a resource to a runner with a smaller horizon can reject an
    /// operation whose deadline cannot be represented.
    pub read_write_timeout: Duration,
    /// Size of the read buffer for batching network reads.
    ///
    /// A larger buffer reduces syscall overhead by reading more data per call,
    /// but uses more memory per connection. Defaults to 64 KB.
    pub read_buffer_size: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            tcp_nodelay: Some(true),
            zero_linger: true,
            connect_timeout: Duration::from_secs(10),
            read_write_timeout: Duration::from_secs(60),
            read_buffer_size: DEFAULT_READ_BUFFER_SIZE,
        }
    }
}

/// [crate::Network] implementation that uses io_uring to do async I/O.
#[derive(Clone)]
pub struct Network {
    /// If Some, explicitly sets TCP_NODELAY on the socket.
    /// Otherwise uses system default.
    tcp_nodelay: Option<bool>,
    /// Whether to set `SO_LINGER` to zero on the socket.
    zero_linger: bool,
    /// Timeout for establishing an outbound TCP connection.
    connect_timeout: Duration,
    /// Timeout budget applied to each send/recv call.
    read_write_timeout: Duration,
    /// Size of the read buffer for batching network reads.
    read_buffer_size: usize,
}

impl Network {
    /// Create a network with the given socket policy.
    pub(crate) const fn new(cfg: Config) -> Self {
        Self {
            tcp_nodelay: cfg.tcp_nodelay,
            zero_linger: cfg.zero_linger,
            connect_timeout: cfg.connect_timeout,
            read_write_timeout: cfg.read_write_timeout,
            read_buffer_size: cfg.read_buffer_size,
        }
    }
}

/// Apply best-effort TCP policy to a socket without transferring ownership.
fn configure_socket(fd: &OwnedFd, tcp_nodelay: Option<bool>, zero_linger: bool) {
    if let Some(enabled) = tcp_nodelay {
        let value: libc::c_int = enabled.into();

        // SAFETY: `fd` owns the live socket throughout this call. The kernel reads
        // exactly one initialized integer from `value` before setsockopt returns.
        if unsafe {
            libc::setsockopt(
                fd.as_raw_fd(),
                libc::IPPROTO_TCP,
                libc::TCP_NODELAY,
                std::ptr::from_ref(&value).cast(),
                size_of_val(&value) as libc::socklen_t,
            )
        } == -1
        {
            warn!(err = ?std::io::Error::last_os_error(), "failed to set TCP_NODELAY");
        }
    }

    if zero_linger {
        let value = libc::linger {
            l_onoff: 1,
            l_linger: 0,
        };
        // SAFETY: `fd` remains owned and `value` is initialized, correctly aligned
        // linger storage. setsockopt copies its bytes synchronously.
        if unsafe {
            libc::setsockopt(
                fd.as_raw_fd(),
                libc::SOL_SOCKET,
                libc::SO_LINGER,
                std::ptr::from_ref(&value).cast(),
                size_of_val(&value) as libc::socklen_t,
            )
        } == -1
        {
            warn!(err = ?std::io::Error::last_os_error(), "failed to set SO_LINGER");
        }
    }
}

impl crate::Network for Network {
    type Listener = Listener;

    async fn bind(&self, socket: SocketAddr) -> Result<Self::Listener, Error> {
        let listener = TcpListener::bind(socket).map_err(|_| Error::BindFailed)?;
        listener
            .set_nonblocking(true)
            .map_err(|_| Error::BindFailed)?;

        Ok(Listener {
            tcp_nodelay: self.tcp_nodelay,
            zero_linger: self.zero_linger,
            inner: Arc::new(listener),
            read_write_timeout: self.read_write_timeout,
            read_buffer_size: self.read_buffer_size,
        })
    }

    async fn dial(
        &self,
        socket: SocketAddr,
    ) -> Result<(crate::SinkOf<Self>, crate::StreamOf<Self>), Error> {
        // Include socket creation and time waiting for staging in the timeout.
        let deadline = Instant::now() + self.connect_timeout;
        let family = if socket.is_ipv4() {
            libc::AF_INET
        } else {
            libc::AF_INET6
        };

        // SAFETY: socket takes only integer flags and returns a fresh descriptor
        // or -1. The successful descriptor is immediately placed in one owner.
        let raw = unsafe {
            libc::socket(
                family,
                libc::SOCK_STREAM | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC,
                0,
            )
        };
        if raw < 0 {
            return Err(Error::ConnectionFailed);
        }

        // SAFETY: `raw` is the unique successful result of socket above and has
        // not been closed or placed in another owning descriptor.
        let fd = Arc::new(unsafe { OwnedFd::from_raw_fd(raw) });
        let output = Operation::register(Request::Connect(ConnectRequest {
            fd: fd.clone(),
            address: Box::new(SockAddr::from(socket)),
            deadline: Some(deadline),
        }))
        .await
        .map_err(|_| Error::ConnectionFailed)?;

        let RequestOutput::Connect(result) = output else {
            unreachable!("connect request returned another output kind");
        };
        result.map_err(|error| match error {
            Error::Timeout => Error::Timeout,
            _ => Error::ConnectionFailed,
        })?;

        configure_socket(&fd, self.tcp_nodelay, self.zero_linger);

        Ok((
            Sink::new(fd.clone(), self.read_write_timeout),
            Stream::new(fd, self.read_write_timeout, self.read_buffer_size),
        ))
    }
}

/// Implementation of [crate::Listener] for an io-uring [Network].
pub struct Listener {
    /// If Some, explicitly sets TCP_NODELAY on the socket.
    /// Otherwise uses system default.
    tcp_nodelay: Option<bool>,
    /// Whether to set `SO_LINGER` to zero on the socket.
    zero_linger: bool,
    /// Shared listener retained by every outstanding readiness observation.
    inner: Arc<TcpListener>,
    /// Timeout budget applied to each send/recv call.
    read_write_timeout: Duration,
    /// Size of the read buffer for batching network reads.
    read_buffer_size: usize,
}

impl crate::Listener for Listener {
    type Stream = Stream;
    type Sink = Sink;

    async fn accept(&mut self) -> Result<(SocketAddr, Self::Sink, Self::Stream), Error> {
        let (stream, remote_addr) = loop {
            // Accept only while this future is being polled. A readiness
            // request left behind by cancellation cannot consume a connection.
            match self.inner.accept() {
                Ok(accepted) => break accepted,
                Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {}
                Err(_) => return Err(Error::ConnectionFailed),
            }

            let output = Operation::register(Request::Poll(PollRequest {
                fd: self.inner.clone(),
                deadline: Some(Instant::now() + self.read_write_timeout),
            }))
            .await
            .map_err(|_| Error::ConnectionFailed)?;

            let RequestOutput::Poll(result) = output else {
                unreachable!("readiness request returned another output kind");
            };

            match result {
                // Readiness may be stale, and an idle listener has no public
                // timeout. Both outcomes retry the nonblocking accept syscall.
                Ok(()) | Err(Error::Timeout) => {}
                Err(_) => return Err(Error::ConnectionFailed),
            }
        };

        // Accepted sockets do not inherit the listener's nonblocking flag.
        stream
            .set_nonblocking(true)
            .map_err(|_| Error::ConnectionFailed)?;

        let fd = Arc::new(OwnedFd::from(stream));
        configure_socket(&fd, self.tcp_nodelay, self.zero_linger);

        Ok((
            remote_addr,
            Sink::new(fd.clone(), self.read_write_timeout),
            Stream::new(fd, self.read_write_timeout, self.read_buffer_size),
        ))
    }

    fn local_addr(&self) -> Result<SocketAddr, std::io::Error> {
        self.inner.local_addr()
    }
}

/// Implementation of [crate::Sink] for an io-uring [Network].
pub struct Sink {
    /// Shared socket descriptor backing this sink half.
    fd: Arc<OwnedFd>,
    /// Timeout budget for a top-level send call.
    timeout: Duration,
    /// Tracks this sink's lifecycle.
    state: SinkState,
}

/// Lifecycle state for the write-half of a connection.
enum SinkState {
    /// Sends may be attempted.
    Open,
    /// A send is currently in progress.
    Sending,
    /// The write-half has been shut down.
    Closed,
}

impl Sink {
    /// Construct an open sink with a timeout for each send.
    const fn new(fd: Arc<OwnedFd>, timeout: Duration) -> Self {
        Self {
            fd,
            timeout,
            state: SinkState::Open,
        }
    }

    fn close(&mut self) {
        if matches!(self.state, SinkState::Closed) {
            return;
        }

        // Best-effort write-half shutdown so the peer can observe that no more
        // bytes will be sent after this sink becomes unusable.
        //
        // SAFETY: `self.fd` owns a live socket descriptor for the lifetime of
        // the sink. `shutdown` does not take ownership of the descriptor.
        unsafe {
            libc::shutdown(self.fd.as_raw_fd(), libc::SHUT_WR);
        }

        self.state = SinkState::Closed;
    }
}

impl Drop for Sink {
    fn drop(&mut self) {
        self.close();
    }
}

impl crate::Sink for Sink {
    async fn send(&mut self, bufs: impl Into<IoBufs> + Send) -> Result<(), Error> {
        match self.state {
            SinkState::Open => {}
            SinkState::Sending => {
                self.close();
                return Err(Error::Closed);
            }
            SinkState::Closed => return Err(Error::Closed),
        }

        let bufs = bufs.into();
        if !bufs.has_remaining() {
            return Ok(());
        }

        // Mark the sink as sending before awaiting so cancellation can be
        // detected by the next send.
        self.state = SinkState::Sending;

        let result = match Operation::register(Request::Send(SendRequest {
            fd: self.fd.clone(),
            write: bufs.into(),
            deadline: Some(Instant::now() + self.timeout),
        }))
        .await
        {
            Ok(RequestOutput::Send(result)) => result,
            Ok(_) => unreachable!("send request returned another output kind"),
            Err(_) => Err(Error::SendFailed),
        };

        // A failed send leaves the write-half unusable.
        if result.is_err() {
            self.close();
            return result;
        }

        // Mark the sink reusable on success.
        self.state = SinkState::Open;
        Ok(())
    }
}

/// Implementation of [crate::Stream] for an io-uring [Network].
///
/// Uses an internal buffer to reduce syscall overhead. Multiple small reads
/// can be satisfied from the buffer without additional network operations.
pub struct Stream {
    /// Shared socket descriptor backing this stream half.
    fd: Arc<OwnedFd>,
    /// Timeout budget for a top-level recv call.
    timeout: Duration,
    /// Tracks whether a previous recv failure has made this stream unusable.
    poisoned: bool,
    /// Internal read buffer.
    buffer: IoBufMut,
    /// Current read position in the buffer.
    buffer_pos: usize,
}

impl Stream {
    /// Construct a stream with an optional internal read buffer.
    fn new(fd: Arc<OwnedFd>, timeout: Duration, buffer_capacity: usize) -> Self {
        Self {
            fd,
            timeout,
            poisoned: false,
            buffer: IoBufMut::with_capacity(buffer_capacity),
            buffer_pos: 0,
        }
    }

    /// Submit a recv request to io_uring and wait for completion.
    ///
    /// `offset` is the byte offset into `buffer` where received data should
    /// start. `len` is the number of bytes to read starting at that offset.
    ///
    /// Returns the buffer and the number of bytes read by this invocation.
    /// Failed requests discard their buffers because the stream is poisoned.
    async fn submit_recv(
        &self,
        buffer: IoBufMut,
        offset: usize,
        len: usize,
        exact: bool,
        deadline: Instant,
    ) -> Result<(IoBufMut, usize), Error> {
        let output = Operation::register(Request::Recv(RecvRequest {
            fd: self.fd.clone(),
            buf: buffer,
            offset,
            len: offset + len,
            exact,
            deadline: Some(deadline),
        }))
        .await
        .map_err(|_| Error::RecvFailed)?;

        let RequestOutput::Recv(result) = output else {
            unreachable!("recv request returned another output kind");
        };

        // Request progress includes the bytes filled by earlier calls.
        result
            .map(|(buf, total)| (buf, total - offset))
            .map_err(|(_, error)| error)
    }

    /// Fills the internal buffer by reading from the socket via io_uring.
    async fn fill_buffer(&mut self, deadline: Instant) -> Result<(), Error> {
        self.buffer_pos = 0;

        let buffer = std::mem::take(&mut self.buffer);
        let len = buffer.capacity();

        let (buffer, read) = self.submit_recv(buffer, 0, len, false, deadline).await?;
        self.buffer = buffer;

        // SAFETY: The successful receive initialized the first `read` bytes.
        unsafe { self.buffer.set_len(read) };

        Ok(())
    }
}

impl crate::Stream for Stream {
    async fn recv(&mut self, len: usize) -> Result<IoBufs, Error> {
        if self.poisoned {
            return Err(Error::Closed);
        }

        // Pre-poison so that cancellation leaves the stream permanently closed
        // rather than silently corrupted.
        self.poisoned = true;

        let result = async {
            let mut owned_buf = IoBufMut::with_capacity(len);
            // SAFETY: `len` bytes are written by the recv loop below.
            unsafe { owned_buf.set_len(len) };
            let mut bytes_received = 0;
            let deadline = Instant::now() + self.timeout;

            while bytes_received < len {
                // First drain any buffered data
                let buffered = self.buffer.len() - self.buffer_pos;
                if buffered > 0 {
                    let to_copy = std::cmp::min(buffered, len - bytes_received);
                    owned_buf.as_mut()[bytes_received..bytes_received + to_copy].copy_from_slice(
                        &self.buffer.as_ref()[self.buffer_pos..self.buffer_pos + to_copy],
                    );
                    self.buffer_pos += to_copy;
                    bytes_received += to_copy;
                    continue;
                }

                let remaining = len - bytes_received;

                // Skip internal buffer if disabled, or if the read is large enough
                // to fill the buffer and immediately drain it
                let buffer_capacity = self.buffer.capacity();
                if buffer_capacity == 0 || remaining >= buffer_capacity {
                    let (buf, read) = self
                        .submit_recv(owned_buf, bytes_received, remaining, true, deadline)
                        .await?;
                    owned_buf = buf;
                    bytes_received += read;
                } else {
                    // Fill internal buffer, then loop will copy
                    self.fill_buffer(deadline).await?;
                }
            }

            Ok(IoBufs::from(owned_buf.freeze()))
        }
        .await;

        // Unpoison on success.
        if result.is_ok() {
            self.poisoned = false;
        }

        result
    }

    fn peek(&self, max_len: usize) -> &[u8] {
        let buffered = self.buffer.len() - self.buffer_pos;
        let len = std::cmp::min(buffered, max_len);
        &self.buffer.as_ref()[self.buffer_pos..self.buffer_pos + len]
    }
}

#[cfg(test)]
mod tests {
    use super::{Config, Network, Sink, Stream};
    use crate::{
        Clock as _, Error, IoBuf, IoBufMut, IoBufs, Listener as _, Network as _, Runner as _,
        Sink as _, Spawner as _, Stream as _, Supervisor as _, iouring, network::tests,
    };
    use commonware_macros::{select, test_group};
    use futures::FutureExt as _;
    use std::{
        io::{Read, Write},
        net::TcpStream,
        os::{
            fd::{AsRawFd, OwnedFd},
            unix::net::UnixStream,
        },
        pin::pin,
        sync::{
            Arc,
            atomic::{AtomicBool, Ordering},
        },
        task::{Context, Poll, Wake, Waker},
        time::{Duration, Instant},
    };

    #[derive(Default)]
    struct Notify(AtomicBool);

    impl Wake for Notify {
        fn wake(self: Arc<Self>) {
            self.0.store(true, Ordering::Relaxed);
        }
    }

    /// Read the TCP_NODELAY and SO_LINGER settings from a connected socket.
    fn socket_options(fd: &OwnedFd) -> (bool, Option<Duration>) {
        let stream = TcpStream::from(fd.try_clone().unwrap());
        let nodelay = stream.nodelay().unwrap();
        let mut linger = libc::linger {
            l_onoff: 0,
            l_linger: 0,
        };
        let mut len = size_of_val(&linger) as libc::socklen_t;

        // SAFETY: `fd` owns the socket and both output pointers refer to writable
        // storage. `len` gives the full size of the initialized linger value.
        let result = unsafe {
            libc::getsockopt(
                fd.as_raw_fd(),
                libc::SOL_SOCKET,
                libc::SO_LINGER,
                std::ptr::from_mut(&mut linger).cast(),
                &mut len,
            )
        };
        assert_eq!(result, 0);
        assert_eq!(len as usize, size_of_val(&linger));

        let linger =
            (linger.l_onoff != 0).then(|| Duration::from_secs(linger.l_linger.try_into().unwrap()));
        (nodelay, linger)
    }

    #[test]
    fn test_queued_accept_and_empty_operations_without_worker() {
        let network = Network::new(Config::default());
        let mut listener =
            futures::executor::block_on(network.bind("127.0.0.1:0".parse().unwrap())).unwrap();
        let peer = TcpStream::connect(listener.local_addr().unwrap()).unwrap();

        // A completed handshake is already in the accept queue. No current
        // worker exists, so success also proves no readiness request was polled.
        let (_, mut sink, mut stream) = futures::executor::block_on(listener.accept()).unwrap();
        assert!(stream.peek(1).is_empty());

        futures::executor::block_on(sink.send(IoBufs::default())).unwrap();
        futures::executor::block_on(sink.send(IoBuf::default())).unwrap();
        futures::executor::block_on(sink.send(Vec::<u8>::new())).unwrap();
        assert!(
            futures::executor::block_on(stream.recv(0))
                .unwrap()
                .is_empty()
        );
        drop(peer);
    }

    #[test]
    fn test_cancel_accept_before_service_preserves_next_connection() {
        iouring::Runner::new(
            iouring::Config::default().with_ring_config(iouring::RingConfig {
                size: 1,
                ..Default::default()
            }),
        )
        .start(|_| async {
            let network = Network::new(Config::default());
            let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
            let address = listener.local_addr().unwrap();

            // Cancel readiness before driver service. Its stale queue entry
            // must not interfere with the connect that reuses the waiter slot.
            assert!(futures::FutureExt::now_or_never(listener.accept()).is_none());
            let (mut sender, _receiver) = network.dial(address).await.unwrap();
            let (_, _sender, mut receiver) = listener.accept().await.unwrap();
            sender.send(b"kept").await.unwrap();
            assert_eq!(receiver.recv(4).await.unwrap().coalesce(), b"kept"[..]);
        });
    }

    #[test]
    fn test_pending_network_operations_observe_worker_closure() {
        let (send, recv, peer) = iouring::Runner::default().start(|_| async {
            let (socket, peer) = UnixStream::pair().unwrap();
            let fd: Arc<OwnedFd> = Arc::new(socket.into());
            let mut sink = Sink::new(fd.clone(), Duration::from_secs(1));
            let mut stream = Stream::new(fd, Duration::from_secs(1), 0);
            let mut send = Box::pin(async move { sink.send(b"x").await });
            let mut recv = Box::pin(async move { stream.recv(1).await });

            // Retain registered futures across shutdown, including their
            // resources, before polling their original worker's closed state.
            assert!(futures::poll!(send.as_mut()).is_pending());
            assert!(futures::poll!(recv.as_mut()).is_pending());

            (send, recv, peer)
        });

        assert!(matches!(
            futures::executor::block_on(send),
            Err(Error::SendFailed)
        ));
        assert!(matches!(
            futures::executor::block_on(recv),
            Err(Error::RecvFailed)
        ));
        drop(peer);
    }

    #[test]
    fn test_trait() {
        iouring::Runner::default().start(|context| async move {
            // Verify the io_uring backend satisfies the shared network trait suite.
            tests::test_network_trait(context, || {
                Network::new(Config {
                    read_write_timeout: Duration::from_secs(15),
                    ..Default::default()
                })
            })
            .await;
        });
    }

    #[test]
    fn test_connect_timeout() {
        iouring::Runner::default().start(|context| async move {
            let connect_timeout = Duration::from_millis(100);
            let network = Network::new(Config {
                connect_timeout,
                ..Default::default()
            });

            tests::test_network_connect_timeout(context, network, connect_timeout).await;
        });
    }

    #[test_group("slow")]
    #[test]
    fn test_stress_trait() {
        iouring::Runner::new(
            iouring::Config::default().with_ring_config(iouring::RingConfig {
                size: 256,
                ..Default::default()
            }),
        )
        .start(|context| async move {
            tests::stress_test_network_trait(context, || Network::new(Config::default())).await;
        });
    }

    #[test]
    fn test_read_timeout_with_partial_data() {
        iouring::Runner::default().start(|context| async move {
            let op_timeout = Duration::from_millis(100);
            let network = Network::new(Config {
                read_write_timeout: op_timeout,
                ..Default::default()
            });

            let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
            let addr = listener.local_addr().unwrap();

            let reader = context.child("reader").spawn(move |_| async move {
                let (_addr, _sink, mut stream) = listener.accept().await.unwrap();

                // Keep the call pending after a short read so expiry must
                // return an error instead of exposing a partial result.
                let start = Instant::now();
                let result = stream.recv(100).await;
                let elapsed = start.elapsed();

                // Failed buffered reads expose no partial data and cannot resume.
                assert!(stream.peek(100).is_empty());
                assert!(matches!(stream.recv(1).await, Err(Error::Closed)));

                (result, elapsed)
            });

            let (mut sink, _stream) = network.dial(addr).await.unwrap();
            sink.send([1u8, 2, 3, 4, 5].as_slice()).await.unwrap();

            let (result, elapsed) = reader.await.unwrap();
            assert!(matches!(result, Err(Error::Timeout)));
            assert!(elapsed >= op_timeout);

            // Allow some margin for scheduling and timer precision.
            assert!(elapsed < op_timeout * 3);
        });
    }

    #[test]
    fn test_carried_receive_deadline_completes_without_rescheduling() {
        iouring::Runner::default().start(|context| async move {
            let timeout = Duration::from_secs(1);
            let (socket, mut peer) = UnixStream::pair().unwrap();
            socket.set_nonblocking(true).unwrap();
            let mut observer = socket.try_clone().unwrap();
            let mut stream = Stream::new(Arc::new(socket.into()), timeout, 8);
            let notified = Arc::new(Notify::default());
            let waker = Waker::from(notified.clone());
            let mut cx = Context::from_waker(&waker);

            peer.write_all(b"x").unwrap();
            {
                let deadline = Instant::now() + timeout;
                let mut receive = pin!(stream.recv(2));
                assert!(receive.poll_unpin(&mut cx).is_pending());

                // Let the first refill complete without observing its result.
                while !notified.0.load(Ordering::Relaxed) {
                    assert!(Instant::now() < deadline, "first refill did not complete");
                    context.sleep(Duration::from_millis(1)).await;
                }
                assert!(Instant::now() < deadline);
                assert_eq!(
                    observer.read(&mut [0]).unwrap_err().kind(),
                    std::io::ErrorKind::WouldBlock
                );

                // The next refill carries the first call's deadline. A timer
                // wake proves the worker's cached time has passed that deadline.
                context.sleep(timeout).await;
                peer.write_all(b"y").unwrap();
                assert!(matches!(
                    receive.poll_unpin(&mut cx),
                    Poll::Ready(Err(Error::Timeout))
                ));
            }

            context.sleep(Duration::from_millis(1)).await;
            let mut remaining = [0];
            assert_eq!(observer.read(&mut remaining).unwrap(), 1);
            assert_eq!(&remaining, b"y");
        });
    }

    #[test]
    fn test_unbuffered_mode() {
        iouring::Runner::default().start(|context| async move {
            let network = Network::new(Config {
                read_buffer_size: 0,
                ..Default::default()
            });

            let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
            let addr = listener.local_addr().unwrap();

            let reader = context.child("reader").spawn(move |_| async move {
                let (_addr, _sink, mut stream) = listener.accept().await.unwrap();

                // Direct receives must leave no unread bytes in the stream buffer.
                assert!(stream.peek(100).is_empty());
                let buf1 = stream.recv(5).await.unwrap();
                assert!(stream.peek(100).is_empty());

                let buf2 = stream.recv(5).await.unwrap();
                assert!(stream.peek(100).is_empty());

                (buf1, buf2)
            });

            let (mut sink, _stream) = network.dial(addr).await.unwrap();
            sink.send([1u8, 2, 3, 4, 5].as_slice()).await.unwrap();
            sink.send([6u8, 7, 8, 9, 10].as_slice()).await.unwrap();

            let (buf1, buf2) = reader.await.unwrap();
            assert_eq!(buf1.coalesce(), &[1u8, 2, 3, 4, 5]);
            assert_eq!(buf2.coalesce(), &[6u8, 7, 8, 9, 10]);
        });
    }

    #[test]
    fn test_cancelled_recv_retains_descriptor_until_completion() {
        iouring::Runner::default().start(|context| async move {
            let op_timeout = Duration::from_millis(200);
            let network = Network::new(Config {
                read_write_timeout: op_timeout,
                ..Default::default()
            });

            let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
            let addr = listener.local_addr().unwrap();

            let (client_sink, mut client_stream) = network.dial(addr).await.unwrap();
            let (_addr, _server_sink, _server_stream) = listener.accept().await.unwrap();

            // Sink + stream + our clone.
            let fd = client_stream.fd.clone();
            assert_eq!(Arc::strong_count(&fd), 3);

            // Cancel a recv mid-flight (blocks because no data arrives).
            select! {
                _ = client_stream.recv(1) => unreachable!("no data was sent"),
                _ = context.sleep(Duration::from_millis(50)) => {},
            }

            // Cancellation cannot release the descriptor before the receive CQE.
            assert_eq!(Arc::strong_count(&fd), 4);

            // Only this test and the in-flight request retain the descriptor.
            drop(client_sink);
            drop(client_stream);
            assert_eq!(Arc::strong_count(&fd), 2);

            // Allow the worker to process cancellation and retire the request.
            context.sleep(op_timeout).await;
            assert_eq!(Arc::strong_count(&fd), 1);
        });
    }

    #[test]
    fn test_peek_with_buffered_data() {
        let (socket, mut peer) = UnixStream::pair().unwrap();
        let mut stream = Stream::new(Arc::new(socket.into()), Duration::from_secs(1), 64);
        assert!(stream.peek(100).is_empty());

        // Queue the full payload before receiving so the fill can read ahead.
        peer.write_all(b"hello world").unwrap();
        let mut stream = iouring::Runner::default().start(|_| async move {
            let first = stream.recv(5).await.unwrap();
            assert_eq!(first.coalesce(), b"hello");
            assert_eq!(stream.peek(100), b" world");

            // Peeking does not consume bytes and respects the requested limit.
            assert_eq!(stream.peek(100), b" world");
            assert_eq!(stream.peek(3), b" wo");
            assert!(stream.peek(0).is_empty());

            // A shorter refill must replace the previous readable extent.
            peer.write_all(b"xy").unwrap();
            assert_eq!(stream.recv(7).await.unwrap().coalesce(), b" worldx");
            assert_eq!(stream.peek(100), b"y");

            // Buffered prefixes and later refills must survive the direct path.
            let direct = [b'z'; 64];
            peer.write_all(&direct).unwrap();
            let received = stream.recv(65).await.unwrap().coalesce();
            assert_eq!(&received.as_ref()[..1], b"y");
            assert_eq!(&received.as_ref()[1..], &direct);
            assert!(stream.peek(100).is_empty());

            peer.write_all(b"next").unwrap();
            assert_eq!(stream.recv(2).await.unwrap().coalesce(), b"ne");
            assert_eq!(stream.peek(100), b"xt");
            stream
        });

        // Buffered bytes remain readable after the worker has shut down.
        let rest = futures::executor::block_on(stream.recv(2)).unwrap();
        assert_eq!(rest.coalesce(), b"xt");
        assert!(stream.peek(100).is_empty());
    }

    #[test]
    fn test_submit_recv_returns_bytes_for_this_call() {
        iouring::Runner::default().start(|_| async {
            let (left, mut right) = UnixStream::pair().unwrap();
            let stream = Stream::new(Arc::new(left.into()), Duration::from_secs(1), 0);

            // Preserve an existing two-byte prefix while receiving three more bytes.
            right.write_all(b"abc").unwrap();
            let buffer = IoBufMut::from(b"xy___");
            let result = stream
                .submit_recv(buffer, 2, 3, true, Instant::now() + Duration::from_secs(1))
                .await;

            // Report this call's progress while returning the whole buffer.
            let (buffer, read) = result.expect("submit_recv should succeed");
            assert_eq!(read, 3);
            assert_eq!(buffer.as_ref(), b"xyabc");
        });
    }

    #[test]
    fn test_large_recv_skips_internal_buffer() {
        iouring::Runner::default().start(|context| async move {
            let network = Network::new(Config {
                read_buffer_size: 8,
                ..Default::default()
            });

            let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
            let addr = listener.local_addr().unwrap();
            let expected = b"abcdefgh";

            // Accept one connection and issue a recv that exactly matches the
            // internal buffer size, forcing the direct-recv branch.
            let reader = context.child("reader").spawn(move |_| async move {
                let (_addr, _sink, mut stream) = listener.accept().await.unwrap();
                let received = stream.recv(expected.len()).await.unwrap();
                assert!(stream.peek(1).is_empty());
                received
            });

            let (mut sink, _stream) = network.dial(addr).await.unwrap();
            sink.send(expected).await.unwrap();

            assert_eq!(reader.await.unwrap().coalesce(), expected);
        });
    }

    #[test]
    fn test_socket_options_on_accept_and_dial() {
        iouring::Runner::default().start(|_| async {
            for (tcp_nodelay, zero_linger) in
                [(Some(true), true), (Some(false), false), (None, false)]
            {
                let network = Network::new(Config {
                    tcp_nodelay,
                    zero_linger,
                    ..Default::default()
                });
                let mut listener = network.bind("127.0.0.1:0".parse().unwrap()).await.unwrap();
                let address = listener.local_addr().unwrap();

                let (client_sink, _client_stream) = network.dial(address).await.unwrap();
                let (_, server_sink, _server_stream) = listener.accept().await.unwrap();

                // Both connection paths must apply the policy. Unconfigured
                // sockets retain the default Nagle and disabled-linger settings.
                let expected = (
                    tcp_nodelay.unwrap_or(false),
                    zero_linger.then_some(Duration::ZERO),
                );
                for fd in [&client_sink.fd, &server_sink.fd] {
                    assert_eq!(socket_options(fd), expected);
                }
            }
        });
    }
}