Skip to main content

melin_server_runtime/
reader.rs

1//! io_uring-based multiplexed reader with multishot RECV.
2//!
3//! Uses `IORING_OP_RECV` with `IORING_RECV_MULTISHOT` — a single SQE per
4//! connection produces multiple CQEs as data arrives, eliminating the
5//! resubmission overhead of standard RECV. Combined with a ring-mapped
6//! provided-buffer ring (`IOSQE_BUFFER_SELECT` + `buf_ring`), the kernel
7//! selects a buffer from a shared pool for each recv, and consumed
8//! buffers are recycled with a shared-memory store — no per-recv
9//! ProvideBuffers SQE/CQE round trip. Requires kernel ≥ 5.19.
10//!
11//! Uses a single reader thread — io_uring is efficient enough for hundreds
12//! of connections. New connections are registered via eventfd wakeup.
13//!
14//! Connection state is stored in a slab (index-stable Vec) so that io_uring
15//! user_data carries a slab index, not an fd. This avoids fd-reuse races
16//! where a recycled fd number could match a stale CQE.
17
18use std::collections::HashMap;
19use std::net::SocketAddr;
20use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
21use std::sync::Arc;
22use std::sync::atomic::{AtomicBool, Ordering};
23use std::sync::mpsc;
24use std::thread::JoinHandle;
25use std::time::{Duration, Instant};
26
27use io_uring::{IoUring, opcode, types};
28use tracing::{debug, error};
29
30use crate::ControlEvent;
31use crate::buf_ring::BufRing;
32use melin_app::Application;
33use melin_app::auth::Permission;
34use melin_app::decoder::RequestDecoder;
35
36/// Decoder type alias: request decoder bound to the application's `Event`
37/// type. Mirrors [`crate::response::ResponseEncoderArc`]; hides
38/// the `dyn RequestDecoder<Event = …>` spelling at call sites that thread
39/// the decoder through several functions.
40pub type RequestDecoderArc<A> = Arc<dyn RequestDecoder<Event = <A as Application>::Event>>;
41use melin_app::unix_epoch_nanos;
42use melin_pipeline::ring;
43use melin_transport_core::pipeline::InputSlot;
44
45/// Size of each provided buffer. 4 KiB accommodates multiple frames per
46/// recv (frames are typically <100 bytes).
47const BUF_SIZE: usize = 4096;
48
49/// Number of provided buffers in the shared pool. Must be a power of two
50/// (buf_ring ABI) and large enough for concurrent in-flight recvs across
51/// all connections. On exhaustion the kernel completes the multishot
52/// with `ENOBUFS` (no data consumed) and the loop re-arms it after the
53/// drain's recycles refill the ring. 2048 supports up to ~1024
54/// connections per reader thread; recycling is now a shared-memory store
55/// (no SQE), so raising this no longer interacts with `RING_SIZE`.
56const NUM_BUFFERS: u16 = 2048;
57
58/// Buffer group ID for the provided recv buffer pool.
59const BUF_GROUP_ID: u16 = 0;
60
61use crate::client_frames::MAX_FRAME_SIZE;
62
63/// io_uring submission queue depth. Power of 2, sized for up to ~1024
64/// connections per reader thread (multishot RECVs + eventfd read; buffer
65/// recycling goes through the buf_ring, not the SQ).
66const RING_SIZE: u32 = 4096;
67
68/// User data sentinel for the eventfd read SQE.
69const EVENTFD_TOKEN: u64 = u64::MAX;
70
71/// User data sentinel for legacy ProvideBuffers CQEs (fallback recycle
72/// path only). Best-effort re-provisions: we log errors but don't act
73/// on success.
74const PROVIDE_BUFS_TOKEN: u64 = u64::MAX - 1;
75
76/// User data sentinel for AsyncCancel SQEs (connection teardown and the
77/// shutdown quiesce). The completion carries no actionable information:
78/// `ENOENT`/`EALREADY` just mean the target op already finished.
79const CANCEL_TOKEN: u64 = u64::MAX - 3;
80
81/// User data sentinel for the tick timeout SQE. The reader arms a single
82/// `IORING_OP_TIMEOUT` per cadence so `submit_and_wait` returns at the tick
83/// deadline even when no client traffic is flowing. The CQE itself carries
84/// no information; the loop body checks `Instant::now()` against the next
85/// deadline and emits the actual `JournalEvent::Tick`.
86const TICK_TIMEOUT_TOKEN: u64 = u64::MAX - 2;
87
88/// CQE flag: buffer ID is valid in upper 16 bits of flags.
89const IORING_CQE_F_BUFFER: u32 = 1 << 0;
90
91/// CQE flag: more completions coming from this multishot operation.
92const IORING_CQE_F_MORE: u32 = 1 << 1;
93
94/// Bit shift to extract buffer ID from CQE flags.
95const IORING_CQE_BUFFER_SHIFT: u32 = 16;
96
97use melin_wire_protocol::control::ConnectionId;
98
99/// Command sent from the accept loop to a reader thread.
100pub struct ReaderRegistration<R> {
101    pub connection_id: ConnectionId,
102    pub reader: R,
103    pub addr: SocketAddr,
104    /// Permission level established during the auth handshake.
105    pub permission: Permission,
106    /// FxHash of the client's Ed25519 public key. Stored per-connection
107    /// and copied into every InputSlot for per-key idempotency dedup.
108    pub key_hash: u64,
109}
110
111/// Handle for the accept loop to register connections with the io_uring reader.
112pub struct UringReaderHandle<R> {
113    tx: mpsc::Sender<ReaderRegistration<R>>,
114    /// Wakeup eventfd, shared with the reader thread.
115    ///
116    /// `Arc<OwnedFd>` rather than a `RawFd` so the descriptor cannot be
117    /// closed while the other side still holds it: the close happens in
118    /// `OwnedFd`'s `Drop`, which runs when the last holder goes away, and
119    /// no code here is able to call `close` early because none of it owns
120    /// a raw descriptor to close.
121    ///
122    /// The reader thread used to close this itself while this handle was
123    /// still writing wakeups to it — a use-after-close whose real danger
124    /// was descriptor reuse, not `EBADF`: once the number is free, a
125    /// journal segment or an accepted connection can be assigned it, and
126    /// the next wakeup writes eight bytes into that instead. Found by
127    /// ThreadSanitizer.
128    event_fd: Arc<OwnedFd>,
129    join_handle: Option<JoinHandle<()>>,
130    shutdown: Arc<AtomicBool>,
131}
132
133impl<R> UringReaderHandle<R> {
134    /// Register a new connection with the reader thread.
135    ///
136    /// If the reader thread's channel is dead (thread panicked), logs an
137    /// error and signals shutdown so the server can restart cleanly.
138    pub fn register(&mut self, registration: ReaderRegistration<R>) {
139        if self.tx.send(registration).is_ok() {
140            // Signal the eventfd to wake the reader from io_uring_enter.
141            let val: u64 = 1;
142            unsafe {
143                libc::write(
144                    self.event_fd.as_raw_fd(),
145                    &val as *const u64 as *const libc::c_void,
146                    8,
147                );
148            }
149        } else {
150            error!("reader thread dead, cannot register connection");
151            self.shutdown.store(true, Ordering::Relaxed);
152        }
153    }
154
155    /// Signal the reader thread to shut down and wake it from io_uring_enter.
156    pub fn shutdown(&self) {
157        self.shutdown.store(true, Ordering::Relaxed);
158        let val: u64 = 1;
159        unsafe {
160            libc::write(
161                self.event_fd.as_raw_fd(),
162                &val as *const u64 as *const libc::c_void,
163                8,
164            );
165        }
166    }
167
168    /// Join the reader thread. Call after `shutdown()`.
169    pub fn join(mut self) {
170        if let Some(handle) = self.join_handle.take()
171            && let Err(panic) = handle.join()
172        {
173            let msg = panic
174                .downcast_ref::<&str>()
175                .copied()
176                .or_else(|| panic.downcast_ref::<String>().map(|s| s.as_str()))
177                .unwrap_or("<non-string panic>");
178            error!(message = msg, "reader thread panicked");
179        }
180    }
181}
182
183/// Spawn the io_uring reader thread. Returns a handle for registering
184/// connections.
185///
186/// One reader thread serves every TCP connection on the server. io_uring
187/// with multishot RECV multiplexes thousands of sockets efficiently and the
188/// matching stage is the throughput limit, so adding more reader threads
189/// would not raise throughput — it would only re-introduce contention on
190/// the input ring's multi-producer cursor.
191///
192/// `tick_cadence: Some(d)` makes the reader the engine's tick generator: it
193/// arms an `IORING_OP_TIMEOUT` so `submit_and_wait` returns at the tick
194/// deadline even when no client traffic is flowing, then publishes a
195/// `JournalEvent::Tick { now_ns }` onto the same input ring it uses for
196/// client requests. Pass `None` to disable the tick (useful for benchmarks
197/// that don't exercise time-driven features).
198pub fn spawn_reader<A: Application, R: AsRawFd + Send + 'static>(
199    producer: ring::Producer<InputSlot<A::Event>>,
200    decoder: Arc<dyn RequestDecoder<Event = A::Event>>,
201    control_tx: mpsc::Sender<ControlEvent>,
202    core: usize,
203    connection_timeout: Option<Duration>,
204    tick_cadence: Option<Duration>,
205    shutdown: Arc<AtomicBool>,
206) -> UringReaderHandle<R>
207where
208    A::Event: Send + Sync + 'static,
209{
210    let (tx, rx) = mpsc::channel();
211
212    let raw_event_fd = unsafe { libc::eventfd(0, libc::EFD_NONBLOCK) };
213    assert!(raw_event_fd >= 0, "eventfd creation failed");
214
215    // SAFETY: `eventfd` returned a fresh descriptor (asserted non-negative
216    // above) that nothing else owns, so transferring ownership to `OwnedFd`
217    // is sound. From here the raw number is never closed by hand — the
218    // descriptor lives exactly as long as the last `Arc` holder.
219    let event_fd = Arc::new(unsafe { OwnedFd::from_raw_fd(raw_event_fd) });
220    let wakeup_fd = Arc::clone(&event_fd);
221    let shutdown_clone = Arc::clone(&shutdown);
222
223    let handle = std::thread::Builder::new()
224        .name("uring-reader".into())
225        .spawn(move || {
226            // `core == 0` is the "do not pin" sentinel — see
227            // `crate::affinity` module docs.
228            if core == 0 {
229                tracing::info!(thread = "uring-reader", "thread left unpinned (core 0 sentinel)");
230            } else {
231                match melin_app::affinity::pin_to_core(core) {
232                    Ok(c) => {
233                        tracing::info!(thread = "uring-reader", core = c, "pinned to core")
234                    }
235                    Err(e) => tracing::warn!(thread = "uring-reader", core = core, error = %e, "failed to pin"),
236                }
237            }
238            reader_loop::<A, R>(
239                rx,
240                wakeup_fd,
241                producer,
242                &*decoder,
243                &control_tx,
244                connection_timeout,
245                tick_cadence,
246                &shutdown_clone,
247            );
248        })
249        .expect("failed to spawn uring reader thread");
250
251    UringReaderHandle {
252        tx,
253        event_fd,
254        join_handle: Some(handle),
255        shutdown,
256    }
257}
258
259// ---------------------------------------------------------------------------
260// Slab-based connection storage
261// ---------------------------------------------------------------------------
262
263/// Per-connection state for multishot io_uring recv + incremental frame parsing.
264struct ConnectionEntry<R> {
265    connection_id: u64,
266    addr: SocketAddr,
267    /// Permission level from auth handshake. Checked per-request on
268    /// the reader thread (cold path), zero cost on the matching engine.
269    permission: Permission,
270    /// FxHash of the client's Ed25519 public key. Copied into every
271    /// InputSlot for per-key idempotency dedup.
272    key_hash: u64,
273    /// Owned reader — keeps the fd alive. Dropping closes the fd.
274    _reader: R,
275    fd: RawFd,
276    /// Accumulated bytes not yet parsed into complete frames.
277    /// Grows when partial frames arrive, shrinks when frames are consumed.
278    parse_buf: Vec<u8>,
279    /// True if a multishot RecvMulti is currently active for this connection.
280    /// Multishot stays active until the kernel clears IORING_CQE_F_MORE
281    /// (e.g., buffer pool exhaustion, socket error, or EOF).
282    multishot_active: bool,
283    /// Last time any data was received on this connection. Used for
284    /// idle timeout detection.
285    last_activity: Instant,
286    /// Teardown has begun (malformed frame or idle timeout) while the
287    /// multishot RECV was still armed. The slab index must stay
288    /// allocated until the armed op's terminal CQE arrives — freeing it
289    /// early lets the LIFO free list hand the index to a new
290    /// registration while the kernel can still post CQEs carrying it,
291    /// and the old peer's bytes would be parsed under the new
292    /// connection's identity and permissions (audit review F1). CQEs
293    /// for a dying entry recycle their buffers and are otherwise
294    /// discarded.
295    dying: bool,
296}
297
298/// Index-stable allocator for connection state. Slab indices are used as
299/// io_uring user_data, avoiding fd-reuse races.
300struct ConnectionSlab<R> {
301    entries: Vec<Option<ConnectionEntry<R>>>,
302    /// Recycled indices for O(1) allocation.
303    free: Vec<usize>,
304}
305
306impl<R> ConnectionSlab<R> {
307    fn new() -> Self {
308        Self {
309            entries: Vec::with_capacity(256),
310            free: Vec::new(),
311        }
312    }
313
314    /// Insert a connection, returning its stable slab index.
315    fn insert(&mut self, entry: ConnectionEntry<R>) -> usize {
316        if let Some(idx) = self.free.pop() {
317            self.entries[idx] = Some(entry);
318            idx
319        } else {
320            let idx = self.entries.len();
321            self.entries.push(Some(entry));
322            idx
323        }
324    }
325
326    fn get_mut(&mut self, idx: usize) -> Option<&mut ConnectionEntry<R>> {
327        self.entries.get_mut(idx).and_then(|e| e.as_mut())
328    }
329
330    /// Remove and return a connection entry, recycling its index.
331    fn remove(&mut self, idx: usize) -> Option<ConnectionEntry<R>> {
332        if let Some(slot) = self.entries.get_mut(idx) {
333            let removed = slot.take();
334            if removed.is_some() {
335                self.free.push(idx);
336            }
337            removed
338        } else {
339            None
340        }
341    }
342}
343
344// ---------------------------------------------------------------------------
345// Main loop
346// ---------------------------------------------------------------------------
347
348/// How the reader should approach the kernel before draining the CQ.
349#[derive(Debug, Clone, Copy, PartialEq, Eq)]
350enum RingEntry {
351    /// Completions are visible and there is nothing to hand over — drain
352    /// from the mmap'd ring without a syscall.
353    Skip,
354    /// SQEs to submit, but completions are already visible: hand them
355    /// over and come straight back rather than asking to wait.
356    Submit,
357    /// Nothing to process yet — submit whatever is queued and park.
358    SubmitAndWait,
359}
360
361/// The reader's enter policy. Two rules: never park while completions
362/// are already visible, and never skip the syscall while SQEs are
363/// waiting to be handed over.
364#[inline]
365fn ring_entry(sq_pending: usize, cq_ready: bool) -> RingEntry {
366    match (sq_pending > 0, cq_ready) {
367        (false, true) => RingEntry::Skip,
368        (true, true) => RingEntry::Submit,
369        (_, false) => RingEntry::SubmitAndWait,
370    }
371}
372
373/// Main io_uring reader loop. Runs until channel disconnection.
374///
375/// When `tick_cadence` is `Some`, the loop also generates the engine's
376/// scheduler ticks — see [`spawn_reader`] for the rationale.
377fn reader_loop<A: Application, R: AsRawFd>(
378    command_rx: mpsc::Receiver<ReaderRegistration<R>>,
379    // Shared with `UringReaderHandle`. Taken by value so this thread keeps
380    // the descriptor alive for as long as it is armed in the ring, and
381    // releases it by dropping the `Arc` rather than by closing the fd.
382    wakeup_fd: Arc<OwnedFd>,
383    mut producer: ring::Producer<InputSlot<A::Event>>,
384    decoder: &dyn RequestDecoder<Event = A::Event>,
385    control_tx: &mpsc::Sender<ControlEvent>,
386    connection_timeout: Option<Duration>,
387    tick_cadence: Option<Duration>,
388    shutdown: &AtomicBool,
389) {
390    // Kernel-referenced memory is declared BEFORE the io_uring so it
391    // drops AFTER the ring on every exit path, including panic unwind
392    // (locals drop in reverse declaration order). The kernel holds live
393    // references into all three for as long as the ring fd is open:
394    // armed multishot RECVs select entries from the buf_ring and write
395    // into the pool, and the armed eventfd READ writes its buffer.
396
397    // Eventfd read buffer — boxed for pointer stability across SQE lifetimes.
398    let mut eventfd_buf: Box<[u8; 8]> = Box::new([0u8; 8]);
399
400    // Shared buffer pool for provided buffers. Contiguous allocation of
401    // NUM_BUFFERS × BUF_SIZE bytes. The kernel selects a buffer from this
402    // pool for each recv completion, identified by buffer ID in the CQE.
403    let mut buffer_pool = vec![0u8; NUM_BUFFERS as usize * BUF_SIZE].into_boxed_slice();
404
405    // Ring-mapped provided-buffer ring: recycling a consumed buffer is a
406    // shared-memory store, not a ProvideBuffers SQE — see `buf_ring`.
407    // Allocated unconditionally (32 KiB) so its declaration precedes the
408    // io_uring's even when the legacy fallback below ends up in use.
409    let mut buf_ring = BufRing::new(NUM_BUFFERS, buffer_pool.as_mut_ptr(), BUF_SIZE);
410
411    // SINGLE_ISSUER: this thread creates the ring and is the only one
412    // that ever submits — lets the kernel skip SQ locking, and turns any
413    // future cross-thread submission bug into an immediate EEXIST
414    // instead of a silent race. Same rationale as the journal and
415    // replication rings. (COOP_TASKRUN/DEFER_TASKRUN deliberately not
416    // set — see the journal ring's measured rationale in
417    // melin-transport-core::pipeline.)
418    let mut ring: IoUring = IoUring::builder()
419        .setup_single_issuer()
420        .build(RING_SIZE)
421        .expect("failed to create io_uring instance");
422
423    // Prefer the buf_ring; fall back to legacy ProvideBuffers SQEs if
424    // the kernel rejects the registration. Not theoretical: PBUF_RING
425    // needs kernel ≥ 5.19, and some virtualized hosts filter newer
426    // io_uring register opcodes (EINVAL) while reporting a modern
427    // uname. The fallback costs one SQE + one CQE per received chunk —
428    // degraded but fully functional, hence warn (see log conventions).
429    let use_buf_ring = match buf_ring.register(&ring, BUF_GROUP_ID) {
430        Ok(()) => true,
431        Err(e) => {
432            tracing::warn!(
433                error = %e,
434                "buf_ring registration rejected — falling back to legacy \
435                 ProvideBuffers recycling (kernel < 5.19, or a hypervisor \
436                 filtering io_uring register opcodes)"
437            );
438            register_buffer_pool(&mut ring, buffer_pool.as_mut_ptr());
439            false
440        }
441    };
442
443    let mut slab = ConnectionSlab::<R>::new();
444    // Reverse map for cleanup when a connection's fd needs removal.
445    // HashMap for O(1) lookup by fd. Sized for typical connection counts.
446    let mut fd_to_slab: HashMap<RawFd, usize> = HashMap::with_capacity(256);
447
448    // Pre-allocated CQE collection buffer. We must collect CQEs before
449    // processing because the CQ borrow must end before pushing new SQEs.
450    // Stores (user_data, result, flags) — flags needed for buffer ID and
451    // multishot continuation. Sized to the CQ depth (2× the SQ) so even
452    // a maximal drain never reallocates mid-loop.
453    let mut cqes: Vec<(u64, i32, u32)> = Vec::with_capacity(RING_SIZE as usize * 2);
454
455    // Submit the initial eventfd read so we wake on first connection.
456    // `eventfd_armed` mirrors whether that READ is pushed/armed — the
457    // kernel owes its CQE and writes `eventfd_buf` when it lands — and
458    // is maintained at every push/consume site so the teardown quiesce
459    // below can prove when the buffer is free of kernel references.
460    push_eventfd_read(&mut ring, wakeup_fd.as_raw_fd(), eventfd_buf.as_mut_ptr());
461    let mut eventfd_armed = true;
462
463    // Stage histograms via the global registry. `publish` is the
464    // narrow ring-publish call cost (lightweight, gated on
465    // `latency-trace`); `ingest` is the full per-frame reader cost
466    // and feeds the bench's tick-to-trade decomposition (heavier,
467    // gated on `tick-to-trade`).
468    #[cfg(feature = "latency-trace")]
469    let mut publish_rec =
470        melin_transport_core::trace::register_stage("reader: publish (decode → disruptor publish)");
471    #[cfg(feature = "tick-to-trade")]
472    let mut ingest_rec =
473        melin_transport_core::trace::register_stage("reader: ingest (recv_ts → publish complete)");
474    // Paces the recorder flush at the tail of each loop iteration. This
475    // thread parks in `submit_and_wait` when there is no traffic, so
476    // without an explicit flush its samples never reach the registry.
477    #[cfg(feature = "latency-trace")]
478    let mut last_stats_flush = Instant::now();
479
480    // Coarse gate for timeout scanning — avoids scanning on every
481    // submit_and_wait return during high throughput.
482    let mut last_timeout_scan = Instant::now();
483    // Pre-allocated buffer for stale connection indices to avoid
484    // heap allocation inside the hot loop.
485    let mut stale: Vec<usize> = Vec::new();
486
487    // Tick generator state. `next_tick_deadline` is the monotonic instant the
488    // next `JournalEvent::Tick` should fire. `last_tick_ns` enforces strict
489    // monotonicity on the wall-clock timestamps published in those events
490    // (NTP can step the wall clock backwards). `tick_armed` tracks whether
491    // an `IORING_OP_TIMEOUT` SQE is currently pending; we keep at most one.
492    //
493    // `tick_ts` lives across loop iterations because the kernel reads its
494    // bytes via the SQE's addr field at submit time, not at push time. If
495    // we declared it inside the `if !tick_armed` arm-timeout block, the
496    // value would be dropped before the `submit_and_wait` below — the
497    // kernel would then read freed stack memory. (See `md-gateway` for the
498    // same pattern: it stores Timespec as a long-lived struct field.)
499    let tick_enabled = tick_cadence.is_some();
500    let cadence = tick_cadence.unwrap_or(Duration::ZERO);
501    let mut next_tick_deadline = Instant::now() + cadence;
502    let mut last_tick_ns: u64 = 0;
503    let mut tick_armed = false;
504    // Arm the very first timeout here, before entering the loop. This both
505    // (a) makes the initial `tick_ts` value actually read by the kernel
506    // (silencing the unused-assignment lint, since rustc cannot see kernel
507    // pointer reads) and (b) ensures the first `submit_and_wait` returns at
508    // the cadence even if no client traffic ever arrives.
509    let mut tick_ts = types::Timespec::new()
510        .sec(cadence.as_secs())
511        .nsec(cadence.subsec_nanos());
512    if tick_enabled {
513        let sqe = opcode::Timeout::new(&tick_ts)
514            .build()
515            .user_data(TICK_TIMEOUT_TOKEN);
516        unsafe {
517            ring.submission()
518                .push(&sqe)
519                .expect("io_uring SQ full while arming initial tick timeout");
520        }
521        tick_armed = true;
522        tracing::info!(
523            cadence_ms = cadence.as_millis() as u64,
524            "tick generator integrated into reader thread"
525        );
526    }
527
528    loop {
529        if shutdown.load(Ordering::Relaxed) {
530            break;
531        }
532
533        // Tick generator: emit any due tick before sleeping again. Done before
534        // the timeout-arm so that a freshly-emitted tick re-arms a timeout for
535        // the *new* deadline.
536        if tick_enabled {
537            let now = Instant::now();
538            if now >= next_tick_deadline {
539                let raw_now_ns = unix_epoch_nanos();
540                let now_ns = melin_transport_core::tick::clamp_monotonic(raw_now_ns, last_tick_ns);
541                last_tick_ns = now_ns;
542                melin_transport_core::tick::publish_tick(&mut producer, now_ns);
543                // Catch up rather than burst-emit if we fell badly behind.
544                let elapsed = Instant::now().saturating_duration_since(next_tick_deadline);
545                next_tick_deadline = if elapsed > cadence {
546                    Instant::now() + cadence
547                } else {
548                    next_tick_deadline + cadence
549                };
550                // The previous timeout (if any) is now stale; let it fire and
551                // be ignored, then arm a new one below.
552                tick_armed = false;
553            }
554
555            if !tick_armed {
556                let remaining = next_tick_deadline.saturating_duration_since(Instant::now());
557                // Update the loop-scoped Timespec in place. The kernel reads
558                // it via the SQE's addr pointer on submit_and_wait below, so
559                // the binding must outlive that call (it does — outer scope).
560                tick_ts = types::Timespec::new()
561                    .sec(remaining.as_secs())
562                    .nsec(remaining.subsec_nanos());
563                let sqe = opcode::Timeout::new(&tick_ts)
564                    .build()
565                    .user_data(TICK_TIMEOUT_TOKEN);
566                unsafe {
567                    ring.submission()
568                        .push(&sqe)
569                        .expect("io_uring SQ full while arming tick timeout");
570                }
571                tick_armed = true;
572            }
573        }
574
575        // Enter the kernel only when there is a reason to.
576        //
577        // Under load the CQ usually already holds completions by the
578        // time we get here: in the ring's default task-work mode the
579        // kernel posts them while this thread is in userspace parsing.
580        // Reading that is pure userspace — the CQ is mmap'd and
581        // `completion()` loads the tail with `Acquire` — so when there
582        // is also nothing to hand over, the `io_uring_enter` is a
583        // ~200 ns mode switch that buys nothing. The replication sender
584        // measured the same skip at ~6 % of its thread's cycles
585        // (`tcp_sender.rs`); this loop makes one enter per drain.
586        //
587        // When there ARE SQEs to submit we still enter, but we do not
588        // ask the kernel to *wait* if completions are already visible.
589        // Only a genuinely empty CQ parks the thread.
590        //
591        // Note this is the half of the io_uring-audit item that pairs
592        // *against* `DEFER_TASKRUN`: deferred task work only runs on an
593        // enter with GETEVENTS, so under that flag the CQ would be empty
594        // here and the skip would simply never fire.
595        let sq_pending = ring.submission().len();
596        let cq_ready = !ring.completion().is_empty();
597        let entered = match ring_entry(sq_pending, cq_ready) {
598            RingEntry::Skip => Ok(0),
599            RingEntry::Submit => ring.submit(),
600            RingEntry::SubmitAndWait => ring.submit_and_wait(1),
601        };
602        match entered {
603            Ok(_) => {}
604            Err(ref e) if e.raw_os_error() == Some(libc::EINTR) => continue,
605            Err(e) => {
606                error!(error = %e, "io_uring submit/wait error");
607                break;
608            }
609        }
610
611        // Drain all available CQEs into the pre-allocated buffer.
612        // Must collect before processing because the CQ borrow must end
613        // before we can push new SQEs to the SQ.
614        cqes.clear();
615        cqes.extend(
616            ring.completion()
617                .map(|cqe| (cqe.user_data(), cqe.result(), cqe.flags())),
618        );
619
620        let batch_now = Instant::now();
621        // One wall-clock read per CQE batch instead of per request. The
622        // reader can see 4–6 M requests/s at peak; a per-request
623        // `unix_epoch_nanos()` was ~2.8 % of the primary's cycles
624        // (vDSO `clock_gettime(CLOCK_REALTIME)`). All requests in the
625        // same batch share the timestamp — precision loss is bounded
626        // by the CQE-drain cadence (tens of µs under load) and order
627        // timestamps are used for reporting, not matching (the engine
628        // orders by sequence, not time).
629        let batch_wall_ns = unix_epoch_nanos();
630
631        for &(token, result, flags) in &cqes {
632            // ── Tick timeout ──
633            // The CQE is just a wakeup signal — the actual tick emission
634            // happens at the top of the next loop iteration via the
635            // deadline check, so the time the tick is stamped with reflects
636            // unix_epoch_nanos at fire time, not at submit time.
637            if token == TICK_TIMEOUT_TOKEN {
638                tick_armed = false;
639                continue;
640            }
641
642            // ── Legacy ProvideBuffers completion (fallback mode only) ──
643            if token == PROVIDE_BUFS_TOKEN {
644                if result < 0 {
645                    error!(error = result, "ProvideBuffers failed");
646                }
647                continue;
648            }
649
650            // ── AsyncCancel completion ──
651            if token == CANCEL_TOKEN {
652                // Nothing to do: the cancelled op's own terminal CQE
653                // drives the state machine; ENOENT/EALREADY just mean
654                // it already finished on its own.
655                continue;
656            }
657
658            // ── Eventfd wakeup ──
659            if token == EVENTFD_TOKEN {
660                // This CQE is the armed READ completing; the re-arm at
661                // the end of the branch replaces it before anything can
662                // observe the gap, so `eventfd_armed` stays `true`
663                // through the pair. The teardown drain clears it for
664                // the one case where a completion is never processed
665                // here (a CQE still queued when the loop breaks).
666                if result >= 0 {
667                    // Process all pending registrations.
668                    while let Ok(reg) = command_rx.try_recv() {
669                        let fd = reg.reader.as_raw_fd();
670                        let entry = ConnectionEntry {
671                            connection_id: reg.connection_id.0,
672                            addr: reg.addr,
673                            permission: reg.permission,
674                            key_hash: reg.key_hash,
675                            fd,
676                            _reader: reg.reader,
677                            parse_buf: Vec::with_capacity(MAX_FRAME_SIZE + 4),
678                            multishot_active: false,
679                            last_activity: Instant::now(),
680                            dying: false,
681                        };
682                        let idx = slab.insert(entry);
683                        fd_to_slab.insert(fd, idx);
684
685                        // Submit multishot RECV for this connection.
686                        push_recv_multi(&mut ring, &mut slab, idx);
687                    }
688                } else {
689                    error!(error = result, "eventfd read error");
690                }
691
692                // Re-submit eventfd read for the next wakeup
693                // (`eventfd_armed` stays `true` — see above).
694                push_eventfd_read(&mut ring, wakeup_fd.as_raw_fd(), eventfd_buf.as_mut_ptr());
695                continue;
696            }
697
698            // ── Connection multishot RECV completion ──
699
700            let slab_idx = token as usize;
701            let has_more = (flags & IORING_CQE_F_MORE) != 0;
702
703            // A cleared F_MORE means THIS armed op posts no further
704            // CQEs — record it before any branch, so no early `continue`
705            // below can wedge the connection with a stale `true`.
706            if !has_more && let Some(entry) = slab.get_mut(slab_idx) {
707                entry.multishot_active = false;
708            }
709
710            // Extract the buffer (if any) before acting on `result`: a
711            // buffer can ride ANY recv CQE, and one that is not recycled
712            // is leaked from the pool forever. On 6.8 the kernel
713            // recycles internally before posting error/EOF CQEs, but the
714            // supported floor is 5.19, where that guarantee could not be
715            // established — the defensive recycle is one branch.
716            let buf_id = if (flags & IORING_CQE_F_BUFFER) != 0 {
717                Some((flags >> IORING_CQE_BUFFER_SHIFT) as usize)
718            } else {
719                None
720            };
721
722            // Dying connection: teardown began while its multishot was
723            // still armed (see `begin_teardown`). Consume its CQEs
724            // without parsing — the bytes belong to a repudiated peer —
725            // but keep recycling their buffers; free the slab index only
726            // at the terminal CQE, so it cannot be handed to a new
727            // registration while the kernel can still post CQEs carrying
728            // it. Checked before the disconnect branch so the cancel's
729            // -ECANCELED completion lands here, not there (which would
730            // emit a second Disconnected event).
731            if slab.get_mut(slab_idx).is_some_and(|e| e.dying) {
732                if let Some(bid) = buf_id {
733                    recycle_buffer(&mut ring, &mut buf_ring, use_buf_ring, &buffer_pool, bid);
734                }
735                if !has_more && let Some(dead) = slab.remove(slab_idx) {
736                    debug!(
737                        connection_id = dead.connection_id,
738                        "teardown complete, slab index released"
739                    );
740                }
741                continue;
742            }
743
744            if result == -libc::ENOBUFS {
745                // Pool exhausted at buffer-selection time: the multishot
746                // terminated WITHOUT reading — no data was lost, it sits
747                // in the socket buffer. This is not a client error and
748                // must not disconnect (a bare `result <= 0 ⇒ remove`
749                // here dropped innocent clients under burst). Re-arm:
750                // recycles from data CQEs earlier in this drain have
751                // already refilled the buf_ring (publication is
752                // immediate), and the re-arm SQE submits after the whole
753                // drain, so the retry finds buffers.
754                if let Some(bid) = buf_id {
755                    // No buffer accompanies a selection failure by
756                    // definition; recycle defensively if one appears.
757                    recycle_buffer(&mut ring, &mut buf_ring, use_buf_ring, &buffer_pool, bid);
758                }
759                if has_more {
760                    // Future-kernel guard: should the kernel ever keep
761                    // the multishot alive across ENOBUFS, arming a second
762                    // one would interleave two delivery streams into the
763                    // same parse buffer.
764                    continue;
765                }
766                push_recv_multi(&mut ring, &mut slab, slab_idx);
767                continue;
768            }
769
770            if result <= 0 {
771                // Disconnect (0) or error (negative errno) — terminal
772                // for the armed op, so the index is immediately safe to
773                // free. Recycle any buffer riding the CQE first.
774                if let Some(bid) = buf_id {
775                    recycle_buffer(&mut ring, &mut buf_ring, use_buf_ring, &buffer_pool, bid);
776                }
777                if let Some(removed) = slab.remove(slab_idx) {
778                    if result == 0 {
779                        debug!(
780                            connection_id = removed.connection_id,
781                            addr = %removed.addr,
782                            "client disconnected"
783                        );
784                    } else {
785                        debug!(
786                            connection_id = removed.connection_id,
787                            addr = %removed.addr,
788                            error = result,
789                            "recv error"
790                        );
791                    }
792                    fd_to_slab.remove(&removed.fd);
793                    let _ = control_tx.send(ControlEvent::Disconnected {
794                        connection_id: removed.connection_id,
795                    });
796                }
797                continue;
798            }
799
800            let n = result as usize;
801
802            // Trace timestamp: the moment the kernel handed us this recv's
803            // bytes. Captured once per CQE — not per frame — and stamped
804            // onto every InputSlot parsed from this buffer below, so the
805            // reader-ingest and server-e2e stages measure from true wire
806            // receipt (frame decode included) rather than re-sampling after
807            // each decode (which excluded decode and drifted forward for
808            // later frames in a multi-frame recv).
809            #[allow(clippy::let_unit_value)] // ZST when latency-trace is off
810            let recv_ts = melin_transport_core::trace::mono_trace_ns();
811
812            // Data CQEs always carry a buffer with buffer-select recvs.
813            // Defensive: nothing to copy or recycle without one, but the
814            // multishot bookkeeping already ran above, so a terminal
815            // no-buffer CQE re-arms instead of wedging the connection.
816            let Some(buf_id) = buf_id else {
817                debug!(slab_idx, "recv CQE without buffer flag");
818                if !has_more {
819                    push_recv_multi(&mut ring, &mut slab, slab_idx);
820                }
821                continue;
822            };
823
824            // Feed received bytes into the frame parser from the shared pool.
825            let action = if let Some(entry) = slab.get_mut(slab_idx) {
826                // Any successful recv resets the idle timeout.
827                entry.last_activity = batch_now;
828
829                // Copy received data from the shared buffer pool into the
830                // connection's parse buffer.
831                let buf_start = buf_id * BUF_SIZE;
832                entry
833                    .parse_buf
834                    .extend_from_slice(&buffer_pool[buf_start..buf_start + n]);
835
836                // Extract and publish complete frames.
837                let drop_conn = process_frames::<A, R>(
838                    entry,
839                    &mut producer,
840                    decoder,
841                    control_tx,
842                    batch_wall_ns,
843                    recv_ts,
844                    #[cfg(feature = "latency-trace")]
845                    &mut publish_rec,
846                    #[cfg(feature = "tick-to-trade")]
847                    &mut ingest_rec,
848                );
849                if drop_conn {
850                    Action::Remove
851                } else if !has_more {
852                    // Multishot terminated (buffer pool exhaustion or kernel
853                    // decision) but connection is healthy — resubmit.
854                    Action::Resubmit
855                } else {
856                    Action::None
857                }
858            } else {
859                // Stale CQE for a removed connection — ignore.
860                Action::None
861            };
862
863            // Recycle the consumed buffer. Must happen after the copy-out
864            // above — from this line on the kernel may write fresh recv
865            // data into the slot.
866            recycle_buffer(&mut ring, &mut buf_ring, use_buf_ring, &buffer_pool, buf_id);
867
868            match action {
869                Action::Remove => {
870                    // Deferred teardown, NOT an immediate slab free — the
871                    // multishot may still be armed and its index must not
872                    // be reused until the terminal CQE (audit review F1).
873                    begin_teardown(&mut ring, &mut slab, &mut fd_to_slab, control_tx, slab_idx);
874                }
875                Action::Resubmit => {
876                    push_recv_multi(&mut ring, &mut slab, slab_idx);
877                }
878                Action::None => {}
879            }
880        }
881
882        // Scan for idle connections that have exceeded the timeout.
883        // Coarse gate: only scan once per second to avoid unnecessary
884        // iteration during high-throughput phases when submit_and_wait
885        // returns immediately with CQEs.
886        if let Some(timeout) = connection_timeout {
887            let now = Instant::now();
888            if now.duration_since(last_timeout_scan) >= Duration::from_secs(1) {
889                last_timeout_scan = now;
890                stale.clear();
891                for (idx, slot) in slab.entries.iter().enumerate() {
892                    // Dying entries are already mid-teardown: their
893                    // `last_activity` stays stale by design, and a second
894                    // teardown would emit a duplicate Disconnected.
895                    if let Some(entry) = slot
896                        && !entry.dying
897                        && now.duration_since(entry.last_activity) > timeout
898                    {
899                        debug!(
900                            connection_id = entry.connection_id,
901                            addr = %entry.addr,
902                            "connection timed out"
903                        );
904                        stale.push(idx);
905                    }
906                }
907                for &idx in &stale {
908                    begin_teardown(&mut ring, &mut slab, &mut fd_to_slab, control_tx, idx);
909                }
910            }
911        }
912
913        // Hand buffered latency samples to the stats registry before
914        // parking in `submit_and_wait` at the top of the next
915        // iteration — once traffic stops this thread can sleep in the
916        // kernel indefinitely, which is precisely when the bench
917        // scrapes /stats-dump. Reuses the per-batch clock read.
918        #[cfg(feature = "latency-trace")]
919        if batch_now.duration_since(last_stats_flush)
920            >= melin_transport_core::trace::IDLE_FLUSH_INTERVAL
921        {
922            last_stats_flush = batch_now;
923            publish_rec.flush();
924            #[cfg(feature = "tick-to-trade")]
925            ingest_rec.flush();
926        }
927    }
928
929    // ── Teardown quiesce ────────────────────────────────────────────
930    // Armed operations hold kernel pointers into the buffer pool, the
931    // buf_ring memory, and the eventfd buffer. Closing the ring fd only
932    // triggers ASYNCHRONOUS cancellation (the kernel's ring-exit work),
933    // which can still be touching those allocations after this function
934    // returns and frees them — the same corruption class fixed on both
935    // replication transports (see `crate::uring_teardown`). Quiesce
936    // *provably*: wake every armed operation, then reap CQEs until none
937    // is owed, and leak the buffers if that cannot be shown within the
938    // deadline. "CQ went quiet" is not proof — a wake that silently
939    // failed leaves armed ops and a quiet CQ.
940    //
941    // The wake cannot rely on the cancel opcode alone: some hosts
942    // filter io_uring opcodes (the reason the buf_ring path has a
943    // legacy fallback), and a filtered cancel completes with EINVAL
944    // while the armed operations live on. So every operation that has
945    // to be proven down gets a wake it must answer: `shutdown(2)` on
946    // each connection socket completes its multishot RECV with EOF,
947    // and a wakeup write completes the armed eventfd READ. The cancel
948    // is still pushed as an accelerator where the host honours it. The
949    // tick timeout is not in the proof at all — see `owed` below.
950    // Panic unwind skips all of this and accepts the (tiny,
951    // process-is-dying) exit-work window; declaration order still
952    // closes the ring fd before the frees.
953    let proven = {
954        let cancel_all = opcode::AsyncCancel2::new(types::CancelBuilder::any())
955            .build()
956            .user_data(CANCEL_TOKEN);
957        unsafe {
958            // Ignore a full SQ: the wakes below do not depend on it.
959            let _ = ring.submission().push(&cancel_all);
960        }
961        for entry in slab.entries.iter().flatten() {
962            crate::uring_teardown::wake_pending_ops(entry.fd);
963        }
964        // Completes the armed eventfd READ into `eventfd_buf` (still
965        // alive). Best-effort: on failure the cancel is the fallback.
966        let wake: u64 = 1;
967        unsafe {
968            libc::write(
969                wakeup_fd.as_raw_fd(),
970                &wake as *const u64 as *const libc::c_void,
971                8,
972            );
973        }
974        // Flush SQEs pushed but never submitted (re-arms from the last
975        // batch, the cancel above): until they reach the kernel no CQE
976        // is coming for them, but their armed-state flags are already
977        // set, so abandoning them in the SQ would stall the proof
978        // below. On a submit error the flags stay set and the deadline
979        // routes to the leak path — the conservative outcome.
980        loop {
981            match ring.submit() {
982                Ok(_) => break,
983                Err(ref e) if e.raw_os_error() == Some(libc::EINTR) => continue,
984                Err(_) => break,
985            }
986        }
987        let mut backoff = crate::uring_teardown::DrainBackoff::new();
988        loop {
989            for cqe in ring.completion() {
990                match cqe.user_data() {
991                    EVENTFD_TOKEN => eventfd_armed = false,
992                    // None of these touch the allocations freed below,
993                    // so reaping them settles nothing: cancel results
994                    // carry no information, legacy ProvideBuffers
995                    // completions only registered addresses, and the
996                    // tick timeout reads nothing after submit (see
997                    // `owed`).
998                    PROVIDE_BUFS_TOKEN | CANCEL_TOKEN | TICK_TIMEOUT_TOKEN => {}
999                    // Connection tokens are slab indices.
1000                    idx => {
1001                        // A terminal CQE retires that connection's arm.
1002                        // Data CQEs landing mid-teardown are discarded
1003                        // and their buffers deliberately not recycled —
1004                        // nothing re-arms, so the pool cannot be
1005                        // selected from again once every arm is down.
1006                        if (cqe.flags() & IORING_CQE_F_MORE) == 0
1007                            && let Some(entry) = slab.get_mut(idx as usize)
1008                        {
1009                            entry.multishot_active = false;
1010                        }
1011                    }
1012                }
1013            }
1014            // `tick_armed` is deliberately absent: an armed
1015            // `IORING_OP_TIMEOUT` holds no pointer into any of the
1016            // allocations below. The kernel copies its `Timespec` out
1017            // of `tick_ts` when the SQE is *submitted* (which is why
1018            // that binding only has to outlive the submit — see its
1019            // declaration), and the ring's own exit work retires the
1020            // timer. Waiting for it would prove nothing and cost
1021            // plenty: nothing here can wake a timeout, so the only
1022            // things that retire one are it firing at its own cadence
1023            // — `--tick-interval-ms`, operator-set and unbounded — or
1024            // the cancel above, which is exactly what a host that
1025            // filters opcodes refuses. Gating the proof on it would
1026            // hand those hosts a full-deadline stall and a spurious
1027            // "leaking" warning on every clean shutdown.
1028            let owed = eventfd_armed || slab.entries.iter().flatten().any(|e| e.multishot_active);
1029            if !owed {
1030                break true;
1031            }
1032            if !backoff.wait() {
1033                break false;
1034            }
1035        }
1036    };
1037    if !proven {
1038        // The kernel may still write the pool or the eventfd buffer, or
1039        // read buf_ring entries. Leak all three rather than hand the
1040        // allocator memory the kernel still touches — a bounded one-off
1041        // cost on a thread that is exiting anyway. `mem::forget` on the
1042        // `BufRing` also skips its ring-memory dealloc, which is the
1043        // point.
1044        // Report what was still owed, not `tick_armed` — naming a
1045        // timer that was never part of the proof would point the next
1046        // reader at the wrong operation.
1047        tracing::warn!(
1048            eventfd_armed,
1049            armed_connections = slab
1050                .entries
1051                .iter()
1052                .flatten()
1053                .filter(|e| e.multishot_active)
1054                .count(),
1055            "io_uring teardown drain did not complete; leaking reader buffers"
1056        );
1057        std::mem::forget(buffer_pool);
1058        std::mem::forget(buf_ring);
1059        std::mem::forget(eventfd_buf);
1060    }
1061
1062    // No `close` here. This thread shares the eventfd with
1063    // `UringReaderHandle`, which writes wakeups to it, and closing it from
1064    // this side left that write racing a reused descriptor number. Dropping
1065    // the `Arc` at the end of this scope releases our share; the descriptor
1066    // itself closes when the handle drops too.
1067}
1068
1069/// What to do after processing a RECV CQE.
1070enum Action {
1071    /// Multishot terminated but connection healthy — resubmit RecvMulti.
1072    Resubmit,
1073    /// Connection should be torn down (malformed frame). Handled via
1074    /// `begin_teardown`, which reads what it needs from the slab entry.
1075    Remove,
1076    /// Multishot still active — nothing to do.
1077    None,
1078}
1079
1080// ---------------------------------------------------------------------------
1081// SQE helpers
1082// ---------------------------------------------------------------------------
1083
1084/// Register the provided buffer pool via a legacy ProvideBuffers op —
1085/// the fallback when buf_ring registration is rejected. Submits
1086/// synchronously and panics on failure — called once at startup, and
1087/// only after the preferred path already failed.
1088fn register_buffer_pool(ring: &mut IoUring, pool_ptr: *mut u8) {
1089    let sqe = opcode::ProvideBuffers::new(pool_ptr, BUF_SIZE as i32, NUM_BUFFERS, BUF_GROUP_ID, 0)
1090        .build()
1091        .user_data(PROVIDE_BUFS_TOKEN);
1092
1093    unsafe {
1094        ring.submission()
1095            .push(&sqe)
1096            .expect("io_uring SQ full during buffer pool registration");
1097    }
1098
1099    ring.submit_and_wait(1)
1100        .expect("io_uring submit failed during buffer pool registration");
1101
1102    // Check the completion result.
1103    let cqe = ring
1104        .completion()
1105        .next()
1106        .expect("no CQE after ProvideBuffers");
1107    assert!(cqe.result() >= 0, "ProvideBuffers failed: {}", cqe.result());
1108}
1109
1110/// Re-provide a single consumed buffer back to the pool (legacy fallback
1111/// mode). Pushed to SQ without immediate submission — batched with the
1112/// next submit_and_wait. Safe against SQ overflow only because at most
1113/// `NUM_BUFFERS` (< `RING_SIZE`) recycles can accumulate per drain.
1114fn re_provide_buffer(ring: &mut IoUring, pool_ptr: *mut u8, buf_id: usize) {
1115    let buf_ptr = unsafe { pool_ptr.add(buf_id * BUF_SIZE) };
1116    let sqe = opcode::ProvideBuffers::new(buf_ptr, BUF_SIZE as i32, 1, BUF_GROUP_ID, buf_id as u16)
1117        .build()
1118        .user_data(PROVIDE_BUFS_TOKEN);
1119
1120    unsafe {
1121        ring.submission()
1122            .push(&sqe)
1123            .expect("io_uring SQ full — increase RING_SIZE");
1124    }
1125}
1126
1127/// Return a consumed buffer to the shared pool, in whichever recycle
1128/// mode registration selected.
1129///
1130/// Hard assert, not debug: the bid comes from a kernel CQE, this path
1131/// runs per CQE (off the per-frame budget), and an out-of-range bid
1132/// handed back to the kernel would point a future recv's DMA at
1133/// arbitrary heap — the one failure mode worth an unconditional branch.
1134fn recycle_buffer(
1135    ring: &mut IoUring,
1136    buf_ring: &mut BufRing,
1137    use_buf_ring: bool,
1138    buffer_pool: &[u8],
1139    buf_id: usize,
1140) {
1141    assert!(
1142        buf_id < NUM_BUFFERS as usize,
1143        "kernel returned out-of-pool buffer id {buf_id}"
1144    );
1145    if use_buf_ring {
1146        buf_ring.push(buf_id as u16);
1147    } else {
1148        re_provide_buffer(ring, buffer_pool.as_ptr() as *mut u8, buf_id);
1149    }
1150}
1151
1152/// Begin tearing down a connection the reader decided to drop while its
1153/// multishot RECV may still be armed (malformed frame, idle timeout).
1154///
1155/// The slab index must NOT be freed yet: the kernel can still post CQEs
1156/// carrying it, and the slab's LIFO free list would hand the index to
1157/// the next registration — the old peer's bytes would then be parsed
1158/// under the new connection's identity, key hash, and permissions
1159/// (order-flow injection; audit review F1). Instead: sever the peer,
1160/// mark the entry dying, cancel the armed op, and let the terminal CQE
1161/// free the index (the `dying` branch of the CQE loop). When no op is
1162/// armed there is nothing that can post — free immediately.
1163fn begin_teardown<R>(
1164    ring: &mut IoUring,
1165    slab: &mut ConnectionSlab<R>,
1166    fd_to_slab: &mut HashMap<RawFd, usize>,
1167    control_tx: &mpsc::Sender<ControlEvent>,
1168    idx: usize,
1169) {
1170    let Some(entry) = slab.get_mut(idx) else {
1171        return;
1172    };
1173    if entry.dying {
1174        return;
1175    }
1176    let connection_id = entry.connection_id;
1177    let fd = entry.fd;
1178    // Sever the peer now. The fd itself must stay open until the armed
1179    // op completes (the kernel holds a file reference for it anyway);
1180    // shutdown(2) stops both directions immediately. Best-effort:
1181    // ENOTCONN just means the peer already went away.
1182    unsafe {
1183        libc::shutdown(fd, libc::SHUT_RDWR);
1184    }
1185    fd_to_slab.remove(&fd);
1186    // Dropped error: a dead control channel means the response stage is
1187    // gone and the server is shutting down.
1188    let _ = control_tx.send(ControlEvent::Disconnected { connection_id });
1189
1190    if entry.multishot_active {
1191        entry.dying = true;
1192        let sqe = opcode::AsyncCancel::new(idx as u64)
1193            .build()
1194            .user_data(CANCEL_TOKEN);
1195        unsafe {
1196            ring.submission()
1197                .push(&sqe)
1198                .expect("io_uring SQ full — increase RING_SIZE");
1199        }
1200    } else {
1201        // No armed op ⇒ no future CQEs can carry this index.
1202        slab.remove(idx);
1203    }
1204}
1205
1206/// Push a multishot RECV SQE for a connection. The kernel will produce
1207/// CQEs continuously until EOF, error, or buffer pool exhaustion —
1208/// no resubmission needed unless multishot terminates.
1209fn push_recv_multi<R>(ring: &mut IoUring, slab: &mut ConnectionSlab<R>, idx: usize) {
1210    let entry = match slab.get_mut(idx) {
1211        Some(e) => e,
1212        None => return,
1213    };
1214
1215    if entry.multishot_active {
1216        return;
1217    }
1218
1219    let sqe = opcode::RecvMulti::new(types::Fd(entry.fd), BUF_GROUP_ID)
1220        .build()
1221        .user_data(idx as u64);
1222
1223    unsafe {
1224        ring.submission()
1225            .push(&sqe)
1226            .expect("io_uring SQ full — increase RING_SIZE");
1227    }
1228    entry.multishot_active = true;
1229}
1230
1231/// Push a READ SQE for the eventfd (wakeup notification).
1232fn push_eventfd_read(ring: &mut IoUring, wakeup_fd: RawFd, buf: *mut u8) {
1233    let sqe = opcode::Read::new(types::Fd(wakeup_fd), buf, 8)
1234        .build()
1235        .user_data(EVENTFD_TOKEN);
1236
1237    unsafe {
1238        ring.submission()
1239            .push(&sqe)
1240            .expect("io_uring SQ full — increase RING_SIZE");
1241    }
1242}
1243
1244// ---------------------------------------------------------------------------
1245// Frame parsing
1246// ---------------------------------------------------------------------------
1247
1248/// Extract complete frames from the connection's parse buffer, decode them,
1249/// and publish to the disruptor. Returns `true` if the connection should be
1250/// dropped (e.g., oversized frame).
1251/// Extract complete frames from `conn.parse_buf` and publish them as
1252/// `InputSlot`s. `batch_wall_ns` is the wall-clock timestamp captured
1253/// once per CQE batch by the caller (see `reader_loop`); all non-query
1254/// requests published in this call share it, sparing the reader a
1255/// per-request `clock_gettime(CLOCK_REALTIME)` on the hot path. Returns
1256/// `true` if the connection should be dropped.
1257fn process_frames<A: Application, R>(
1258    conn: &mut ConnectionEntry<R>,
1259    producer: &mut ring::Producer<InputSlot<A::Event>>,
1260    decoder: &dyn RequestDecoder<Event = A::Event>,
1261    control_tx: &mpsc::Sender<ControlEvent>,
1262    batch_wall_ns: u64,
1263    recv_ts: melin_transport_core::trace::MonoTraceInstant,
1264    #[cfg(feature = "latency-trace")] publish_rec: &mut melin_transport_core::trace::StageRecorder,
1265    #[cfg(feature = "tick-to-trade")] ingest_rec: &mut melin_transport_core::trace::StageRecorder,
1266) -> bool {
1267    use crate::client_frames::{FrameAction, process_client_frames};
1268
1269    let action = process_client_frames(
1270        &mut conn.parse_buf,
1271        conn.connection_id,
1272        conn.key_hash,
1273        conn.permission,
1274        producer,
1275        decoder,
1276        batch_wall_ns,
1277        recv_ts,
1278        #[cfg(feature = "latency-trace")]
1279        publish_rec,
1280        #[cfg(feature = "tick-to-trade")]
1281        ingest_rec,
1282    );
1283
1284    match action {
1285        FrameAction::Continue => false,
1286        FrameAction::Disconnect => true,
1287        FrameAction::PipelineFull => {
1288            debug!(
1289                connection_id = conn.connection_id,
1290                "pipeline full, routing ServerBusy via response stage"
1291            );
1292            // The response stage owns ALL egress on a client socket. A
1293            // reader-side send here — however non-blocking — races the
1294            // response stage's own writes and can land between the two
1295            // halves of a partially-flushed response frame, permanently
1296            // desyncing the client's length-prefix framing (audit review
1297            // F3). It also kept a client-socket syscall on the ingress
1298            // thread. Routing through the control channel makes the
1299            // notice an ordinary send_buf append over there.
1300            //
1301            // Error deliberately dropped: a dead control channel means
1302            // the response stage is gone and the server is shutting
1303            // down — same reasoning as the Disconnected sends below.
1304            let _ = control_tx.send(ControlEvent::PipelineBusy {
1305                connection_id: conn.connection_id,
1306            });
1307            false
1308        }
1309    }
1310}
1311
1312#[cfg(test)]
1313mod tests {
1314    //! Unit tests for [`process_frames`]. The function has four exit paths
1315    //! (normal end, partial parse-buf, pipeline-full, oversize-frame), each
1316    //! with subtle batch-commit ordering requirements. These tests pin that
1317    //! behaviour against a synthetic decoder so refactors of the batch path
1318    //! (e.g. moving the batch up to span the whole CQE drain) can't silently
1319    //! regress the "earlier frames must be visible before ServerBusy /
1320    //! disconnect" guarantees.
1321    use super::*;
1322    use melin_app::auth::Permission;
1323    use melin_app::decoder::{Decoded, RequestDecoder};
1324    use melin_app::{AppEvent, Application, ApplyCtx, CodecError, RejectReason};
1325    use melin_journal::JournalEvent;
1326    use melin_pipeline::ring::DisruptorBuilder;
1327    use std::io::{ErrorKind, Read};
1328    use std::os::unix::net::UnixStream;
1329    use std::time::Duration;
1330
1331    /// The reader's enter policy. Each case is a syscall-per-drain
1332    /// decision on the hot path, and getting one wrong is either a lost
1333    /// wakeup (skipping with an empty CQ) or unsubmitted SQEs (skipping
1334    /// with a non-empty SQ) — both silent.
1335    mod ring_entry {
1336        use super::super::{RingEntry, ring_entry};
1337
1338        #[test]
1339        fn visible_completions_and_nothing_to_submit_need_no_syscall() {
1340            assert_eq!(ring_entry(0, true), RingEntry::Skip);
1341        }
1342
1343        #[test]
1344        fn queued_sqes_are_always_handed_over() {
1345            // Never skip with work in the SQ: those entries would sit
1346            // there until some later iteration happened to enter.
1347            assert_eq!(ring_entry(1, true), RingEntry::Submit);
1348            assert_eq!(ring_entry(8, false), RingEntry::SubmitAndWait);
1349        }
1350
1351        #[test]
1352        fn an_empty_cq_is_the_only_thing_that_parks_the_thread() {
1353            assert_eq!(ring_entry(0, false), RingEntry::SubmitAndWait);
1354            // ...and visible work never does, whatever the SQ holds.
1355            for sq in [0, 1, 64] {
1356                assert_ne!(ring_entry(sq, true), RingEntry::SubmitAndWait);
1357            }
1358        }
1359    }
1360
1361    /// Minimal `AppEvent` for these tests. `Copy` is required by `AppEvent`;
1362    /// the on-wire codec is unused because [`TagDecoder`] never invokes it
1363    /// (frames are interpreted directly from their tag byte).
1364    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
1365    enum TestEvent {
1366        Cmd(u8),
1367        Query,
1368    }
1369
1370    impl AppEvent for TestEvent {
1371        fn encoded_size(&self) -> usize {
1372            // Unused — the tests never round-trip through encode/decode.
1373            2
1374        }
1375        fn encode(&self, _buf: &mut [u8]) -> usize {
1376            unreachable!("process_frames does not encode app events")
1377        }
1378        fn decode(_buf: &[u8]) -> Result<Self, CodecError> {
1379            unreachable!("process_frames does not decode app events directly")
1380        }
1381        fn is_query(&self) -> bool {
1382            matches!(self, TestEvent::Query)
1383        }
1384    }
1385
1386    /// Placeholder `Application` impl. `process_frames` is generic over `A`
1387    /// only to constrain `A::Event` — none of the trait methods are called
1388    /// from the function under test, so they all `unreachable!`.
1389    struct TestApp;
1390
1391    impl Application for TestApp {
1392        type Event = TestEvent;
1393        type Report = ();
1394        type QueryResponse = ();
1395        const APP_VERSION: u16 = 0;
1396        fn apply(&mut self, _event: TestEvent, _ctx: &ApplyCtx, _out: &mut Vec<()>) -> Option<()> {
1397            unreachable!()
1398        }
1399        fn tick(&mut self, _now_ns: u64, _out: &mut Vec<()>) {
1400            unreachable!()
1401        }
1402        fn check_request_seq(&mut self, _key_hash: u64, _seq: u64) -> bool {
1403            unreachable!()
1404        }
1405        fn build_reject(_event: &TestEvent, _reason: RejectReason) -> () {
1406            unreachable!()
1407        }
1408        fn snapshot<W: std::io::Write>(&self, _w: &mut W) -> std::io::Result<()> {
1409            unreachable!()
1410        }
1411        fn restore<R: Read>(_r: &mut R) -> std::io::Result<Self> {
1412            unreachable!()
1413        }
1414    }
1415
1416    /// Stateless decoder that maps a frame's single payload byte to a
1417    /// [`Decoded`] outcome. Lets each test feed a precise mix of permitted,
1418    /// filtered, denied, and decode-error frames without standing up the
1419    /// real wire codec.
1420    ///
1421    /// Tag mapping (`0x00..=0xFB` map 1:1 to a Permitted seq, reserving the
1422    /// top four byte values for the non-Permitted outcomes):
1423    ///   * `0xFC` -> `Filter`
1424    ///   * `0xFD` -> `PermissionDenied`
1425    ///   * `0xFE` -> `DecodeError`
1426    ///   * `0xFF` -> `Permitted` with `is_query == true`
1427    ///   * `0x00..=0xFB` -> `Permitted` with `request_seq == byte`
1428    struct TagDecoder;
1429
1430    impl RequestDecoder for TagDecoder {
1431        type Event = TestEvent;
1432        fn decode(&self, bytes: &[u8], _permission: Permission) -> Decoded<TestEvent> {
1433            match bytes.first().copied() {
1434                None => Decoded::DecodeError("empty payload"),
1435                Some(0xFC) => Decoded::Filter,
1436                Some(0xFD) => Decoded::PermissionDenied("denied"),
1437                Some(0xFE) => Decoded::DecodeError("bad"),
1438                Some(0xFF) => Decoded::Permitted {
1439                    request_seq: 0xFF,
1440                    event: TestEvent::Query,
1441                },
1442                Some(b) => Decoded::Permitted {
1443                    request_seq: b as u64,
1444                    event: TestEvent::Cmd(b),
1445                },
1446            }
1447        }
1448    }
1449
1450    /// One-byte payload framed as `[u32 LE length=1][byte]`.
1451    fn frame(byte: u8) -> [u8; 5] {
1452        let mut f = [0u8; 5];
1453        f[..4].copy_from_slice(&1u32.to_le_bytes());
1454        f[4] = byte;
1455        f
1456    }
1457
1458    /// Length prefix announcing an oversize frame. No payload bytes follow —
1459    /// `process_frames` decides on the prefix alone, before waiting for the
1460    /// body.
1461    fn oversize_prefix() -> [u8; 4] {
1462        ((MAX_FRAME_SIZE as u32) + 1).to_le_bytes()
1463    }
1464
1465    /// Test fixture bundle. Grouped into a struct rather than returned as a
1466    /// 4-tuple to keep clippy happy (`type_complexity`) and to give each
1467    /// field a name at call sites.
1468    struct Fixture {
1469        conn: ConnectionEntry<UnixStream>,
1470        producer: ring::Producer<InputSlot<TestEvent>>,
1471        consumer: ring::Consumer<InputSlot<TestEvent>>,
1472        /// Client-side end of the socket pair — read from this to inspect
1473        /// any `ServerBusy` bytes the function under test writes.
1474        peer: UnixStream,
1475    }
1476
1477    /// Build a fresh fixture: a `ConnectionEntry` backed by a `UnixStream`
1478    /// pair plus a single-consumer disruptor of the requested capacity.
1479    fn make_fixture(ring_capacity: usize) -> Fixture {
1480        let (server_side, peer) = UnixStream::pair().expect("UnixStream::pair");
1481        // Short read timeout on the peer so assertions of "no ServerBusy
1482        // written" return promptly instead of hanging the test.
1483        peer.set_read_timeout(Some(Duration::from_millis(50)))
1484            .expect("set_read_timeout");
1485
1486        let entry = ConnectionEntry::<UnixStream> {
1487            connection_id: 7,
1488            addr: "127.0.0.1:1".parse().expect("addr parses"),
1489            permission: Permission::Trader,
1490            key_hash: 0xC0FFEE_u64,
1491            fd: server_side.as_raw_fd(),
1492            _reader: server_side,
1493            parse_buf: Vec::with_capacity(64),
1494            multishot_active: false,
1495            last_activity: Instant::now(),
1496            dying: false,
1497        };
1498
1499        let (producer, mut consumers) =
1500            DisruptorBuilder::<InputSlot<TestEvent>>::new(ring_capacity)
1501                .add_consumer()
1502                .build();
1503        let consumer = consumers.pop().expect("consumer present");
1504
1505        Fixture {
1506            conn: entry,
1507            producer,
1508            consumer,
1509            peer,
1510        }
1511    }
1512
1513    /// Invoke `process_frames::<TestApp, UnixStream>`, threading the
1514    /// feature-gated histogram args when the relevant features are on so
1515    /// the call compiles in every `cargo test` configuration.
1516    ///
1517    /// Returns the disconnect flag plus the control-channel receiver —
1518    /// pipeline-full now surfaces as a `PipelineBusy` event for the
1519    /// response stage rather than reader-side socket bytes, so busy
1520    /// assertions read the channel, not the peer.
1521    fn run_process_frames(
1522        conn: &mut ConnectionEntry<UnixStream>,
1523        producer: &mut ring::Producer<InputSlot<TestEvent>>,
1524    ) -> (bool, mpsc::Receiver<ControlEvent>) {
1525        #[cfg(feature = "latency-trace")]
1526        let mut publish_rec = melin_transport_core::trace::register_stage("test: publish");
1527        #[cfg(feature = "tick-to-trade")]
1528        let mut ingest_rec = melin_transport_core::trace::register_stage("test: ingest");
1529
1530        #[allow(clippy::let_unit_value)] // ZST when latency-trace is off
1531        let recv_ts = melin_transport_core::trace::mono_trace_ns();
1532
1533        let (control_tx, control_rx) = mpsc::channel();
1534        let disconnect = process_frames::<TestApp, UnixStream>(
1535            conn,
1536            producer,
1537            &TagDecoder,
1538            &control_tx,
1539            0xDEAD_BEEF,
1540            recv_ts,
1541            #[cfg(feature = "latency-trace")]
1542            &mut publish_rec,
1543            #[cfg(feature = "tick-to-trade")]
1544            &mut ingest_rec,
1545        );
1546        (disconnect, control_rx)
1547    }
1548
1549    /// Count `PipelineBusy` events for the fixture connection (id 7)
1550    /// queued on the control channel.
1551    fn busy_events(rx: &mpsc::Receiver<ControlEvent>) -> usize {
1552        let mut n = 0;
1553        while let Ok(event) = rx.try_recv() {
1554            if matches!(event, ControlEvent::PipelineBusy { connection_id } if connection_id == 7) {
1555                n += 1;
1556            }
1557        }
1558        n
1559    }
1560
1561    /// Drain `consumer` into a Vec of `(seq, slot)` until it yields `None`.
1562    /// Used to assert exact event sequences after `process_frames` returns.
1563    fn drain(
1564        consumer: &mut ring::Consumer<InputSlot<TestEvent>>,
1565    ) -> Vec<(u64, InputSlot<TestEvent>)> {
1566        let mut out = Vec::new();
1567        while let Some(pair) = consumer.try_consume() {
1568            out.push(pair);
1569        }
1570        out
1571    }
1572
1573    /// Try to read exactly 5 bytes (the ServerBusy frame size) from the
1574    /// peer. Returns `Some(bytes)` if the read completes within the peer's
1575    /// configured timeout, `None` if it times out (i.e. nothing was sent).
1576    fn read_server_busy(peer: &mut UnixStream) -> Option<[u8; 5]> {
1577        let mut buf = [0u8; 5];
1578        match peer.read_exact(&mut buf) {
1579            Ok(()) => Some(buf),
1580            Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => None,
1581            Err(e) => panic!("unexpected peer read error: {e}"),
1582        }
1583    }
1584
1585    #[test]
1586    fn process_frames_publishes_all_frames_after_single_commit() {
1587        // Capacity > number of frames — every frame must succeed and become
1588        // visible to the consumer after the trailing `batch.commit()`.
1589        let Fixture {
1590            mut conn,
1591            mut producer,
1592            mut consumer,
1593            mut peer,
1594        } = make_fixture(16);
1595        for byte in [0x01, 0x02, 0x03, 0x04, 0x05] {
1596            conn.parse_buf.extend_from_slice(&frame(byte));
1597        }
1598
1599        let (disconnect, control_rx) = run_process_frames(&mut conn, &mut producer);
1600        assert!(!disconnect, "no oversize frame ⇒ no disconnect");
1601
1602        let events = drain(&mut consumer);
1603        assert_eq!(events.len(), 5, "all 5 frames must be visible");
1604        for (i, (seq, slot)) in events.iter().enumerate() {
1605            assert_eq!(*seq, i as u64, "seq monotonic from 0");
1606            assert_eq!(slot.connection_id, 7);
1607            assert_eq!(slot.key_hash, 0xC0FFEE_u64);
1608            let byte = (i + 1) as u8;
1609            assert_eq!(slot.request_seq, byte as u64);
1610            assert_eq!(slot.event, JournalEvent::App(TestEvent::Cmd(byte)));
1611            // Non-query event ⇒ inherits the caller-supplied wall-clock.
1612            assert_eq!(slot.timestamp_ns, 0xDEAD_BEEF);
1613        }
1614        // Parse buffer fully consumed.
1615        assert!(conn.parse_buf.is_empty());
1616        // No Full happened: no busy event, and the reader never writes
1617        // the client socket (egress belongs to the response stage).
1618        assert_eq!(
1619            busy_events(&control_rx),
1620            0,
1621            "no PipelineBusy on the happy path"
1622        );
1623        assert!(
1624            read_server_busy(&mut peer).is_none(),
1625            "reader must never write the client socket"
1626        );
1627    }
1628
1629    /// The caller stamps `recv_ts` once per recv (at the kernel-return
1630    /// site) and every frame parsed from that buffer must carry that exact
1631    /// value. Guards against a regression to per-frame `recv_ts` capture,
1632    /// which excluded decode from the reader-ingest / server-e2e windows
1633    /// and drifted forward for later frames in a multi-frame recv. Gated on
1634    /// `latency-trace` because `recv_ts` is a real `u64` only then (`()`
1635    /// otherwise, leaving nothing to assert).
1636    #[cfg(feature = "latency-trace")]
1637    #[test]
1638    fn process_frames_stamps_every_slot_with_caller_recv_ts() {
1639        let Fixture {
1640            mut conn,
1641            mut producer,
1642            mut consumer,
1643            ..
1644        } = make_fixture(16);
1645        for byte in [0x01, 0x02, 0x03, 0x04] {
1646            conn.parse_buf.extend_from_slice(&frame(byte));
1647        }
1648
1649        // A recognizable sentinel the caller would have captured at the
1650        // recv site; distinct from the wall-clock stamp (0xDEAD_BEEF).
1651        const RECV_TS: u64 = 0x5EED_5EED;
1652
1653        let mut publish_rec = melin_transport_core::trace::register_stage("test: publish recv_ts");
1654        #[cfg(feature = "tick-to-trade")]
1655        let mut ingest_rec = melin_transport_core::trace::register_stage("test: ingest recv_ts");
1656
1657        let (control_tx, _control_rx) = mpsc::channel();
1658        let disconnect = process_frames::<TestApp, UnixStream>(
1659            &mut conn,
1660            &mut producer,
1661            &TagDecoder,
1662            &control_tx,
1663            0xDEAD_BEEF,
1664            RECV_TS,
1665            &mut publish_rec,
1666            #[cfg(feature = "tick-to-trade")]
1667            &mut ingest_rec,
1668        );
1669        assert!(!disconnect);
1670
1671        let events = drain(&mut consumer);
1672        assert_eq!(events.len(), 4, "all frames published");
1673        for (_seq, slot) in &events {
1674            assert_eq!(
1675                slot.recv_ts, RECV_TS,
1676                "every frame from one recv shares the single caller-supplied recv_ts"
1677            );
1678        }
1679    }
1680
1681    #[test]
1682    fn process_frames_rotates_batch_at_commit_every_cap() {
1683        // Push more events than `COMMIT_EVERY` (= 16) into a recv-cycle.
1684        // The cap must trigger at least one mid-loop commit so the
1685        // consumer sees the first capacity-many events before the
1686        // remainder lands. Validates the visibility-delay cap from the
1687        // perf branch — without it, all 32 events would commit together
1688        // and the first frame would wait for the 32nd to decode.
1689        //
1690        // Capacity 64 leaves room for the entire input (no Full); ring
1691        // backpressure is exercised separately in
1692        // `process_frames_partial_commit_then_server_busy_when_pipeline_full`.
1693        let Fixture {
1694            mut conn,
1695            mut producer,
1696            mut consumer,
1697            ..
1698        } = make_fixture(64);
1699        const EVENT_COUNT: usize = 32;
1700        for i in 0..EVENT_COUNT {
1701            // Use bytes 1..=32 (each ≤ 0xFB so TagDecoder yields
1702            // `Permitted` with `request_seq == byte`).
1703            conn.parse_buf.extend_from_slice(&frame((i + 1) as u8));
1704        }
1705
1706        let (disconnect, _control_rx) = run_process_frames(&mut conn, &mut producer);
1707        assert!(!disconnect, "no oversize / no Full ⇒ no disconnect");
1708
1709        let events = drain(&mut consumer);
1710        assert_eq!(events.len(), EVENT_COUNT, "every event visible");
1711        for (i, (seq, slot)) in events.iter().enumerate() {
1712            assert_eq!(*seq, i as u64, "seq contiguous across batch rotations");
1713            let byte = (i + 1) as u8;
1714            assert_eq!(slot.event, JournalEvent::App(TestEvent::Cmd(byte)));
1715        }
1716        assert!(conn.parse_buf.is_empty());
1717    }
1718
1719    #[test]
1720    fn process_frames_query_event_skips_wall_clock_stamp() {
1721        // `AppEvent::is_query` events bypass the journal stamp — verify
1722        // the timestamp is zeroed even when a non-zero batch_wall_ns was
1723        // supplied.
1724        let Fixture {
1725            mut conn,
1726            mut producer,
1727            mut consumer,
1728            ..
1729        } = make_fixture(8);
1730        conn.parse_buf.extend_from_slice(&frame(0xFF)); // tag → Query
1731
1732        let (disconnect, _control_rx) = run_process_frames(&mut conn, &mut producer);
1733        assert!(!disconnect);
1734
1735        let events = drain(&mut consumer);
1736        assert_eq!(events.len(), 1);
1737        let (_, slot) = &events[0];
1738        assert_eq!(slot.event, JournalEvent::App(TestEvent::Query));
1739        assert_eq!(
1740            slot.timestamp_ns, 0,
1741            "query events must skip the wall-clock stamp"
1742        );
1743    }
1744
1745    #[test]
1746    fn process_frames_partial_commit_then_server_busy_when_pipeline_full() {
1747        // Ring capacity 4 + 6 frames ⇒ first 4 commit, 5th triggers Full,
1748        // 6th is never reached because the loop breaks on Full. Validates:
1749        //   * `Err(Full)` does not roll back the prior 4 (single commit
1750        //     happens before the ServerBusy write).
1751        //   * The frame that triggered Full is silently dropped — its bytes
1752        //     are compacted out of `parse_buf` along with every earlier
1753        //     frame, mirroring pre-batch behaviour.
1754        //   * Exactly one PipelineBusy event is routed to the response
1755        //     stage, and nothing is written to the socket from here.
1756        let Fixture {
1757            mut conn,
1758            mut producer,
1759            mut consumer,
1760            mut peer,
1761        } = make_fixture(4);
1762        for byte in [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] {
1763            conn.parse_buf.extend_from_slice(&frame(byte));
1764        }
1765
1766        let (disconnect, control_rx) = run_process_frames(&mut conn, &mut producer);
1767        assert!(!disconnect, "Full does not drop the connection");
1768
1769        let events = drain(&mut consumer);
1770        assert_eq!(
1771            events.len(),
1772            4,
1773            "only the first capacity-many frames are visible"
1774        );
1775        for (i, (_, slot)) in events.iter().enumerate() {
1776            let byte = (i + 1) as u8;
1777            assert_eq!(slot.event, JournalEvent::App(TestEvent::Cmd(byte)));
1778        }
1779
1780        // The busy notice goes to the response stage — never directly
1781        // onto the socket, where it could tear a partially-flushed
1782        // response frame (audit review F3).
1783        assert_eq!(
1784            busy_events(&control_rx),
1785            1,
1786            "exactly one PipelineBusy routed"
1787        );
1788        assert!(
1789            read_server_busy(&mut peer).is_none(),
1790            "reader must never write the client socket"
1791        );
1792
1793        // The frame that triggered Full (0x05) had its bytes consumed by
1794        // the loop's `cursor +=` before `try_push_with` ran; the 6th frame
1795        // is never inspected because the loop broke. Compaction shifts
1796        // the unprocessed tail (the 6th frame's bytes) to the front.
1797        assert_eq!(
1798            conn.parse_buf,
1799            frame(0x06).to_vec(),
1800            "the 6th frame remains in parse_buf for the next recv-cycle"
1801        );
1802    }
1803
1804    /// Regression for the 2026-08 io_uring audit, finding 2 and review
1805    /// finding F3 (docs/internal/io-uring-audit-2026-08.md): the
1806    /// pipeline-full path must not touch the client socket at all. The
1807    /// original defect was a blocking `write(2)` of ServerBusy that
1808    /// parked the reader thread whenever the offender's socket buffer
1809    /// was also full — stalling ingress for *every* connection exactly
1810    /// on the overload path (against that code this test never
1811    /// returns). The busy notice now routes to the response stage,
1812    /// which also closes the frame-tearing race of any reader-side
1813    /// send. The socket-buffer fill stays: it proves the path is
1814    /// insensitive to socket state.
1815    #[test]
1816    fn pipeline_full_never_touches_the_client_socket() {
1817        let Fixture {
1818            mut conn,
1819            mut producer,
1820            mut consumer,
1821            mut peer,
1822        } = make_fixture(2);
1823
1824        // Fill the server→peer direction until the kernel refuses more —
1825        // the state a non-reading client leaves the connection in.
1826        let junk = [0u8; 4096];
1827        loop {
1828            let n = unsafe {
1829                libc::send(
1830                    conn.fd,
1831                    junk.as_ptr().cast(),
1832                    junk.len(),
1833                    libc::MSG_DONTWAIT,
1834                )
1835            };
1836            if n < 0 {
1837                assert_eq!(
1838                    std::io::Error::last_os_error().raw_os_error(),
1839                    Some(libc::EAGAIN),
1840                    "unexpected errno while filling socket"
1841                );
1842                break;
1843            }
1844        }
1845
1846        // Capacity 2 + 3 frames ⇒ the third triggers Full ⇒ ServerBusy.
1847        conn.parse_buf.extend_from_slice(&frame(0x01));
1848        conn.parse_buf.extend_from_slice(&frame(0x02));
1849        conn.parse_buf.extend_from_slice(&frame(0x03));
1850
1851        let start = std::time::Instant::now();
1852        let (disconnect, control_rx) = run_process_frames(&mut conn, &mut producer);
1853        // Generous bound to stay unflaky under CI load — the fixed path
1854        // returns in microseconds, the broken one only when the peer
1855        // reads (here: never).
1856        assert!(
1857            start.elapsed() < Duration::from_secs(1),
1858            "pipeline-full path blocked on a full socket"
1859        );
1860        assert!(!disconnect, "Full does not drop the connection");
1861        assert_eq!(
1862            drain(&mut consumer).len(),
1863            2,
1864            "the frames that fit are still published"
1865        );
1866        assert_eq!(
1867            busy_events(&control_rx),
1868            1,
1869            "busy notice routed to response stage"
1870        );
1871        // Nothing beyond the pre-fill junk arrives: drain it, then the
1872        // read must time out rather than yield a reader-written frame.
1873        let mut sink = [0u8; 4096];
1874        loop {
1875            match peer.read(&mut sink) {
1876                Ok(0) => panic!("unexpected EOF"),
1877                Ok(_) => continue,
1878                Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => {
1879                    break;
1880                }
1881                Err(e) => panic!("unexpected peer read error: {e}"),
1882            }
1883        }
1884    }
1885
1886    #[test]
1887    fn process_frames_oversize_commits_prior_frames_then_signals_disconnect() {
1888        // Two valid frames followed by an oversize length prefix must:
1889        //   * publish the two valid frames (commit-before-break) so the
1890        //     pipeline observes them even though we're about to tear the
1891        //     connection down,
1892        //   * return `true` so the caller drops the connection,
1893        //   * NOT emit PipelineBusy (that is reserved for pipeline-full).
1894        let Fixture {
1895            mut conn,
1896            mut producer,
1897            mut consumer,
1898            mut peer,
1899        } = make_fixture(16);
1900        conn.parse_buf.extend_from_slice(&frame(0x01));
1901        conn.parse_buf.extend_from_slice(&frame(0x02));
1902        conn.parse_buf.extend_from_slice(&oversize_prefix());
1903
1904        let (disconnect, control_rx) = run_process_frames(&mut conn, &mut producer);
1905        assert!(disconnect, "oversize frame must request disconnect");
1906
1907        let events = drain(&mut consumer);
1908        assert_eq!(
1909            events.len(),
1910            2,
1911            "prior frames are committed before the break"
1912        );
1913        assert_eq!(events[0].1.event, JournalEvent::App(TestEvent::Cmd(0x01)));
1914        assert_eq!(events[1].1.event, JournalEvent::App(TestEvent::Cmd(0x02)));
1915
1916        assert_eq!(
1917            busy_events(&control_rx),
1918            0,
1919            "PipelineBusy is emitted on Full, not on oversize"
1920        );
1921        assert!(
1922            read_server_busy(&mut peer).is_none(),
1923            "reader must never write the client socket"
1924        );
1925    }
1926
1927    #[test]
1928    fn process_frames_filters_denied_and_decode_errors_advance_cursor() {
1929        // Mixed batch: Permitted, Filter, PermissionDenied, DecodeError,
1930        // Permitted. Only the two Permitted frames must reach the
1931        // consumer; all bytes are consumed (parse_buf fully drains).
1932        let Fixture {
1933            mut conn,
1934            mut producer,
1935            mut consumer,
1936            ..
1937        } = make_fixture(16);
1938        for byte in [0x01, 0xFC, 0xFD, 0xFE, 0x02] {
1939            conn.parse_buf.extend_from_slice(&frame(byte));
1940        }
1941
1942        let (disconnect, _control_rx) = run_process_frames(&mut conn, &mut producer);
1943        assert!(!disconnect);
1944
1945        let events = drain(&mut consumer);
1946        assert_eq!(events.len(), 2);
1947        assert_eq!(events[0].1.event, JournalEvent::App(TestEvent::Cmd(0x01)));
1948        assert_eq!(events[1].1.event, JournalEvent::App(TestEvent::Cmd(0x02)));
1949        assert!(
1950            conn.parse_buf.is_empty(),
1951            "all bytes advanced past compaction"
1952        );
1953    }
1954
1955    #[test]
1956    fn process_frames_preserves_partial_trailing_frame() {
1957        // One complete frame followed by a truncated length prefix. The
1958        // complete frame must publish; the partial bytes must survive
1959        // compaction at the front of `parse_buf` for the next recv-cycle.
1960        let Fixture {
1961            mut conn,
1962            mut producer,
1963            mut consumer,
1964            ..
1965        } = make_fixture(16);
1966        conn.parse_buf.extend_from_slice(&frame(0x42));
1967        // Three of four length-prefix bytes — `cursor + 4 <= len()` is
1968        // false, so the loop breaks before consuming anything from the
1969        // partial.
1970        conn.parse_buf.extend_from_slice(&[0xDE, 0xAD, 0xBE]);
1971
1972        let (disconnect, _control_rx) = run_process_frames(&mut conn, &mut producer);
1973        assert!(!disconnect);
1974
1975        let events = drain(&mut consumer);
1976        assert_eq!(events.len(), 1);
1977        assert_eq!(events[0].1.event, JournalEvent::App(TestEvent::Cmd(0x42)));
1978        assert_eq!(
1979            conn.parse_buf,
1980            vec![0xDE, 0xAD, 0xBE],
1981            "partial length prefix preserved for next recv-cycle"
1982        );
1983    }
1984
1985    #[test]
1986    fn process_frames_empty_buffer_is_noop() {
1987        // No bytes in parse_buf ⇒ loop never enters; commit is the
1988        // documented zero-slot no-op; no busy event; no disconnect.
1989        let Fixture {
1990            mut conn,
1991            mut producer,
1992            mut consumer,
1993            mut peer,
1994        } = make_fixture(4);
1995
1996        let (disconnect, control_rx) = run_process_frames(&mut conn, &mut producer);
1997        assert!(!disconnect);
1998        assert_eq!(drain(&mut consumer).len(), 0);
1999        assert!(conn.parse_buf.is_empty());
2000        assert_eq!(busy_events(&control_rx), 0);
2001        assert!(read_server_busy(&mut peer).is_none());
2002    }
2003
2004    /// End-to-end soak through the real reader loop: 4 connections
2005    /// concurrently write 10 000 frames each, one `write(2)` per frame,
2006    /// so the receive path churns through the shared buffer pool and
2007    /// its recycle machinery under genuine concurrency (buf_ring where
2008    /// the kernel supports it, legacy ProvideBuffers otherwise — this
2009    /// test is the coverage for whichever mode the host engages).
2010    ///
2011    /// The failure signatures of a recycle bug are exactly what is
2012    /// asserted: a torn or misordered frame (a buffer reused while the
2013    /// kernel still owned it), a lost frame, or a spurious disconnect
2014    /// (`ENOBUFS` mishandled as a client error).
2015    #[test]
2016    fn reader_loop_soak_delivers_every_frame_in_order() {
2017        use std::io::Write as _;
2018
2019        const CONNS: u64 = 4;
2020        const FRAMES: u32 = 10_000;
2021
2022        // Capacity ≥ total frames: even if this thread is descheduled
2023        // and stops draining, the ring cannot fill, so the reader never
2024        // sheds load (which would legitimately drop frames and turn a
2025        // CI hiccup into a false failure).
2026        let (producer, mut consumers) = DisruptorBuilder::<InputSlot<TestEvent>>::new(65536)
2027            .add_consumer()
2028            .build();
2029        let mut consumer = consumers.pop().expect("consumer");
2030        let (control_tx, control_rx) = mpsc::channel();
2031        let shutdown = Arc::new(AtomicBool::new(false));
2032
2033        let mut handle = spawn_reader::<TestApp, UnixStream>(
2034            producer,
2035            Arc::new(TagDecoder),
2036            control_tx,
2037            0,    // "do not pin" sentinel
2038            None, // no idle timeout — a CI stall must not disconnect
2039            None, // no tick generator
2040            Arc::clone(&shutdown),
2041        );
2042
2043        let mut writers = Vec::new();
2044        for id in 0..CONNS {
2045            let (client, server_side) = UnixStream::pair().expect("socketpair");
2046            handle.register(ReaderRegistration {
2047                connection_id: ConnectionId(id),
2048                reader: server_side,
2049                addr: "127.0.0.1:1".parse().expect("addr"),
2050                permission: Permission::Trader,
2051                key_hash: id,
2052            });
2053            writers.push(std::thread::spawn(move || {
2054                let mut client = client;
2055                for i in 0..FRAMES {
2056                    let byte = (i % 200 + 1) as u8;
2057                    client.write_all(&frame(byte)).expect("client write");
2058                    // Brief pauses fragment the stream so the reader
2059                    // sees many small recvs (heavy buffer churn) rather
2060                    // than a few large coalesced ones.
2061                    if i % 512 == 0 {
2062                        std::thread::sleep(Duration::from_micros(200));
2063                    }
2064                }
2065                client // keep the socket alive until after the drain
2066            }));
2067        }
2068
2069        // Drain until every frame arrived or the deadline passes. The
2070        // deadline is generous — the run takes well under a second on
2071        // an idle machine — because a genuine recycle deadlock must
2072        // fail loudly, not hang CI.
2073        let deadline = Instant::now() + Duration::from_secs(60);
2074        let mut per_conn_count = [0u32; CONNS as usize];
2075        let mut total: u64 = 0;
2076        while total < CONNS * FRAMES as u64 {
2077            assert!(
2078                Instant::now() < deadline,
2079                "soak stalled: {total} of {} frames after 60s ({per_conn_count:?})",
2080                CONNS * FRAMES as u64
2081            );
2082            while let Some((_seq, slot)) = consumer.try_consume() {
2083                let conn = slot.connection_id as usize;
2084                assert!(conn < CONNS as usize, "unknown connection id");
2085                let expected = (per_conn_count[conn] % 200 + 1) as u8;
2086                assert_eq!(
2087                    slot.event,
2088                    JournalEvent::App(TestEvent::Cmd(expected)),
2089                    "conn {conn} frame {} out of order or corrupted",
2090                    per_conn_count[conn]
2091                );
2092                per_conn_count[conn] += 1;
2093                total += 1;
2094            }
2095            std::hint::spin_loop();
2096        }
2097
2098        // Any control event at this point is a defect: sockets are
2099        // still alive (held by the writer join handles) and the ring
2100        // never filled. Every arm diverges, so inspecting the first
2101        // queued event suffices.
2102        if let Ok(event) = control_rx.try_recv() {
2103            match event {
2104                ControlEvent::Disconnected { connection_id } => {
2105                    panic!("spurious disconnect of connection {connection_id}")
2106                }
2107                ControlEvent::PipelineBusy { connection_id } => {
2108                    panic!("spurious pipeline-full for connection {connection_id}")
2109                }
2110                ControlEvent::Connected { .. } => {
2111                    unreachable!("reader never sends Connected")
2112                }
2113            }
2114        }
2115
2116        let _clients: Vec<UnixStream> = writers
2117            .into_iter()
2118            .map(|w| w.join().expect("writer thread"))
2119            .collect();
2120        handle.shutdown();
2121        handle.join();
2122    }
2123
2124    /// Shutdown with live, armed connections must quiesce the ring
2125    /// provably and promptly. The armed multishot RECVs sit on sockets
2126    /// whose peers never disconnect, so nothing completes them on its
2127    /// own — the teardown's wakes (per-connection `shutdown(2)`, the
2128    /// eventfd write) are what retire them, including on hosts that
2129    /// filter the AsyncCancel opcode, where the old cancel-then-quiet
2130    /// teardown could conclude "done" with arms still live against the
2131    /// soon-freed pool. A drain that cannot prove quiescence only
2132    /// returns at the full deadline (and leaks the buffers), so
2133    /// returning well inside it is the proof this test can observe.
2134    #[test]
2135    fn shutdown_quiesces_armed_connections_promptly() {
2136        use std::io::Write as _;
2137
2138        const CONNS: u64 = 3;
2139        let (producer, mut consumers) = DisruptorBuilder::<InputSlot<TestEvent>>::new(1024)
2140            .add_consumer()
2141            .build();
2142        let mut consumer = consumers.pop().expect("consumer");
2143        let (control_tx, _control_rx) = mpsc::channel();
2144        let shutdown = Arc::new(AtomicBool::new(false));
2145        let mut handle = spawn_reader::<TestApp, UnixStream>(
2146            producer,
2147            Arc::new(TagDecoder),
2148            control_tx,
2149            0,    // "do not pin" sentinel
2150            None, // no idle timeout
2151            None, // no tick generator
2152            Arc::clone(&shutdown),
2153        );
2154
2155        let mut clients = Vec::new();
2156        for id in 0..CONNS {
2157            let (mut client, server_side) = UnixStream::pair().expect("socketpair");
2158            handle.register(ReaderRegistration {
2159                connection_id: ConnectionId(id),
2160                reader: server_side,
2161                addr: "127.0.0.1:1".parse().expect("addr"),
2162                permission: Permission::Trader,
2163                key_hash: id,
2164            });
2165            // One delivered frame per connection proves its multishot
2166            // is armed and active before the shutdown fires.
2167            client.write_all(&frame(7)).expect("client write");
2168            clients.push(client);
2169        }
2170        let deadline = Instant::now() + Duration::from_secs(30);
2171        let mut seen = 0u64;
2172        while seen < CONNS {
2173            assert!(
2174                Instant::now() < deadline,
2175                "frames never arrived ({seen}/{CONNS})"
2176            );
2177            while consumer.try_consume().is_some() {
2178                seen += 1;
2179            }
2180            std::hint::spin_loop();
2181        }
2182
2183        // The clients stay alive across the shutdown: no peer
2184        // disconnect can complete the armed RECVs — only the teardown's
2185        // own wakes can.
2186        let started = Instant::now();
2187        handle.shutdown();
2188        handle.join();
2189        let elapsed = started.elapsed();
2190        assert!(
2191            elapsed < crate::uring_teardown::DRAIN_TIMEOUT / 4,
2192            "reader teardown took {elapsed:?}, close to the drain deadline — \
2193             armed operations were not woken"
2194        );
2195        drop(clients);
2196    }
2197
2198    /// Regression for audit review F1
2199    /// (docs/internal/io-uring-audit-2026-08.md): a connection torn down
2200    /// while its multishot RECV is still armed must not have its slab
2201    /// index reused while the kernel can still post CQEs carrying it.
2202    /// Pre-fix, the LIFO free list handed client A's index straight to
2203    /// client B; A's socket stayed open (the armed op holds a file
2204    /// reference past the fd close), and A's continued bytes were parsed
2205    /// under B's connection id, key hash, and permissions — order-flow
2206    /// injection under someone else's identity. Post-fix, A's entry dies
2207    /// in place until the cancelled op's terminal CQE, so B gets a fresh
2208    /// index and A's post-teardown bytes are discarded.
2209    #[test]
2210    fn torn_down_connection_cannot_inject_frames_into_a_reused_slot() {
2211        use std::io::Write as _;
2212
2213        let (producer, mut consumers) = DisruptorBuilder::<InputSlot<TestEvent>>::new(65536)
2214            .add_consumer()
2215            .build();
2216        let mut consumer = consumers.pop().expect("consumer");
2217        let (control_tx, control_rx) = mpsc::channel();
2218        let shutdown = Arc::new(AtomicBool::new(false));
2219        let mut handle = spawn_reader::<TestApp, UnixStream>(
2220            producer,
2221            Arc::new(TagDecoder),
2222            control_tx,
2223            0,
2224            None,
2225            None,
2226            Arc::clone(&shutdown),
2227        );
2228
2229        const A_ID: u64 = 100;
2230        const B_ID: u64 = 200;
2231
2232        // Client A: valid frames (0x01..=0x08), an oversize prefix (the
2233        // teardown trigger), then a sustained stream of would-be
2234        // injection frames from A's byte range.
2235        let (a_client, a_server) = UnixStream::pair().expect("socketpair");
2236        handle.register(ReaderRegistration {
2237            connection_id: ConnectionId(A_ID),
2238            reader: a_server,
2239            addr: "127.0.0.1:1".parse().expect("addr"),
2240            permission: Permission::Trader,
2241            key_hash: A_ID,
2242        });
2243        let a_writer = std::thread::spawn(move || {
2244            let mut a = a_client;
2245            for byte in 0x01..=0x08u8 {
2246                let _ = a.write_all(&frame(byte));
2247            }
2248            let _ = a.write_all(&oversize_prefix());
2249            // Keep writing after the reader repudiates us. Write errors
2250            // (EPIPE once teardown's shutdown(2) lands) are the fix
2251            // doing its job — ignore them and keep trying.
2252            let stop = Instant::now() + Duration::from_millis(300);
2253            while Instant::now() < stop {
2254                let _ = a.write_all(&frame(0x05));
2255                std::thread::sleep(Duration::from_micros(50));
2256            }
2257            a
2258        });
2259
2260        // Wait for A's teardown announcement, then register B — the
2261        // instant a buggy free list would hand B the still-armed index.
2262        let deadline = Instant::now() + Duration::from_secs(10);
2263        loop {
2264            match control_rx.try_recv() {
2265                Ok(ControlEvent::Disconnected { connection_id }) if connection_id == A_ID => break,
2266                Ok(_) => {}
2267                Err(_) => {
2268                    assert!(Instant::now() < deadline, "no Disconnected for A");
2269                    std::thread::sleep(Duration::from_millis(1));
2270                }
2271            }
2272        }
2273
2274        const B_FRAMES: u32 = 2_000;
2275        let (b_client, b_server) = UnixStream::pair().expect("socketpair");
2276        handle.register(ReaderRegistration {
2277            connection_id: ConnectionId(B_ID),
2278            reader: b_server,
2279            addr: "127.0.0.1:2".parse().expect("addr"),
2280            permission: Permission::Trader,
2281            key_hash: B_ID,
2282        });
2283        let b_writer = std::thread::spawn(move || {
2284            let mut b = b_client;
2285            for i in 0..B_FRAMES {
2286                let byte = 0x21 + (i % 0x10) as u8; // 0x21..=0x30, disjoint from A
2287                b.write_all(&frame(byte)).expect("B write");
2288                if i % 128 == 0 {
2289                    std::thread::sleep(Duration::from_micros(100));
2290                }
2291            }
2292            b
2293        });
2294
2295        // Drain until all of B's frames arrive. Every B-attributed slot
2296        // must carry B's key hash and a B-range byte; anything else is
2297        // the injection this test exists to prevent.
2298        let deadline = Instant::now() + Duration::from_secs(30);
2299        let mut b_seen = 0u32;
2300        while b_seen < B_FRAMES {
2301            assert!(
2302                Instant::now() < deadline,
2303                "stalled: {b_seen}/{B_FRAMES} B frames"
2304            );
2305            while let Some((_seq, slot)) = consumer.try_consume() {
2306                match slot.event {
2307                    JournalEvent::App(TestEvent::Cmd(byte)) => {
2308                        if slot.connection_id == B_ID {
2309                            assert_eq!(slot.key_hash, B_ID, "B slot with foreign key hash");
2310                            assert!(
2311                                (0x21..=0x30).contains(&byte),
2312                                "byte {byte:#x} injected into B's stream"
2313                            );
2314                            b_seen += 1;
2315                        } else {
2316                            assert_eq!(slot.connection_id, A_ID, "unknown connection id");
2317                            assert!(
2318                                (0x01..=0x08).contains(&byte),
2319                                "unexpected byte {byte:#x} on A"
2320                            );
2321                        }
2322                    }
2323                    ref other => panic!("unexpected event {other:?}"),
2324                }
2325            }
2326            std::hint::spin_loop();
2327        }
2328
2329        let _a = a_writer.join().expect("A writer");
2330        let _b = b_writer.join().expect("B writer");
2331        handle.shutdown();
2332        handle.join();
2333    }
2334}