subc-daemon 0.20.23

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Test-gated walking skeleton for route-local request dispatch.
//!
//! This module is deliberately excluded from default builds. It proves the concurrency
//! mechanism against the real frame, sink, and credit types without changing daemon routing.
//! Run the model-checked test with
//! `cargo test -p subc-core dispatch_spike --features loom`; the package build script scopes
//! `cfg(loom)` to this crate because a workspace-wide setting disables Tokio networking.
//!
//! # Why it is still dormant, and what would promote it
//!
//! It exists to answer one question ahead of the work: can cancellation reach a request
//! that is already queued behind a saturated serial route? Today it cannot -- the connection
//! read loop blocks on the route's credit semaphore, so a CANCEL for a queued request waits
//! behind the request it is trying to cancel. This skeleton demonstrates the fix (per-route
//! bounded queues with a single drain owner) without touching daemon routing, so the
//! mechanism could be model-checked before anything depended on it.
//!
//! It stays dormant because the starvation it addresses has not been observed in production;
//! the cost of the redesign is currently larger than the harm. That is a judgement about
//! today's evidence, not a permanent verdict -- a reproducible case of a cancel failing to
//! land on a busy serial route is what would justify wiring it, and the mechanism is proven
//! and waiting when that arrives.
//!
//! Recorded because dormancy and abandonment look identical from the outside: a reader
//! finding unreferenced code cannot tell whether it is waiting or forgotten, and deleting
//! a proven mechanism is expensive to undo.
//!
//! # Single-writer invariant
//!
//! [`RouteDrain`] owns the only `FrameSink` for a route and is consumed by one drain task.
//! Read-loop actors can only enqueue cancellation intent through [`RouteDispatcher`]; they can
//! never write module-bound frames directly. Therefore the drain's Request send completes before
//! it dequeues and sends a CANCEL for that Delivered correlation.

use std::{
    collections::{HashMap, VecDeque},
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex, Weak,
    },
};

use subc_protocol::{Frame, FrameType};
use tokio::sync::{mpsc, Notify, Semaphore};

use crate::{forwarding::ChannelFlow, router::FrameSink};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Admission {
    Open,
    Closing,
    Closed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SlotState {
    Queued,
    Sending { cancelled: bool },
    Delivered,
}

#[derive(Debug)]
struct Slot {
    frame: Frame,
    state: SlotState,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PushOutcome {
    Admitted,
    DuplicateCorr,
    Rejected(Admission),
    Backpressure,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CancelDecision {
    SynthesizeCancelled,
    DeferredToDrain,
    ForwardToModule,
    QueuedForDrain,
    DrainStopped,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum CommitDecision {
    Proceed(Frame),
    RollbackCancelled,
    Missing,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TeardownKind {
    None,
    Reloading,
    Goodbye,
    ConnectionClose,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SyntheticKind {
    Cancelled,
    ModuleReloading,
    BackendError,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SyntheticTerminal {
    pub corr: u64,
    pub kind: SyntheticKind,
}

/// The synchronized decision core. Callers hold the sole route-inbox mutex.
#[derive(Debug)]
pub(crate) struct RouteInbox {
    admission: Admission,
    queue: VecDeque<u64>,
    slots: HashMap<u64, Slot>,
    depth_cap: usize,
    teardown: TeardownKind,
}

impl RouteInbox {
    pub(crate) fn new(depth_cap: usize) -> Self {
        Self {
            admission: Admission::Open,
            queue: VecDeque::new(),
            slots: HashMap::new(),
            depth_cap,
            teardown: TeardownKind::None,
        }
    }

    pub(crate) fn push_request(&mut self, corr: u64, frame: Frame) -> PushOutcome {
        if self.slots.contains_key(&corr) {
            return PushOutcome::DuplicateCorr;
        }
        if self.admission != Admission::Open {
            return PushOutcome::Rejected(self.admission);
        }
        if self.slots.len() >= self.depth_cap {
            return PushOutcome::Backpressure;
        }

        self.slots.insert(
            corr,
            Slot {
                frame,
                state: SlotState::Queued,
            },
        );
        self.queue.push_back(corr);
        PushOutcome::Admitted
    }

    /// Atomically removes the FIFO head and marks it Sending under the same lock.
    pub(crate) fn pop_for_dispatch(&mut self) -> Option<u64> {
        let corr = self.queue.pop_front()?;
        let Some(slot) = self.slots.get_mut(&corr) else {
            // A queued cancellation leaves one O(1) tombstone. The drain discards at most
            // one tombstone per call instead of scanning or indexing a missing slot.
            return None;
        };
        if slot.state != SlotState::Queued {
            return None;
        }
        slot.state = SlotState::Sending { cancelled: false };
        Some(corr)
    }

    pub(crate) fn on_cancel(&mut self, corr: u64) -> CancelDecision {
        let Some(state) = self.slots.get(&corr).map(|slot| slot.state) else {
            return CancelDecision::ForwardToModule;
        };
        match state {
            SlotState::Queued => {
                self.slots.remove(&corr);
                CancelDecision::SynthesizeCancelled
            }
            SlotState::Sending { .. } => {
                if let Some(slot) = self.slots.get_mut(&corr) {
                    slot.state = SlotState::Sending { cancelled: true };
                }
                CancelDecision::DeferredToDrain
            }
            SlotState::Delivered => CancelDecision::ForwardToModule,
        }
    }

    /// Commits ownership to the terminal path before the module send can observe the frame.
    pub(crate) fn commit_delivered(&mut self, corr: u64) -> CommitDecision {
        let Some(state) = self.slots.get(&corr).map(|slot| slot.state) else {
            return CommitDecision::Missing;
        };
        match state {
            SlotState::Sending { cancelled: true } => {
                self.slots.remove(&corr);
                CommitDecision::RollbackCancelled
            }
            SlotState::Sending { cancelled: false } if self.admission != Admission::Closed => {
                let slot = self
                    .slots
                    .get_mut(&corr)
                    .expect("slot existence was checked under the same mutex");
                slot.state = SlotState::Delivered;
                CommitDecision::Proceed(slot.frame.clone())
            }
            SlotState::Sending { cancelled: false } => {
                self.slots.remove(&corr);
                CommitDecision::RollbackCancelled
            }
            SlotState::Queued | SlotState::Delivered => CommitDecision::Missing,
        }
    }

    /// Returns true only for the first terminal of an outstanding delivered request.
    pub(crate) fn on_terminal(&mut self, corr: u64) -> bool {
        if matches!(
            self.slots.get(&corr).map(|slot| slot.state),
            Some(SlotState::Delivered)
        ) {
            self.slots.remove(&corr);
            true
        } else {
            false
        }
    }

    pub(crate) fn begin_closing(&mut self) {
        if self.admission == Admission::Open {
            self.admission = Admission::Closing;
        }
    }

    /// Closes admission and removes queued slots; Sending/Delivered ownership stays explicit.
    pub(crate) fn finish_closed(&mut self) -> Vec<u64> {
        self.admission = Admission::Closed;
        let mut queued = Vec::new();
        while let Some(corr) = self.queue.pop_front() {
            if matches!(
                self.slots.get(&corr).map(|slot| slot.state),
                Some(SlotState::Queued)
            ) {
                self.slots.remove(&corr);
                queued.push(corr);
            }
        }
        queued
    }

    fn set_teardown(&mut self, kind: TeardownKind) {
        self.teardown = kind;
    }

    fn on_acquire_closed(&mut self, corr: u64) -> Option<TeardownKind> {
        if matches!(
            self.slots.get(&corr).map(|slot| slot.state),
            Some(SlotState::Sending { .. })
        ) {
            self.slots.remove(&corr);
            Some(self.teardown)
        } else {
            None
        }
    }

    fn queue_is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    fn admission(&self) -> Admission {
        self.admission
    }

    #[cfg(test)]
    fn slot_count(&self) -> usize {
        self.slots.len()
    }

    #[cfg(test)]
    fn state(&self, corr: u64) -> Option<SlotState> {
        self.slots.get(&corr).map(|slot| slot.state)
    }
}

trait CreditRelease {
    fn release_credit(&self);
}

impl CreditRelease for Arc<ChannelFlow> {
    fn release_credit(&self) {
        self.release();
    }
}

/// One acquired credit. Dropping rolls back; committing transfers release to a terminal slot.
struct CreditToken<F: CreditRelease> {
    flow: F,
    armed: bool,
}

impl<F: CreditRelease> CreditToken<F> {
    fn new(flow: F) -> Self {
        Self { flow, armed: true }
    }

    fn commit(mut self) {
        self.armed = false;
    }
}

impl<F: CreditRelease> Drop for CreditToken<F> {
    fn drop(&mut self) {
        if self.armed {
            self.flow.release_credit();
        }
    }
}

/// Thin synchronized wrapper around [`RouteInbox`]. It is not wired into the daemon.
#[derive(Debug)]
pub(crate) struct RouteDispatcher {
    inbox: Mutex<RouteInbox>,
    notify: Notify,
    flow: Arc<ChannelFlow>,
    cancel_tx: mpsc::UnboundedSender<Frame>,
    synthetic_sink: mpsc::UnboundedSender<SyntheticTerminal>,
}

/// Consumed by one task, making that task the route's only module-sink writer.
#[derive(Debug)]
pub(crate) struct RouteDrain {
    dispatcher: Arc<RouteDispatcher>,
    module_sink: FrameSink,
    cancel_rx: mpsc::UnboundedReceiver<Frame>,
    send_gate: Option<Arc<SendGate>>,
}

/// Test synchronization hook that parks the first Request before it reaches the real sink.
#[derive(Debug)]
pub(crate) struct SendGate {
    armed: AtomicBool,
    entered: Notify,
    release: Semaphore,
}

impl SendGate {
    pub(crate) fn new() -> Arc<Self> {
        Arc::new(Self {
            armed: AtomicBool::new(true),
            entered: Notify::new(),
            release: Semaphore::new(0),
        })
    }

    pub(crate) async fn wait_until_parked(&self) {
        self.entered.notified().await;
    }

    pub(crate) fn release(&self) {
        self.release.add_permits(1);
    }

    async fn park_if_first_request(&self, frame_type: FrameType) {
        if frame_type == FrameType::Request && self.armed.swap(false, Ordering::AcqRel) {
            self.entered.notify_one();
            let permit = self
                .release
                .acquire()
                .await
                .expect("test send gate must remain open");
            permit.forget();
        }
    }
}

impl RouteDispatcher {
    pub(crate) fn new(
        depth_cap: usize,
        flow: Arc<ChannelFlow>,
        module_sink: FrameSink,
        synthetic_sink: mpsc::UnboundedSender<SyntheticTerminal>,
    ) -> (Arc<Self>, RouteDrain) {
        let (cancel_tx, cancel_rx) = mpsc::unbounded_channel();
        let dispatcher = Arc::new(Self {
            inbox: Mutex::new(RouteInbox::new(depth_cap)),
            notify: Notify::new(),
            flow,
            cancel_tx,
            synthetic_sink,
        });
        let drain = RouteDrain {
            dispatcher: Arc::clone(&dispatcher),
            module_sink,
            cancel_rx,
            send_gate: None,
        };
        (dispatcher, drain)
    }

    pub(crate) fn push_request(&self, corr: u64, frame: Frame) -> PushOutcome {
        let outcome = self
            .inbox
            .lock()
            .expect("route inbox mutex poisoned")
            .push_request(corr, frame);
        if outcome == PushOutcome::Admitted {
            self.notify.notify_one();
        }
        outcome
    }

    /// Records cancellation synchronously; module forwarding is always delegated to the drain.
    pub(crate) fn submit_cancel(&self, frame: Frame) -> CancelDecision {
        debug_assert_eq!(frame.header.ty, FrameType::Cancel);
        let corr = frame.header.corr;
        let decision = self
            .inbox
            .lock()
            .expect("route inbox mutex poisoned")
            .on_cancel(corr);
        match decision {
            CancelDecision::SynthesizeCancelled => {
                self.synthesize(corr, SyntheticKind::Cancelled);
                CancelDecision::SynthesizeCancelled
            }
            CancelDecision::DeferredToDrain => CancelDecision::DeferredToDrain,
            CancelDecision::ForwardToModule => {
                if self.cancel_tx.send(frame).is_ok() {
                    CancelDecision::QueuedForDrain
                } else {
                    CancelDecision::DrainStopped
                }
            }
            CancelDecision::QueuedForDrain | CancelDecision::DrainStopped => decision,
        }
    }

    pub(crate) fn begin_closing(&self, kind: TeardownKind) {
        let mut inbox = self.inbox.lock().expect("route inbox mutex poisoned");
        inbox.set_teardown(kind);
        inbox.begin_closing();
        drop(inbox);
        self.notify.notify_one();
    }

    pub(crate) fn finish_closed(&self) -> Vec<u64> {
        self.inbox
            .lock()
            .expect("route inbox mutex poisoned")
            .finish_closed()
    }

    pub(crate) fn close_flow(&self, kind: TeardownKind) {
        self.begin_closing(kind);
        self.flow.close();
    }

    pub(crate) fn terminal_from_module(&self, corr: u64) -> bool {
        let releases = self
            .inbox
            .lock()
            .expect("route inbox mutex poisoned")
            .on_terminal(corr);
        if releases {
            self.flow.release();
        }
        releases
    }

    fn synthesize(&self, corr: u64, kind: SyntheticKind) {
        let _ = self.synthetic_sink.send(SyntheticTerminal { corr, kind });
    }
}

impl RouteDrain {
    pub(crate) fn with_send_gate(mut self, gate: Arc<SendGate>) -> Self {
        self.send_gate = Some(gate);
        self
    }

    /// The sole call site that writes a route frame to the module sink.
    async fn send_to_module(&self, frame: Frame) -> bool {
        if let Some(gate) = &self.send_gate {
            gate.park_if_first_request(frame.header.ty).await;
        }
        self.module_sink.send(frame).await.is_ok()
    }

    pub(crate) async fn run(mut self) {
        loop {
            // A cancel queued while the preceding Request send was blocked must be emitted
            // before the drain starts another request-credit acquisition.
            if let Ok(cancel) = self.cancel_rx.try_recv() {
                let _ = self.send_to_module(cancel).await;
                continue;
            }

            let notified = self.dispatcher.notify.notified();
            let next = {
                let mut inbox = self
                    .dispatcher
                    .inbox
                    .lock()
                    .expect("route inbox mutex poisoned");
                let corr = inbox.pop_for_dispatch();
                let has_more_queue_entries = !inbox.queue_is_empty();
                let should_exit = inbox.admission() != Admission::Open
                    && corr.is_none()
                    && !has_more_queue_entries;
                (corr, has_more_queue_entries, should_exit)
            };

            let Some(corr) = next.0 else {
                if next.2 {
                    return;
                }
                if next.1 {
                    continue;
                }
                tokio::select! {
                    cancel = self.cancel_rx.recv() => {
                        if let Some(cancel) = cancel {
                            let _ = self.send_to_module(cancel).await;
                        }
                    }
                    () = notified => {}
                }
                continue;
            };

            // A later Request may be waiting for credit held by the cancel target. Keep
            // forwarding cancels while acquire is blocked so the target can settle and return
            // the credit that lets the later Request proceed.
            let acquired = loop {
                tokio::select! {
                    cancel = self.cancel_rx.recv() => {
                        if let Some(cancel) = cancel {
                            let _ = self.send_to_module(cancel).await;
                        }
                    }
                    acquired = self.dispatcher.flow.acquire() => break acquired,
                }
            };

            if acquired.is_err() {
                let kind = self
                    .dispatcher
                    .inbox
                    .lock()
                    .expect("route inbox mutex poisoned")
                    .on_acquire_closed(corr);
                match kind {
                    Some(TeardownKind::Reloading) => {
                        self.dispatcher
                            .synthesize(corr, SyntheticKind::ModuleReloading);
                    }
                    Some(TeardownKind::Goodbye | TeardownKind::ConnectionClose) => {}
                    Some(TeardownKind::None) => {
                        self.dispatcher
                            .synthesize(corr, SyntheticKind::BackendError);
                    }
                    None => {}
                }
                continue;
            }

            let token = CreditToken::new(Arc::clone(&self.dispatcher.flow));
            let decision = self
                .dispatcher
                .inbox
                .lock()
                .expect("route inbox mutex poisoned")
                .commit_delivered(corr);
            match decision {
                CommitDecision::Proceed(frame) => {
                    token.commit();
                    if !self.send_to_module(frame).await
                        && self.dispatcher.terminal_from_module(corr)
                    {
                        self.dispatcher
                            .synthesize(corr, SyntheticKind::BackendError);
                    }
                }
                CommitDecision::RollbackCancelled => {
                    drop(token);
                    self.dispatcher.synthesize(corr, SyntheticKind::Cancelled);
                }
                CommitDecision::Missing => {
                    drop(token);
                    self.dispatcher
                        .synthesize(corr, SyntheticKind::BackendError);
                }
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct SpikeRouteKey {
    pub connection: u64,
    pub channel: u16,
}

/// Spike counterpart of `forwarding.rs:52-65`'s `RouteBinding`.
///
/// The weak edge is the concrete path from `router.rs:281-309`'s module terminal arm back to
/// the route inbox. A weak reference avoids a binding/dispatcher ownership cycle.
#[derive(Debug)]
pub(crate) struct SpikeRouteBinding {
    dispatcher: Weak<RouteDispatcher>,
}

impl SpikeRouteBinding {
    pub(crate) fn new(dispatcher: &Arc<RouteDispatcher>) -> Self {
        Self {
            dispatcher: Arc::downgrade(dispatcher),
        }
    }

    pub(crate) fn on_terminal(&self, corr: u64) -> bool {
        self.dispatcher
            .upgrade()
            .is_some_and(|dispatcher| dispatcher.terminal_from_module(corr))
    }
}

/// Spike counterpart of `forwarding.rs`'s `ForwardingInner` dispatcher registry.
///
/// Connection/endpoint teardown enumerates this strong map to close every live dispatcher.
/// The lock order is registry then inbox; no inbox operation calls back into the registry.
#[derive(Debug, Default)]
pub(crate) struct SpikeForwardingInner {
    dispatchers: Mutex<HashMap<SpikeRouteKey, Arc<RouteDispatcher>>>,
}

impl SpikeForwardingInner {
    pub(crate) fn insert(&self, key: SpikeRouteKey, dispatcher: Arc<RouteDispatcher>) {
        self.dispatchers
            .lock()
            .expect("spike forwarding registry mutex poisoned")
            .insert(key, dispatcher);
    }

    pub(crate) fn begin_connection_teardown(&self, connection: u64, kind: TeardownKind) -> usize {
        let dispatchers = self
            .dispatchers
            .lock()
            .expect("spike forwarding registry mutex poisoned");
        let mut count = 0;
        for (key, dispatcher) in dispatchers.iter() {
            if key.connection == connection {
                dispatcher.begin_closing(kind);
                count += 1;
            }
        }
        count
    }
}

#[cfg(test)]
mod tests;