subc-daemon 0.50.49

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
// Regression tests for module-to-client splice defects found by a read-only
// audit of the router. Each test states the behaviour that should hold.

/// One module (registered with `concurrency`) and one client bound by one
/// route. The client's egress sink has `byte_budget` bytes, so a test can make
/// small frames fit while a larger one has to wait.
async fn audit_route(
    module_id: &str,
    concurrency: Concurrency,
    byte_budget: usize,
) -> (
    Arc<Router>,
    Arc<ForwardingTable>,
    crate::forwarding::ModuleEndpointId,
    mpsc::Receiver<OutboundFrame>,
    RouteCtx,
    crate::forwarding::PendingRouteBindRelay,
    mpsc::Receiver<OutboundFrame>,
) {
    let forwarding = Arc::new(ForwardingTable::default());
    let control = Arc::new(ControlHandler::with_forwarding(
        Arc::new(crate::Registry::default()),
        Arc::clone(&forwarding),
    ));
    let router = Arc::new(Router::with_control_handler(control));
    let module_connection = ConnectionId::new(1_100);
    let client_connection = ConnectionId::new(1_101);
    let (module_tx, module_rx) = mpsc::channel(8);
    let endpoint = forwarding
        .register_module_connection(
            module_connection,
            module_id.to_string(),
            1,
            concurrency,
            FrameSink::new(module_tx),
        )
        .unwrap();
    let (client_tx, mut client_rx) = mpsc::channel(64);
    let client_sink = FrameSink::with_byte_budget(client_tx, byte_budget);
    let route = forwarding
        .begin_route_bind_relay_for_test(client_connection, client_sink, 900, module_id)
        .unwrap();
    forwarding
        .complete_pending_relay(
            module_connection,
            route.corr,
            RouteBindRelayOutcome::Accepted,
        )
        .unwrap();
    assert_eq!(client_rx.recv().await.unwrap().header.corr, 900);
    let (module_egress_tx, _module_egress_rx) = mpsc::channel(8);
    let module_ctx = RouteCtx {
        connection_id: module_connection,
        egress: FrameSink::new(module_egress_tx),
    };
    (
        router, forwarding, endpoint, client_rx, module_ctx, route, module_rx,
    )
}

fn oversized_stream_chunk(route: &crate::forwarding::PendingRouteBindRelay, corr: u64) -> Frame {
    stream_frame(
        route.module_channel,
        route.module_epoch,
        corr,
        vec![b's'; crate::server::CONNECTION_EGRESS_BYTE_BUDGET - subc_protocol::HEADER_LEN + 1],
    )
}

/// A module that emits one stream chunk larger than the client's byte budget
/// is refused with `frame_too_large`, but it is still computing the request.
/// On a Serial route the next request must not be admitted to the module until
/// the module itself ends the first one.
#[tokio::test]
async fn oversized_stream_chunk_keeps_the_serial_credit_until_the_module_ends_the_request() {
    let (router, _forwarding, _endpoint, mut client_rx, module_ctx, route, _module_rx) =
        audit_route(
            "oversize-serial-provider",
            Concurrency::Serial,
            crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
        )
        .await;
    let binding = module_route_for_test(&router, &module_ctx, &route);
    binding.flow.acquire_tagged(401, false).await.unwrap();

    router
        .route_for_connection(&module_ctx, oversized_stream_chunk(&route, 401))
        .await
        .unwrap();
    assert_frame_size_error(
        &client_rx
            .try_recv()
            .expect("the client is told frame_too_large"),
        route.client_channel,
        route.client_epoch,
        401,
        crate::server::CONNECTION_EGRESS_BYTE_BUDGET + 1,
    );

    // The module has emitted no terminal for 401, so it is still running it.
    let held_after_refusal = binding.flow.drain_in_flight();
    let second = binding.flow.acquire_tagged(402, false);
    tokio::pin!(second);
    assert!(
        poll_once(second.as_mut()).await.is_pending(),
        "a second request entered the Serial route while the module was still running \
         request 401: the oversized STREAM_DATA chunk released 401's credit \
         (credits held after the refusal: {held_after_refusal})"
    );
    // The module's own terminal ends 401 and admits the waiting request.
    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::StreamEnd,
                route.module_channel,
                route.module_epoch,
                401,
            ),
        )
        .await
        .unwrap();
    assert!(poll_once(second.as_mut()).await.is_ready());
    assert!(
        client_rx.try_recv().is_err(),
        "the client already saw 401 end"
    );
}

/// After an oversized stream chunk ends the client's view of a request with
/// `frame_too_large`, the client must see nothing more on that correlation:
/// the module's later chunks and its STREAM_END are not delivered.
#[tokio::test]
async fn frames_after_an_oversized_stream_chunk_are_not_forwarded_to_the_client() {
    let (router, _forwarding, _endpoint, mut client_rx, module_ctx, route, mut module_rx) =
        audit_route(
            "oversize-continuation-provider",
            Concurrency::ModuleManaged,
            crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
        )
        .await;
    let binding = module_route_for_test(&router, &module_ctx, &route);
    binding.flow.acquire_tagged(401, false).await.unwrap();

    router
        .route_for_connection(&module_ctx, oversized_stream_chunk(&route, 401))
        .await
        .unwrap();
    let error = client_rx
        .try_recv()
        .expect("the client is told frame_too_large");
    assert_eq!(error.header.ty, FrameType::Error);
    let cancel = module_rx
        .try_recv()
        .expect("the module is asked to stop the refused request");
    assert_eq!(
        (
            cancel.header.ty,
            cancel.header.channel,
            cancel.header.epoch,
            cancel.header.corr
        ),
        (
            FrameType::Cancel,
            route.module_channel,
            route.module_epoch,
            401
        )
    );

    // The module keeps going: one ordinary chunk, then its terminal.
    router
        .route_for_connection(
            &module_ctx,
            stream_frame(
                route.module_channel,
                route.module_epoch,
                401,
                b"late".to_vec(),
            ),
        )
        .await
        .unwrap();
    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::StreamEnd,
                route.module_channel,
                route.module_epoch,
                401,
            ),
        )
        .await
        .unwrap();

    let mut after_error = Vec::new();
    while let Ok(outbound) = client_rx.try_recv() {
        after_error.push((outbound.header.ty, outbound.header.corr));
    }
    assert!(
        after_error.is_empty(),
        "corr 401 already ended with frame_too_large, yet the client was sent {after_error:?}"
    );
    assert_eq!(
        binding.flow.drain_in_flight(),
        0,
        "the module's STREAM_END releases the credit"
    );
}

/// Stand in for the supervisor's restart drain from the moment it polls for
/// quiescence: when the endpoint holds no request credit it reports
/// `drained: true`, releases the endpoint's routes and sends each client a
/// route GOODBYE, as `wait_for_forwarding_quiescence` followed by
/// `send_route_goodbyes` do. Returns whether the drain completed.
fn drain_if_quiescent(
    forwarding: &ForwardingTable,
    endpoint: crate::forwarding::ModuleEndpointId,
) -> bool {
    if forwarding.endpoint_in_flight_count(endpoint).unwrap() != 0 {
        return false;
    }
    for target in forwarding.release_module_endpoint_routes(endpoint).unwrap() {
        let goodbye = Frame::build(
            FrameType::Goodbye,
            crate::control::control_flags(),
            target.channel,
            target.epoch,
            0,
            Vec::new(),
        )
        .unwrap();
        target
            .sink
            .try_send(goodbye)
            .expect("the small route GOODBYE fits the client's remaining budget");
    }
    true
}

/// The last call of a draining module completes while its ordinary-sized
/// response cannot yet fit the client's byte budget. The drain must not
/// report quiescence and send the route GOODBYE ahead of that response: the
/// client would settle the call as closed and then discard the reply.
#[tokio::test(start_paused = true)]
async fn restart_drain_delivers_a_completed_reply_before_the_route_goodbye() {
    const BUDGET: usize = 4_096;
    const ROOM_LEFT: usize = 600;
    let (router, forwarding, endpoint, mut client_rx, module_ctx, route, _module_rx) =
        audit_route("drain-reply-provider", Concurrency::ModuleManaged, BUDGET).await;
    let binding = module_route_for_test(&router, &module_ctx, &route);
    binding.flow.acquire_tagged(401, false).await.unwrap();

    // Earlier traffic the client has not read yet leaves room for a header-only
    // GOODBYE but not for the response below.
    binding
        .client_sink
        .try_send(stream_frame(
            route.client_channel,
            route.client_epoch,
            300,
            vec![b'q'; BUDGET - ROOM_LEFT - subc_protocol::HEADER_LEN],
        ))
        .unwrap();
    let response = Frame::build(
        FrameType::Response,
        Flags::new(false, Priority::Interactive, false),
        route.module_channel,
        route.module_epoch,
        401,
        vec![b'r'; 1_500],
    )
    .unwrap();
    let delivery = tokio::spawn({
        let router = Arc::clone(&router);
        let module_ctx = module_ctx.clone();
        async move { router.route_for_connection(&module_ctx, response).await }
    });
    for _ in 0..8 {
        tokio::task::yield_now().await;
    }
    assert!(
        !delivery.is_finished(),
        "the response must still be waiting for client egress room"
    );

    let mut drained = drain_if_quiescent(&forwarding, endpoint);

    // The client resumes reading: the old traffic leaves, the response fits.
    let earlier = client_rx.recv().await.unwrap();
    assert_eq!(earlier.header.corr, 300);
    drop(earlier);
    delivery.await.unwrap().unwrap();
    if !drained {
        drained = drain_if_quiescent(&forwarding, endpoint);
    }
    assert!(
        drained,
        "the drain completes once the reply has been queued"
    );

    let mut arrived = Vec::new();
    while let Ok(outbound) = client_rx.try_recv() {
        arrived.push((outbound.header.ty, outbound.header.corr));
    }
    let response_at = arrived
        .iter()
        .position(|(ty, corr)| *ty == FrameType::Response && *corr == 401);
    let goodbye_at = arrived.iter().position(|(ty, _)| *ty == FrameType::Goodbye);
    assert!(
        matches!((response_at, goodbye_at), (Some(response), Some(goodbye)) if response < goodbye),
        "the drain saw zero in-flight credits while corr 401's response still waited for client \
         egress, so the route GOODBYE reached the client first: {arrived:?}"
    );
}

/// On a legacy Serial route, a request that must wait for the route's only
/// credit is queued rather than holding the connection reader: the client's
/// answer to the module's reverse question still reaches the module at once,
/// and the waiting request and its CANCEL follow, in order, when the running
/// request ends.
#[tokio::test]
async fn legacy_serial_route_queues_a_waiting_request_without_holding_the_reader() {
    let (router, _forwarding, _endpoint, _client_rx, module_ctx, route, mut module_rx) =
        audit_route(
            "legacy-queue-provider",
            Concurrency::Serial,
            crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
        )
        .await;
    let (client_tx, _client_error_rx) = mpsc::channel(8);
    let client_ctx = RouteCtx {
        connection_id: ConnectionId::new(1_101),
        egress: FrameSink::new(client_tx),
    };
    let client = |ty, corr| route_frame(ty, route.client_channel, route.client_epoch, corr);
    for (ty, corr) in [
        (FrameType::Request, 10),
        (FrameType::Request, 11),
        (FrameType::Cancel, 11),
        (FrameType::Response, 77),
    ] {
        tokio::time::timeout(
            Duration::from_secs(1),
            router.route_for_connection(&client_ctx, client(ty, corr)),
        )
        .await
        .unwrap_or_else(|_| panic!("the reader was held by {ty:?} {corr}"))
        .unwrap();
    }
    let mut reached = Vec::new();
    while let Ok(outbound) = module_rx.try_recv() {
        reached.push((outbound.header.ty, outbound.header.corr));
    }
    assert_eq!(
        reached,
        vec![(FrameType::Request, 10), (FrameType::Response, 77)],
        "only the running request and the reverse answer reach the module"
    );

    // The module ends request 10; the waiting request, then its CANCEL, go.
    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::Response,
                route.module_channel,
                route.module_epoch,
                10,
            ),
        )
        .await
        .unwrap();
    for expected in [(FrameType::Request, 11), (FrameType::Cancel, 11)] {
        let next = tokio::time::timeout(Duration::from_secs(1), module_rx.recv())
            .await
            .expect("the waiting frames are forwarded once credit frees")
            .unwrap();
        assert_eq!((next.header.ty, next.header.corr), expected);
    }
}

/// A CANCEL for a request still waiting for credit goes directly behind that
/// request, not behind later requests: once the running request ends, the
/// module gets the waiting request and then its CANCEL, while the later
/// request keeps waiting for credit the CANCEL needs none of.
#[tokio::test]
async fn cancel_of_a_waiting_request_follows_it_ahead_of_later_requests() {
    let (router, _forwarding, _endpoint, _client_rx, module_ctx, route, mut module_rx) =
        audit_route(
            "legacy-cancel-provider",
            Concurrency::Serial,
            crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
        )
        .await;
    let (client_tx, _client_error_rx) = mpsc::channel(8);
    let client_ctx = RouteCtx {
        connection_id: ConnectionId::new(1_101),
        egress: FrameSink::new(client_tx),
    };
    let client = |ty, corr| route_frame(ty, route.client_channel, route.client_epoch, corr);
    for (ty, corr) in [
        (FrameType::Request, 10),
        (FrameType::Request, 11),
        (FrameType::Request, 12),
        (FrameType::Cancel, 11),
    ] {
        router
            .route_for_connection(&client_ctx, client(ty, corr))
            .await
            .unwrap();
    }
    assert_eq!(
        module_rx.recv().await.unwrap().header.corr,
        10,
        "request 10 runs"
    );
    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::Response,
                route.module_channel,
                route.module_epoch,
                10,
            ),
        )
        .await
        .unwrap();
    for expected in [(FrameType::Request, 11), (FrameType::Cancel, 11)] {
        let next = tokio::time::timeout(Duration::from_secs(1), module_rx.recv())
            .await
            .unwrap_or_else(|_| panic!("{expected:?} never reached the module"))
            .unwrap();
        assert_eq!((next.header.ty, next.header.corr), expected);
    }
    assert!(
        module_rx.try_recv().is_err(),
        "request 12 still waits for the credit request 11 holds"
    );
}

/// A request the forwarding task is still trying to put on a stalled
/// module's queue must not reach the module once its route is released.
#[tokio::test]
async fn a_waiting_request_never_reaches_the_module_after_its_route_is_released() {
    let (router, forwarding, _endpoint, _client_rx, module_ctx, route, mut module_rx) =
        audit_route(
            "legacy-release-provider",
            Concurrency::Serial,
            crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
        )
        .await;
    let binding = module_route_for_test(&router, &module_ctx, &route);
    let (client_tx, _client_error_rx) = mpsc::channel(8);
    let client_ctx = RouteCtx {
        connection_id: ConnectionId::new(1_101),
        egress: FrameSink::new(client_tx),
    };
    let client = |corr| {
        route_frame(
            FrameType::Request,
            route.client_channel,
            route.client_epoch,
            corr,
        )
    };
    router
        .route_for_connection(&client_ctx, client(10))
        .await
        .unwrap();
    router
        .route_for_connection(&client_ctx, client(11))
        .await
        .unwrap();
    // The module stops reading: its queue fills.
    while binding
        .module_sink
        .try_send(
            Frame::build(
                FrameType::Push,
                Flags::new(false, Priority::Passive, false),
                0,
                0,
                0,
                Vec::new(),
            )
            .unwrap(),
        )
        .is_ok()
    {}
    // Request 10 ends; the task takes the credit for 11 and waits for room.
    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::Response,
                route.module_channel,
                route.module_epoch,
                10,
            ),
        )
        .await
        .unwrap();
    for _ in 0..8 {
        tokio::task::yield_now().await;
    }
    forwarding
        .release_client_route(
            ConnectionId::new(1_101),
            route.client_channel,
            route.client_epoch,
        )
        .unwrap();
    // The module resumes reading.
    let mut reached = Vec::new();
    for _ in 0..4 {
        while let Ok(outbound) = module_rx.try_recv() {
            reached.push((outbound.header.ty, outbound.header.corr));
        }
        for _ in 0..8 {
            tokio::task::yield_now().await;
        }
    }
    assert!(
        !reached.contains(&(FrameType::Request, 11)),
        "request 11 reached the module after its route was released: {reached:?}"
    );
}

/// Records the route's held credits whenever the delivery-failure path logs
/// that it is closing the client connection.
#[derive(Clone)]
struct HeldAtEscalation {
    flow: Arc<crate::forwarding::ChannelFlow>,
    held: Arc<std::sync::Mutex<Vec<usize>>>,
}

impl std::io::Write for HeldAtEscalation {
    fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
        if String::from_utf8_lossy(buffer).contains("closing client connection") {
            self.held.lock().unwrap().push(self.flow.drain_in_flight());
        }
        Ok(buffer.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// A final reply the client can no longer take is settled by the
/// delivery-failure path. Its credit must still be held while that path marks
/// the client closing, so a drain polling in between cannot report the
/// request finished and `drained: true` before the failure is owned.
#[tokio::test]
async fn a_failed_final_reply_keeps_its_credit_until_the_client_close_is_requested() {
    let (router, forwarding, _endpoint, _client_rx, module_ctx, route, _module_rx) = audit_route(
        "failed-reply-provider",
        Concurrency::ModuleManaged,
        crate::server::CONNECTION_EGRESS_BYTE_BUDGET,
    )
    .await;
    let binding = module_route_for_test(&router, &module_ctx, &route);
    binding.flow.acquire_tagged(401, false).await.unwrap();
    let _close = forwarding.register_connection_close(ConnectionId::new(1_101));
    binding.client_sink.close_client_delivery();
    let probe = HeldAtEscalation {
        flow: Arc::clone(&binding.flow),
        held: Arc::new(std::sync::Mutex::new(Vec::new())),
    };
    let held = Arc::clone(&probe.held);
    let subscriber = tracing_subscriber::fmt()
        .without_time()
        .with_ansi(false)
        .with_writer(move || probe.clone())
        .finish();
    let _subscriber = tracing::subscriber::set_default(subscriber);

    router
        .route_for_connection(
            &module_ctx,
            route_frame(
                FrameType::Response,
                route.module_channel,
                route.module_epoch,
                401,
            ),
        )
        .await
        .unwrap();
    assert_eq!(
        *held.lock().unwrap(),
        vec![1],
        "credits held when the client close was requested"
    );
    assert_eq!(binding.flow.drain_in_flight(), 0, "and retired after it");
}