rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
use std::collections::BTreeSet;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex};

use tokio::sync::Notify;

const TICKET_PENDING: u8 = 0;
const TICKET_CLAIMED: u8 = 1;
const TICKET_FINISHED: u8 = 2;

/// Orders lifecycle publication by the point at which an event was prepared
/// while the authoritative state lock was held.
#[derive(Debug)]
pub(crate) struct LifecycleCommitOrder {
    admission: Mutex<LifecycleCommitAdmission>,
    coordinator: Arc<LifecycleCommitCoordinator>,
    pending: LifecycleCommitPending,
}

#[derive(Debug)]
struct LifecycleCommitAdmission {
    accepting: bool,
    accepting_publications: bool,
    next_sequence: u64,
}

impl Default for LifecycleCommitOrder {
    fn default() -> Self {
        Self {
            admission: Mutex::new(LifecycleCommitAdmission {
                accepting: true,
                accepting_publications: true,
                next_sequence: 0,
            }),
            coordinator: Arc::default(),
            pending: LifecycleCommitPending::default(),
        }
    }
}

impl LifecycleCommitOrder {
    pub(crate) fn try_reserve(&self) -> Option<LifecycleCommitTicket> {
        let sequence = {
            let mut admission = self
                .admission
                .lock()
                .expect("lifecycle commit admission must not be poisoned");
            if !admission.accepting {
                return None;
            }
            let sequence = admission.next_sequence;
            admission.next_sequence = admission.next_sequence.wrapping_add(1);
            self.pending.start();
            sequence
        };
        Some(LifecycleCommitTicket {
            inner: Arc::new(LifecycleCommitTicketInner {
                sequence,
                coordinator: Arc::clone(&self.coordinator),
                state: AtomicU8::new(TICKET_PENDING),
                finished: Notify::new(),
                pending: self.pending.clone(),
                pending_accounted: AtomicU8::new(1),
            }),
        })
    }

    pub(crate) fn close(&self) -> LifecycleCommitPending {
        self.admission
            .lock()
            .expect("lifecycle commit admission must not be poisoned")
            .accepting = false;
        self.pending.clone()
    }

    /// Tracks a hookless publication that does not own an ordered hook ticket.
    ///
    /// Hook admission may already be closed during shutdown, but committed
    /// control effects still have to remain visible to the final drain barrier.
    pub(crate) fn track_unordered_publication(&self) -> Option<LifecyclePublicationGuard> {
        let admission = self
            .admission
            .lock()
            .expect("lifecycle commit admission must not be poisoned");
        if !admission.accepting_publications {
            return None;
        }
        self.pending.start();
        Some(LifecyclePublicationGuard {
            inner: Arc::new(LifecyclePublicationGuardInner {
                pending: self.pending.clone(),
            }),
        })
    }

    /// Seals the final publication boundary after all ordinary producers stop.
    pub(crate) fn seal_publications(&self) -> LifecycleCommitPending {
        let mut admission = self
            .admission
            .lock()
            .expect("lifecycle commit admission must not be poisoned");
        admission.accepting = false;
        admission.accepting_publications = false;
        self.pending.clone()
    }

    #[cfg(test)]
    pub(crate) fn pending(&self) -> LifecycleCommitPending {
        self.pending.clone()
    }

    #[cfg(test)]
    fn reserve(&self) -> LifecycleCommitTicket {
        self.try_reserve()
            .expect("test lifecycle commit admission remains open")
    }
}

/// Keeps one hookless lifecycle publication visible to shutdown barriers.
#[derive(Debug, Clone)]
pub(crate) struct LifecyclePublicationGuard {
    inner: Arc<LifecyclePublicationGuardInner>,
}

#[derive(Debug)]
struct LifecyclePublicationGuardInner {
    pending: LifecycleCommitPending,
}

impl Drop for LifecyclePublicationGuardInner {
    fn drop(&mut self) {
        self.pending.finish();
    }
}

impl PartialEq for LifecyclePublicationGuard {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Eq for LifecyclePublicationGuard {}

#[derive(Debug, Default)]
struct LifecycleCommitCoordinator {
    state: Mutex<LifecycleCommitCoordinatorState>,
    advanced: Notify,
}

#[derive(Debug, Default)]
struct LifecycleCommitCoordinatorState {
    next_sequence: u64,
    finished_out_of_order: BTreeSet<u64>,
}

impl LifecycleCommitCoordinator {
    async fn wait_for_turn(&self, sequence: u64) {
        loop {
            let advanced = self.advanced.notified();
            tokio::pin!(advanced);
            advanced.as_mut().enable();
            if self
                .state
                .lock()
                .expect("lifecycle commit coordinator must not be poisoned")
                .next_sequence
                == sequence
            {
                return;
            }
            advanced.await;
        }
    }

    fn finish(&self, sequence: u64) {
        let advanced = {
            let mut state = self
                .state
                .lock()
                .expect("lifecycle commit coordinator must not be poisoned");
            if sequence < state.next_sequence {
                false
            } else if sequence == state.next_sequence {
                state.next_sequence = state.next_sequence.wrapping_add(1);
                while {
                    let next_sequence = state.next_sequence;
                    state.finished_out_of_order.remove(&next_sequence)
                } {
                    state.next_sequence = state.next_sequence.wrapping_add(1);
                }
                true
            } else {
                state.finished_out_of_order.insert(sequence);
                false
            }
        };
        if advanced {
            self.advanced.notify_waiters();
        }
    }
}

#[derive(Debug, Clone, Default)]
pub(crate) struct LifecycleCommitPending {
    inner: Arc<LifecycleCommitPendingInner>,
}

#[derive(Debug, Default)]
struct LifecycleCommitPendingInner {
    count: Mutex<usize>,
    idle: Notify,
}

impl LifecycleCommitPending {
    fn start(&self) {
        let mut count = self
            .inner
            .count
            .lock()
            .expect("lifecycle pending count must not be poisoned");
        *count = count.saturating_add(1);
    }

    fn finish(&self) {
        let is_idle = {
            let mut count = self
                .inner
                .count
                .lock()
                .expect("lifecycle pending count must not be poisoned");
            debug_assert!(*count > 0, "lifecycle pending count underflow");
            *count = count.saturating_sub(1);
            *count == 0
        };
        if is_idle {
            self.inner.idle.notify_waiters();
        }
    }

    pub(crate) async fn wait_until_idle(&self) {
        loop {
            let idle = self.inner.idle.notified();
            tokio::pin!(idle);
            idle.as_mut().enable();
            if *self
                .inner
                .count
                .lock()
                .expect("lifecycle pending count must not be poisoned")
                == 0
            {
                return;
            }
            idle.await;
        }
    }
}

/// A clonable reference to one committed lifecycle position.
///
/// Production clones represent the same committed event (for example an
/// alert plan cloned before dispatch), so exactly one clone claims the turn.
#[derive(Debug, Clone)]
pub(crate) struct LifecycleCommitTicket {
    inner: Arc<LifecycleCommitTicketInner>,
}

#[derive(Debug)]
struct LifecycleCommitTicketInner {
    sequence: u64,
    coordinator: Arc<LifecycleCommitCoordinator>,
    state: AtomicU8,
    finished: Notify,
    pending: LifecycleCommitPending,
    pending_accounted: AtomicU8,
}

impl LifecycleCommitTicketInner {
    fn finish_once(&self) {
        if self.state.swap(TICKET_FINISHED, Ordering::AcqRel) == TICKET_FINISHED {
            return;
        }
        self.coordinator.finish(self.sequence);
        self.finish_pending_once();
        self.finished.notify_waiters();
    }

    fn finish_pending_once(&self) {
        if self.pending_accounted.swap(0, Ordering::AcqRel) == 1 {
            self.pending.finish();
        }
    }
}

impl Drop for LifecycleCommitTicketInner {
    fn drop(&mut self) {
        self.finish_once();
    }
}

/// Owns a claimed position while its predecessor is still outstanding.
///
/// Dropping the future returned by `wait_for_turn` also drops this guard. That
/// marks the abandoned position finished, while the coordinator keeps later
/// positions behind every earlier outstanding commit.
#[derive(Debug)]
struct LifecycleCommitClaimGuard {
    ticket: Option<LifecycleCommitTicket>,
}

impl LifecycleCommitClaimGuard {
    fn into_turn(mut self) -> LifecycleCommitTurn {
        LifecycleCommitTurn {
            ticket: self.ticket.take(),
        }
    }
}

impl Drop for LifecycleCommitClaimGuard {
    fn drop(&mut self) {
        if let Some(ticket) = self.ticket.take() {
            ticket.inner.finish_once();
        }
    }
}

impl LifecycleCommitTicket {
    pub(crate) async fn wait_for_turn(&self) -> LifecycleCommitTurn {
        if self
            .inner
            .state
            .compare_exchange(
                TICKET_PENDING,
                TICKET_CLAIMED,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_ok()
        {
            let claim = LifecycleCommitClaimGuard {
                ticket: Some(self.clone()),
            };
            self.inner
                .coordinator
                .wait_for_turn(self.inner.sequence)
                .await;
            return claim.into_turn();
        }

        loop {
            let finished = self.inner.finished.notified();
            tokio::pin!(finished);
            finished.as_mut().enable();
            if self.inner.state.load(Ordering::Acquire) == TICKET_FINISHED {
                return LifecycleCommitTurn { ticket: None };
            }
            finished.await;
        }
    }

    #[cfg(test)]
    pub(crate) fn sequence(&self) -> u64 {
        self.inner.sequence
    }
}

impl PartialEq for LifecycleCommitTicket {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Eq for LifecycleCommitTicket {}

/// Releases the next committed lifecycle event when publication admission is
/// complete. Hook execution itself deliberately happens after this turn.
#[derive(Debug)]
pub(crate) struct LifecycleCommitTurn {
    ticket: Option<LifecycleCommitTicket>,
}

impl Drop for LifecycleCommitTurn {
    fn drop(&mut self) {
        let Some(ticket) = self.ticket.take() else {
            return;
        };
        ticket.inner.finish_once();
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::Ordering;
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    use super::{LifecycleCommitOrder, TICKET_CLAIMED, TICKET_FINISHED};

    #[tokio::test]
    async fn turns_follow_reservation_order_even_when_waiters_arrive_reversed() {
        let order = LifecycleCommitOrder::default();
        let first = order.reserve();
        let second = order.reserve();
        let observed = Arc::new(Mutex::new(Vec::new()));

        let second_observed = Arc::clone(&observed);
        let second_task = tokio::spawn(async move {
            let _turn = second.wait_for_turn().await;
            second_observed.lock().expect("observed order").push(2);
        });
        tokio::task::yield_now().await;
        assert!(observed.lock().expect("observed order").is_empty());

        let first_observed = Arc::clone(&observed);
        let first_task = tokio::spawn(async move {
            let _turn = first.wait_for_turn().await;
            first_observed.lock().expect("observed order").push(1);
        });

        first_task.await.expect("first waiter");
        second_task.await.expect("second waiter");
        assert_eq!(*observed.lock().expect("observed order"), vec![1, 2]);
    }

    #[tokio::test]
    async fn dropping_an_unpublished_ticket_releases_its_successor() {
        let order = LifecycleCommitOrder::default();
        let first = order.reserve();
        let second = order.reserve();
        drop(first);

        tokio::time::timeout(std::time::Duration::from_secs(1), second.wait_for_turn())
            .await
            .expect("cancelled predecessor releases successor");
    }

    #[tokio::test]
    async fn cancelling_claim_while_waiting_preserves_order_and_releases_successor() {
        let order = LifecycleCommitOrder::default();
        let predecessor = order.reserve();
        let cancelled = order.reserve();
        let successor = order.reserve();
        let predecessor_turn = predecessor.wait_for_turn().await;

        let cancelled_probe = cancelled.clone();
        let cancelled_task = tokio::spawn(async move {
            let _turn = cancelled.wait_for_turn().await;
        });
        tokio::time::timeout(Duration::from_secs(1), async {
            while cancelled_probe.inner.state.load(Ordering::Acquire) != TICKET_CLAIMED {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("cancelled waiter claims its position");

        let successor_probe = successor.clone();
        let mut successor_task = tokio::spawn(async move {
            let _turn = successor.wait_for_turn().await;
        });
        tokio::time::timeout(Duration::from_secs(1), async {
            while successor_probe.inner.state.load(Ordering::Acquire) != TICKET_CLAIMED {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("successor claims its position");

        cancelled_task.abort();
        assert!(cancelled_task
            .await
            .expect_err("cancelled waiter must not complete")
            .is_cancelled());
        assert_eq!(
            cancelled_probe.inner.state.load(Ordering::Acquire),
            TICKET_FINISHED
        );
        tokio::task::yield_now().await;
        assert!(
            !successor_task.is_finished(),
            "cancellation must not let the successor overtake its predecessor"
        );

        drop(predecessor_turn);
        tokio::time::timeout(Duration::from_secs(1), &mut successor_task)
            .await
            .expect("successor advances after the predecessor finishes")
            .expect("successor waiter joins");
    }

    #[tokio::test]
    async fn pending_wait_includes_unpublished_tickets_and_finishes_on_cancellation() {
        let order = LifecycleCommitOrder::default();
        let pending = order.pending();
        let ticket = order.reserve();
        let mut idle = tokio::spawn(async move { pending.wait_until_idle().await });
        tokio::task::yield_now().await;
        assert!(!idle.is_finished());

        drop(ticket);
        tokio::time::timeout(std::time::Duration::from_secs(1), &mut idle)
            .await
            .expect("cancelling the unpublished ticket reaches idle")
            .expect("idle waiter joins");
    }

    #[tokio::test]
    async fn closing_admission_rejects_later_tickets_and_waits_for_earlier_ones() {
        let order = LifecycleCommitOrder::default();
        let ticket = order.reserve();
        let pending = order.close();
        assert!(order.try_reserve().is_none());

        let mut drained = tokio::spawn(async move { pending.wait_until_idle().await });
        tokio::task::yield_now().await;
        assert!(!drained.is_finished());
        drop(ticket);
        tokio::time::timeout(Duration::from_secs(1), &mut drained)
            .await
            .expect("pre-close ticket drains")
            .expect("drain waiter joins");
    }

    #[tokio::test]
    async fn hookless_publication_after_admission_close_is_still_counted() {
        let order = LifecycleCommitOrder::default();
        let pending = order.close();
        let publication = order
            .track_unordered_publication()
            .expect("hook closure still accepts hookless control effects");
        let mut drained = tokio::spawn(async move { pending.wait_until_idle().await });

        assert!(
            tokio::time::timeout(Duration::from_millis(20), &mut drained)
                .await
                .is_err(),
            "the final barrier must include hookless committed effects"
        );

        drop(publication);
        tokio::time::timeout(Duration::from_secs(1), drained)
            .await
            .expect("hookless publication releases final barrier")
            .expect("drain waiter joins");
    }

    #[tokio::test]
    async fn sealing_publications_linearizes_the_final_idle_barrier() {
        let order = LifecycleCommitOrder::default();
        let publication = order
            .track_unordered_publication()
            .expect("publication admission starts open");
        let pending = order.seal_publications();
        assert!(order.track_unordered_publication().is_none());
        assert!(order.try_reserve().is_none());

        let mut drained = tokio::spawn(async move { pending.wait_until_idle().await });
        assert!(
            tokio::time::timeout(Duration::from_millis(20), &mut drained)
                .await
                .is_err(),
            "the pre-seal publication remains part of the final barrier"
        );
        drop(publication);
        tokio::time::timeout(Duration::from_secs(1), drained)
            .await
            .expect("pre-seal publication drains")
            .expect("drain waiter joins");
    }

    #[test]
    fn cloned_ticket_keeps_one_commit_identity() {
        let order = LifecycleCommitOrder::default();
        let ticket = order.reserve();
        let clone = ticket.clone();
        assert_eq!(ticket, clone);
        assert_eq!(ticket.sequence(), clone.sequence());

        let unrelated = LifecycleCommitOrder::default().reserve();
        assert_ne!(ticket, unrelated);
    }
}