Skip to main content

subc_daemon/
server.rs

1use std::{
2    any::Any,
3    error::Error,
4    fmt, io,
5    net::SocketAddr,
6    sync::{
7        atomic::{AtomicU64, Ordering as AtomicOrdering},
8        Arc,
9    },
10    time::{Duration, Instant},
11};
12
13use subc_transport::{
14    authenticate_server, AuthError, AuthStage, DAEMON_ID_LEN, WATCHDOG_CLIENT_ROLE,
15};
16use tokio::{
17    io::{AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter},
18    net::TcpListener,
19    sync::{mpsc, Semaphore},
20    task::{JoinHandle, JoinSet},
21    time::timeout,
22};
23use tracing::{debug, info, warn};
24
25use crate::{
26    forwarding::{CloseReason, ConnectionCloseReceiver},
27    observability::ConnectedClients,
28    read_frame,
29    router::{FrameSink, RouteCtx, Router},
30    write_frame, FrameIoError, RouterError,
31};
32
33/// Bytes (envelope header plus body) one connection's egress queue may hold
34/// before a module-to-client enqueue must wait for room.
35///
36/// Sized for a client that is slow but still reading: one connection can
37/// multiplex many token streams, and at roughly 200-byte frames (21-byte header
38/// plus body) 4 MiB is about 19,000 queued frames, i.e. thousands of frames on
39/// each of several routes, enough to ride out a pause of seconds. It is also the
40/// most a stuck client can make the daemon hold for it: 4 MiB per connection,
41/// so even 128 stuck connections stay near 512 MiB rather than growing without
42/// bound. A single frame larger than the whole budget is still admitted into an
43/// empty queue on send paths that do not check its size first.
44///
45/// The router, however, refuses a module-to-client frame whose header-plus-body
46/// size exceeds the client connection's budget before queueing it, so the budget
47/// is also the largest module-to-client frame the daemon delivers. Modules read
48/// that limit from `subc_protocol::MAX_DELIVERED_FRAME_LEN`, so the budget is
49/// defined as that constant; giving it a different value would make the
50/// published limit wrong.
51pub const CONNECTION_EGRESS_BYTE_BUDGET: usize = subc_protocol::MAX_DELIVERED_FRAME_LEN;
52/// Let a reading client drain a short fanout burst, but never park a module's
53/// frame reader for more than 250 ms. The atomic backlog age is measured from
54/// the enqueue time of the frame the writer is on, or of the frame that made
55/// the queue non-empty. A blocked writer makes that age the stopped-reader
56/// signal; between frames it may be one frame older than the true head, which
57/// is acceptable for this bounded grace and avoids a lock on every frame.
58pub const CONNECTION_EGRESS_STALL_GRACE: Duration = Duration::from_millis(250);
59/// Frame-count backstop for the same queue (the tokio channel's capacity).
60/// The byte budget is the real bound; this one only matters for floods of tiny
61/// or empty frames, which cost bookkeeping per frame rather than bytes. 32,768
62/// frames times 128 bytes is exactly the 4 MiB budget, so for any mean frame of
63/// 128 bytes or more the byte budget binds first; below that (for example
64/// 21-byte empty frames, of which 4 MiB would be almost 200,000) the count
65/// does. The channel allocates slots lazily, so a large capacity costs nothing
66/// until it is used.
67pub const CONNECTION_EGRESS_FRAME_CAP: usize = 32 * 1024;
68/// A pending `route.open` holds one reserved slot in the connection's egress
69/// queue until its module answers, and its response is sent outside the byte
70/// budget. Eight concurrent opens per connection keep a reconnect burst
71/// parallel while bounding both the reserved slots and the bytes that bypass
72/// the budget.
73pub const MAX_PENDING_ROUTE_OPENS_PER_CONNECTION: usize = 8;
74/// A reconnect herd may spread one target's opens over many client connections.
75/// Two safe per-connection bursts retain useful parallelism without restoring
76/// the hundreds-of-binds fanout that serial dispatch used to suppress.
77pub(crate) const MAX_PENDING_ROUTE_BINDS_PER_TARGET: usize =
78    MAX_PENDING_ROUTE_OPENS_PER_CONNECTION * 2;
79pub const DEFAULT_AUTH_DEADLINE: Duration = Duration::from_secs(2);
80// Sized for the restart-herd shape: after a daemon bounce, every live client
81// connection plus all supervised children re-dial within the same second
82// (~120+ observed on the 2026-07-14 fleet). Handshakes are cheap loopback
83// HMAC exchanges; the deadline, not the permit count, is the DoS bound.
84pub const DEFAULT_MAX_UNAUTHENTICATED_CONNECTIONS: usize = 256;
85const CLOSE_DRAIN_GRACE: Duration = Duration::from_secs(2);
86const PRE_AUTH_REJECT_LOG_INTERVAL: Duration = Duration::from_secs(1);
87
88/// A connection's writer can outlive its reader while draining. Wake module
89/// deliveries when the reader exits, including task cancellation or a panic.
90struct ClientDeliveryGuard(FrameSink);
91
92impl Drop for ClientDeliveryGuard {
93    fn drop(&mut self) {
94        self.0.close_client_delivery();
95    }
96}
97
98#[derive(Clone, Copy, Debug)]
99enum PreAuthRejectStage {
100    WaitingForHandshakeSlot,
101    HandshakeDeadline,
102    InvalidProof,
103    MalformedMessage,
104    PeerClosed,
105    OtherAuthenticationFailure,
106}
107
108impl PreAuthRejectStage {
109    const fn index(self) -> usize {
110        match self {
111            Self::WaitingForHandshakeSlot => 0,
112            Self::HandshakeDeadline => 1,
113            Self::InvalidProof => 2,
114            Self::MalformedMessage => 3,
115            Self::PeerClosed => 4,
116            Self::OtherAuthenticationFailure => 5,
117        }
118    }
119
120    const fn stage(self) -> &'static str {
121        match self {
122            Self::WaitingForHandshakeSlot => "waiting_for_handshake_slot",
123            Self::HandshakeDeadline => "handshake_deadline",
124            Self::InvalidProof => "client_authentication",
125            Self::MalformedMessage => "malformed_message",
126            Self::PeerClosed => "peer_closed",
127            Self::OtherAuthenticationFailure => "authentication",
128        }
129    }
130
131    const fn reason(self) -> &'static str {
132        match self {
133            Self::WaitingForHandshakeSlot => "capacity_wait_timed_out",
134            Self::HandshakeDeadline => "handshake_deadline_expired",
135            Self::InvalidProof => "client_proof_mismatch",
136            Self::MalformedMessage => "malformed_or_oversized_message",
137            Self::PeerClosed => "peer_closed_before_authentication_completed",
138            Self::OtherAuthenticationFailure => "authentication_failed_before_routing",
139        }
140    }
141}
142
143#[derive(Debug)]
144struct PreAuthRejectRateLimit {
145    next_allowed_ns: AtomicU64,
146    suppressed: AtomicU64,
147}
148
149impl PreAuthRejectRateLimit {
150    const fn new() -> Self {
151        Self {
152            next_allowed_ns: AtomicU64::new(0),
153            suppressed: AtomicU64::new(0),
154        }
155    }
156}
157
158/// Rate-limit state for pre-authentication rejection logs, owned by one
159/// `ServerAuth` so each listener (and each test) counts only its own rejects.
160#[derive(Debug)]
161struct PreAuthRejectLog {
162    epoch: Instant,
163    limiter: PreAuthRejectLimiter,
164}
165
166impl PreAuthRejectLog {
167    fn new() -> Self {
168        Self {
169            epoch: Instant::now(),
170            limiter: PreAuthRejectLimiter::new(),
171        }
172    }
173}
174
175// Report accumulated rejects on the next allowed line instead of running a timer task.
176#[derive(Debug)]
177struct PreAuthRejectLimiter {
178    stages: [PreAuthRejectRateLimit; 6],
179}
180
181impl PreAuthRejectLimiter {
182    fn new() -> Self {
183        Self {
184            stages: std::array::from_fn(|_| PreAuthRejectRateLimit::new()),
185        }
186    }
187
188    fn allow_at(&self, stage: PreAuthRejectStage, now: Duration) -> Option<u64> {
189        let slot = &self.stages[stage.index()];
190        let now_ns = now.as_nanos().min(u64::MAX as u128) as u64;
191        let interval_ns = PRE_AUTH_REJECT_LOG_INTERVAL.as_nanos() as u64;
192        let mut next_allowed_ns = slot.next_allowed_ns.load(AtomicOrdering::Relaxed);
193
194        loop {
195            if now_ns < next_allowed_ns {
196                slot.suppressed.fetch_add(1, AtomicOrdering::Relaxed);
197                return None;
198            }
199
200            match slot.next_allowed_ns.compare_exchange_weak(
201                next_allowed_ns,
202                now_ns.saturating_add(interval_ns),
203                AtomicOrdering::Relaxed,
204                AtomicOrdering::Relaxed,
205            ) {
206                Ok(_) => return Some(slot.suppressed.swap(0, AtomicOrdering::Relaxed)),
207                Err(actual) => next_allowed_ns = actual,
208            }
209        }
210    }
211}
212
213fn log_pre_auth_rejection(
214    error: &ConnectionError,
215    peer_addr: SocketAddr,
216    elapsed: Duration,
217    now: Duration,
218    limiter: &PreAuthRejectLimiter,
219) {
220    let Some(stage) = error.pre_auth_reject_stage() else {
221        return;
222    };
223    let Some(suppressed_since_last) = limiter.allow_at(stage, now) else {
224        return;
225    };
226    let elapsed_ms = elapsed.as_millis().min(u64::MAX as u128) as u64;
227
228    info!(
229        peer_addr = %peer_addr,
230        elapsed_ms,
231        stage = stage.stage(),
232        reason = stage.reason(),
233        auth_stage = ?error.auth_stage(),
234        suppressed_since_last,
235        error = %error,
236        "subc TCP connection rejected before routing"
237    );
238}
239
240async fn handle_accepted_connection<S>(
241    stream: S,
242    router: Arc<Router>,
243    auth: ServerAuth,
244    peer_addr: SocketAddr,
245    accepted_at: Instant,
246) -> Result<(), ConnectionError>
247where
248    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
249{
250    let reject_log = Arc::clone(&auth.pre_auth_reject_log);
251    let result = handle_connection(stream, router, auth).await;
252    if let Err(error) = &result {
253        if error.is_quiet_reject() {
254            log_pre_auth_rejection(
255                error,
256                peer_addr,
257                accepted_at.elapsed(),
258                reject_log.epoch.elapsed(),
259                &reject_log.limiter,
260            );
261        } else {
262            warn!(?peer_addr, error = %error, "subc connection ended with error");
263        }
264    }
265    result
266}
267
268/// Authentication material and DoS bounds applied before a TCP connection may
269/// reach the frame router.
270#[derive(Clone)]
271pub struct ServerAuth {
272    key: Arc<[u8]>,
273    daemon_id: [u8; DAEMON_ID_LEN],
274    daemon_ver: Arc<str>,
275    deadline: Duration,
276    unauthenticated: Arc<Semaphore>,
277    connected_clients: ConnectedClients,
278    pre_auth_reject_log: Arc<PreAuthRejectLog>,
279}
280
281impl ServerAuth {
282    pub fn new(
283        key: Vec<u8>,
284        daemon_id: [u8; DAEMON_ID_LEN],
285        daemon_ver: impl Into<String>,
286    ) -> Self {
287        Self::with_limits(
288            key,
289            daemon_id,
290            daemon_ver,
291            DEFAULT_AUTH_DEADLINE,
292            DEFAULT_MAX_UNAUTHENTICATED_CONNECTIONS,
293        )
294    }
295
296    // Production limits are deliberately not config-routed: loosening pre-auth DoS posture changes attack surface.
297    pub fn with_limits(
298        key: Vec<u8>,
299        daemon_id: [u8; DAEMON_ID_LEN],
300        daemon_ver: impl Into<String>,
301        deadline: Duration,
302        max_unauthenticated: usize,
303    ) -> Self {
304        Self {
305            key: Arc::from(key),
306            daemon_id,
307            daemon_ver: Arc::from(daemon_ver.into()),
308            deadline,
309            unauthenticated: Arc::new(Semaphore::new(max_unauthenticated.max(1))),
310            connected_clients: ConnectedClients::new(),
311            pre_auth_reject_log: Arc::new(PreAuthRejectLog::new()),
312        }
313    }
314
315    pub fn with_connected_clients(mut self, connected_clients: ConnectedClients) -> Self {
316        self.connected_clients = connected_clients;
317        self
318    }
319}
320
321impl fmt::Debug for ServerAuth {
322    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323        f.debug_struct("ServerAuth")
324            .field("key", &"<redacted>")
325            .field("daemon_id", &self.daemon_id)
326            .field("daemon_ver", &self.daemon_ver)
327            .field("deadline", &self.deadline)
328            .finish_non_exhaustive()
329    }
330}
331
332/// Serve an already-bound TCP listener. Each accepted connection gets its own
333/// async task so concurrent clients do not block the accept loop.
334pub async fn serve_listener(
335    listener: TcpListener,
336    router: Arc<Router>,
337    auth: ServerAuth,
338) -> Result<(), ServerError> {
339    serve_listener_with_accept(listener.local_addr().ok(), router, auth, || {
340        listener.accept()
341    })
342    .await
343}
344
345async fn serve_listener_with_accept<A, F>(
346    local_addr: Option<SocketAddr>,
347    router: Arc<Router>,
348    auth: ServerAuth,
349    mut accept: A,
350) -> Result<(), ServerError>
351where
352    A: FnMut() -> F,
353    F: std::future::Future<Output = io::Result<(tokio::net::TcpStream, SocketAddr)>>,
354{
355    loop {
356        let (stream, peer_addr) = match accept().await {
357            Ok(accepted) => accepted,
358            Err(source) => {
359                let kind = source.kind();
360                // Aborted/reset connections and interrupted syscalls affect one accept only.
361                // File-descriptor or socket-buffer exhaustion needs a short backoff to avoid spinning;
362                // other errors may mean the listener itself is unusable.
363                let exhausted = accept_resource_exhausted(&source);
364                if matches!(
365                    kind,
366                    io::ErrorKind::ConnectionAborted
367                        | io::ErrorKind::ConnectionReset
368                        | io::ErrorKind::Interrupted
369                ) || exhausted
370                {
371                    warn!(?local_addr, error = %source, "temporary TCP accept failure");
372                    if exhausted {
373                        tokio::time::sleep(Duration::from_millis(75)).await;
374                    }
375                    continue;
376                }
377                return Err(ServerError::Accept { local_addr, source });
378            }
379        };
380        let accepted_at = Instant::now();
381        // Every route frame is a discrete message whose reply the peer is waiting
382        // for, so there is never a later write for Nagle to coalesce with -- it can
383        // only hold a frame back until an ACK arrives.
384        //
385        // MEASURED: no effect on this transport. A 50-sample-per-arm sweep from
386        // 1 to 32 KiB over the full client->daemon->module->client path showed a
387        // flat 0.28-0.38ms p50 with no step at any buffer boundary, because a
388        // loopback ACK returns in microseconds and never reaches the delayed-ACK
389        // timer that makes Nagle expensive on a real network. Kept anyway: it is
390        // one syscall at accept, it removes the mechanism rather than relying on
391        // loopback staying fast, and Windows loopback was not part of that sweep.
392        // Do not cite it as a latency fix -- the measurement says it is not one.
393        //
394        // Failure is not fatal: the connection works, and refusing to serve a
395        // client over a socket option would be worse than anything it saves.
396        if let Err(source) = stream.set_nodelay(true) {
397            warn!(?peer_addr, error = %source, "could not disable Nagle on accepted connection");
398        }
399        debug!(?peer_addr, ?local_addr, "accepted subc TCP connection");
400        let router = Arc::clone(&router);
401        let auth = auth.clone();
402        tokio::spawn(async move {
403            let _ = handle_accepted_connection(stream, router, auth, peer_addr, accepted_at).await;
404        });
405    }
406}
407
408#[cfg_attr(not(unix), allow(dead_code))]
409fn temporary_accept_failure(source: &io::Error) -> bool {
410    matches!(
411        source.kind(),
412        io::ErrorKind::ConnectionAborted
413            | io::ErrorKind::ConnectionReset
414            | io::ErrorKind::Interrupted
415    ) || accept_resource_exhausted(source)
416}
417
418fn accept_resource_exhausted(error: &io::Error) -> bool {
419    // ENFILE/EMFILE/ENOBUFS by name rather than number: the numbers differ by
420    // platform (ENOBUFS is 55 on macOS and 105 on Linux, and each number means
421    // something unrelated on the other), so a literal list misclassifies.
422    // Winsock: WSAEMFILE and WSAENOBUFS.
423    #[cfg(unix)]
424    {
425        use rustix::io::Errno;
426        let exhausted = [Errno::NFILE, Errno::MFILE, Errno::NOBUFS];
427        error
428            .raw_os_error()
429            .is_some_and(|code| exhausted.iter().any(|e| e.raw_os_error() == code))
430    }
431    #[cfg(windows)]
432    {
433        matches!(error.raw_os_error(), Some(10024 | 10055))
434    }
435    #[cfg(not(any(unix, windows)))]
436    {
437        let _ = error;
438        false
439    }
440}
441
442/// Serve this daemon's local endpoint (a Unix-domain socket, or a Windows named
443/// pipe) until its accept loop fails.
444///
445/// Every connection gets the same key handshake, frame format and router as a
446/// TCP client, so a client's bytes are identical either way. What differs is
447/// that this endpoint serves clients only: a HELLO that would register a module
448/// is refused with [`crate::local_listener::LOCAL_ENDPOINT_CLIENTS_ONLY`]. A
449/// peer read failure costs that one connection; a failure to accept at all ends
450/// the loop, because a local listener that cannot accept is no longer an
451/// endpoint.
452pub async fn serve_local_listener(
453    bound: crate::local_listener::BoundLocalListener,
454    router: Arc<Router>,
455    auth: ServerAuth,
456) -> Result<(), ServerError> {
457    match bound {
458        #[cfg(unix)]
459        crate::local_listener::BoundLocalListener::Unix { listener, path } => {
460            serve_unix_local(listener, path, router, auth).await
461        }
462        #[cfg(windows)]
463        crate::local_listener::BoundLocalListener::Windows {
464            pipe_name,
465            first_instance,
466        } => serve_pipe_local(pipe_name, first_instance, router, auth).await,
467        // The variants above exist only on their own platform.
468        #[allow(unreachable_patterns)]
469        _ => Err(ServerError::NoListeners),
470    }
471}
472
473#[cfg(unix)]
474async fn serve_unix_local(
475    listener: std::os::unix::net::UnixListener,
476    path: std::path::PathBuf,
477    router: Arc<Router>,
478    auth: ServerAuth,
479) -> Result<(), ServerError> {
480    // The socket file is removed when this future ends, whether it returns or is
481    // dropped during shutdown, so a later daemon start finds no leftover.
482    let _release = SocketFileRelease(path.clone());
483    listener
484        .set_nonblocking(true)
485        .map_err(|source| local_accept_error(&path, source))?;
486    let listener = tokio::net::UnixListener::from_std(listener)
487        .map_err(|source| local_accept_error(&path, source))?;
488    loop {
489        let (stream, _addr) = match listener.accept().await {
490            Ok(accepted) => accepted,
491            Err(source) if temporary_accept_failure(&source) => {
492                warn!(error = %source, "temporary local-socket accept failure");
493                if accept_resource_exhausted(&source) {
494                    tokio::time::sleep(Duration::from_millis(75)).await;
495                }
496                continue;
497            }
498            Err(source) => return Err(local_accept_error(&path, source)),
499        };
500        debug!(?path, "accepted a local-socket connection");
501        let peer = subc_os::local_peer::capture_unix_peer(&stream);
502        let router = Arc::clone(&router);
503        let auth = auth.clone();
504        tokio::spawn(async move {
505            let _ = handle_accepted_local_connection(stream, router, auth, peer).await;
506        });
507    }
508}
509
510#[cfg(unix)]
511fn local_accept_error(endpoint: &std::path::Path, source: io::Error) -> ServerError {
512    ServerError::LocalAccept {
513        endpoint: endpoint.display().to_string(),
514        source,
515    }
516}
517
518#[cfg(unix)]
519struct SocketFileRelease(std::path::PathBuf);
520
521#[cfg(unix)]
522impl Drop for SocketFileRelease {
523    fn drop(&mut self) {
524        crate::local_listener::remove_unix_socket(&self.0);
525    }
526}
527
528#[cfg(windows)]
529async fn serve_pipe_local(
530    pipe_name: String,
531    first_instance: Option<tokio::net::windows::named_pipe::NamedPipeServer>,
532    router: Arc<Router>,
533    auth: ServerAuth,
534) -> Result<(), ServerError> {
535    // The end a client may connect to. An end exists whenever the loop is not
536    // accepting one, because Win32 refuses a client that finds none, and no
537    // other task accepts ends here.
538    let mut free = first_instance;
539    loop {
540        let server = match free.take() {
541            Some(server) => server,
542            None => create_pipe_instance(&pipe_name)?,
543        };
544        // Overlapped I/O: the accepted end is AsyncRead and AsyncWrite, so a
545        // connection's reader and writer never wait for each other. A
546        // synchronous handle would, and the daemon's reply would sit behind the
547        // reader's pending read while the client waited for that reply.
548        if let Err(source) = server.connect().await {
549            if temporary_accept_failure(&source) {
550                continue;
551            }
552            return Err(ServerError::LocalAccept {
553                endpoint: pipe_name.clone(),
554                source,
555            });
556        }
557        // The next end is made before this one is served, so a client that
558        // arrives while this one is being served still finds an end.
559        free = Some(create_pipe_instance(&pipe_name)?);
560        let router = Arc::clone(&router);
561        let auth = auth.clone();
562        // Refuse network clients before observing a process or reading a byte.
563        // The pipe's PIPE_REJECT_REMOTE_CLIENTS flag is the first gate.
564        if let Err(error) = subc_os::named_pipe::reject_remote_client(&server) {
565            warn!(%error, "refused a non-local named-pipe client on the local endpoint");
566            continue;
567        }
568        let peer = subc_os::local_peer::observe_pipe_client(&server).into();
569        tokio::spawn(async move {
570            let _ = handle_accepted_local_connection(server, router, auth, peer).await;
571        });
572    }
573}
574
575#[cfg(windows)]
576fn create_pipe_instance(
577    pipe_name: &str,
578) -> Result<tokio::net::windows::named_pipe::NamedPipeServer, ServerError> {
579    crate::local_listener::create_named_pipe_end(pipe_name).map_err(|source| {
580        ServerError::LocalAccept {
581            endpoint: pipe_name.to_owned(),
582            source,
583        }
584    })
585}
586
587/// Run one local-endpoint connection: the same handshake as TCP, with the
588/// clients-only rule attached after it.
589async fn handle_accepted_local_connection<S>(
590    stream: S,
591    router: Arc<Router>,
592    auth: ServerAuth,
593    peer: subc_os::local_peer::CapturedPeer,
594) -> Result<(), ConnectionError>
595where
596    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
597{
598    let result =
599        handle_connection_with_peer(stream, router, auth, LocalOrigin::Local, Some(peer)).await;
600    if let Err(error) = &result {
601        debug!(error = %error, "local-endpoint connection ended");
602    }
603    result
604}
605
606/// Where a connection came in. The local endpoint serves clients only, because
607/// a module registers over TCP, where the daemon can tell a launched process
608/// apart by the launch nonce in its HELLO.
609#[derive(Debug, Clone, Copy, PartialEq, Eq)]
610pub(crate) enum LocalOrigin {
611    Tcp,
612    Local,
613}
614
615/// Serve all already-bound loopback TCP listeners until one accept loop fails.
616pub async fn serve_listeners(
617    listeners: Vec<TcpListener>,
618    router: Arc<Router>,
619    auth: ServerAuth,
620) -> Result<(), ServerError> {
621    if listeners.is_empty() {
622        return Err(ServerError::NoListeners);
623    }
624
625    let (tx, mut rx) = mpsc::channel(listeners.len());
626    let mut accept_tasks = AbortTasksOnDrop::default();
627    for listener in listeners {
628        let router = Arc::clone(&router);
629        let auth = auth.clone();
630        let tx = tx.clone();
631        accept_tasks.push(tokio::spawn(async move {
632            let result = serve_listener(listener, router, auth).await;
633            let _ = tx.send(result).await;
634        }));
635    }
636    drop(tx);
637
638    rx.recv().await.unwrap_or(Ok(()))
639}
640
641#[derive(Default)]
642struct AbortTasksOnDrop {
643    handles: Vec<JoinHandle<()>>,
644}
645
646impl AbortTasksOnDrop {
647    fn push(&mut self, handle: JoinHandle<()>) {
648        self.handles.push(handle);
649    }
650}
651
652impl Drop for AbortTasksOnDrop {
653    fn drop(&mut self) {
654        for handle in &self.handles {
655            if !handle.is_finished() {
656                handle.abort();
657            }
658        }
659    }
660}
661
662#[derive(Debug)]
663enum ConnectionLoopExit {
664    PeerClosed,
665    CloseRequested(CloseReason),
666}
667
668/// Run the authenticated frame read -> route loop for one connection.
669///
670/// Every accepted TCP connection must complete the key-auth prelude before any
671/// envelope bytes are read by the router. Outbound frames flow through a bounded
672/// [`FrameSink`] drained by one writer task. Inbound dispatch remains serial for
673/// every frame except `route.open`, activity operations and privileged requests;
674/// their bind, file and operator waits run in connection-owned tasks so
675/// unrelated frames can keep flowing.
676pub async fn handle_connection<S>(
677    stream: S,
678    router: Arc<Router>,
679    auth: ServerAuth,
680) -> Result<(), ConnectionError>
681where
682    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
683{
684    handle_connection_with_origin(stream, router, auth, LocalOrigin::Tcp).await
685}
686
687/// [`handle_connection`] for one endpoint kind: `origin` decides whether a
688/// module HELLO is refused before it can register.
689pub(crate) async fn handle_connection_with_origin<S>(
690    stream: S,
691    router: Arc<Router>,
692    auth: ServerAuth,
693    origin: LocalOrigin,
694) -> Result<(), ConnectionError>
695where
696    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
697{
698    handle_connection_with_peer(stream, router, auth, origin, None).await
699}
700
701async fn handle_connection_with_peer<S>(
702    mut stream: S,
703    router: Arc<Router>,
704    auth: ServerAuth,
705    origin: LocalOrigin,
706    peer: Option<subc_os::local_peer::CapturedPeer>,
707) -> Result<(), ConnectionError>
708where
709    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
710{
711    // Capture only socket facts here. Process-table lookup happens lazily on a
712    // blocking thread when privileged dispatch needs a caller description.
713    let endpoints = (&stream as &dyn Any)
714        .downcast_ref::<tokio::net::TcpStream>()
715        .and_then(|tcp| tcp.local_addr().ok().zip(tcp.peer_addr().ok()));
716    // Over-cap connections WAIT for a handshake slot (bounded by the auth deadline)
717    // instead of being reset. A restart herd — every client and supervised child
718    // re-dialing the fresh daemon at once — otherwise loses supervised modules to
719    // the permit lottery: each reset burns a module restart-budget slot, and a
720    // module that treats auth failure as fatal can exhaust its budget into
721    // state=failed within the boot window (2026-07-14 aft outage). On loopback
722    // with the pre-auth HMAC deadline, a bounded queue is strictly safer than a
723    // reset.
724    //
725    // Pre-auth time is governed by TWO deliberately separate budgets: the queue
726    // wait below is bounded by `auth.deadline`, and `authenticate_server` then
727    // starts a FRESH `auth.deadline` for the handshake itself. Total pre-auth
728    // occupancy per connection is therefore up to 2x the configured deadline.
729    // This is intentional, not an accounting slip: charging queue time against
730    // the handshake budget would hand a herd-queued supervised module a
731    // near-zero handshake window under CPU saturation, recreating the fatal
732    // auth-failure -> restart-budget-burn path this queue exists to prevent.
733    // On a loopback-only, key-authenticated listener the doubled bound is a
734    // per-connection occupancy cost, not a meaningful DoS surface.
735    let permit =
736        match tokio::time::timeout(auth.deadline, auth.unauthenticated.clone().acquire_owned())
737            .await
738        {
739            Ok(Ok(permit)) => permit,
740            Ok(Err(_)) | Err(_) => {
741                let _ = stream.shutdown().await;
742                return Err(ConnectionError::UnauthenticatedCapacity);
743            }
744        };
745
746    let authenticated = authenticate_server(
747        &mut stream,
748        auth.key.as_ref(),
749        &auth.daemon_id,
750        auth.daemon_ver.as_ref(),
751        auth.deadline,
752    )
753    .await
754    .map_err(ConnectionError::Auth)?;
755    drop(permit);
756
757    let mut connection = router.begin_connection();
758    let connection_id = connection.id();
759    router.record_caller_endpoints(connection_id, endpoints);
760    if let Some(peer) = peer {
761        router.record_captured_peer(connection_id, peer);
762    }
763    // `authenticated.role` is CLIENT-SUPPLIED and unverified -- the handshake
764    // proves possession of the connection key, nothing about who is calling. So
765    // this exclusion is safe ONLY while the role decides a reporting question and
766    // never an authorization one: the watchdog's own loopback probe would
767    // otherwise inflate the very count it exists to sanity-check.
768    //
769    // The consequence of it being unverified, stated so nobody has to rediscover
770    // it: ANY client holding the key can claim this role and omit itself from
771    // `connected_clients`. That is a gauge a caller can lie to, and it is
772    // acceptable because the gauge informs an operator rather than gating
773    // anything. IF A ROLE IS EVER USED TO DECIDE ADMISSION, CAPACITY, OR PRIVILEGE,
774    // this stops being safe and the role must be attested rather than declared --
775    // the daemon already has the mechanism for that in the spawn-nonce path used
776    // for module identity.
777    let _connected_client = (authenticated.role != WATCHDOG_CLIENT_ROLE)
778        .then(|| auth.connected_clients.open(connection_id));
779    let close_receiver = connection.take_close_receiver();
780    debug!(
781        connection_id = connection_id.get(),
782        "subc authenticated connection opened"
783    );
784
785    let (read_half, write_half) = tokio::io::split(stream);
786    // Authentication is complete before this buffer can read ahead. The connection loop
787    // retains a partial frame read across completed route.open tasks; dropping the read
788    // happens only when a frame finishes or the connection ends.
789    let mut read_half = BufReader::new(read_half);
790    let (egress, rx) = connection_egress();
791    let delivery_guard = ClientDeliveryGuard(egress.clone());
792    let writer = tokio::spawn(drain_writer(write_half, rx));
793
794    let ctx = RouteCtx {
795        connection_id,
796        egress: egress.clone(),
797    };
798
799    let mut route_open_tasks = JoinSet::new();
800    let mut privileged_tasks = JoinSet::new();
801    let loop_result = connection_loop(
802        &mut read_half,
803        Arc::clone(&router),
804        ctx.clone(),
805        close_receiver,
806        &mut route_open_tasks,
807        &mut privileged_tasks,
808        origin,
809    )
810    .await;
811
812    drop(delivery_guard);
813    // Every in-flight route.open must have FINISHED before `connection` drops,
814    // because that drop runs the forwarding cleanup for this id, and cleanup is
815    // also where the id's closing mark is lifted. Dropping a JoinSet only
816    // requests abort: a task already running on another worker keeps going
817    // until its next await, and could commit a route for this connection after
818    // cleanup has cleared it, with nothing left to refuse it and nothing to
819    // clean it up. shutdown() aborts and then waits.
820    route_open_tasks.shutdown().await;
821    // Abort held approvals and wait for their withdrawal guards before lifting
822    // this connection's cleanup state or removing its caller endpoints.
823    privileged_tasks.shutdown().await;
824
825    drop(ctx);
826    drop(egress);
827    drop(connection);
828
829    let close_reason = match &loop_result {
830        Ok(ConnectionLoopExit::CloseRequested(reason)) => Some(reason.to_string()),
831        Ok(ConnectionLoopExit::PeerClosed) | Err(_) => None,
832    };
833    let writer_result = finish_connection_writer(writer, connection_id.get()).await;
834
835    let result = if let Some(reason) = close_reason.as_deref() {
836        match writer_result {
837            Some(Ok(Ok(()))) | None => Ok(()),
838            Some(Ok(Err(writer_err))) => {
839                debug!(
840                    connection_id = connection_id.get(),
841                    close_reason = reason,
842                    writer_error = %writer_err,
843                    "writer failed after requested connection close"
844                );
845                Ok(())
846            }
847            Some(Err(join_err)) => {
848                warn!(
849                    connection_id = connection_id.get(),
850                    close_reason = reason,
851                    join_error = %join_err,
852                    "writer task join failed after requested connection close"
853                );
854                Ok(())
855            }
856        }
857    } else {
858        match (loop_result, writer_result) {
859            (Err(loop_err), Some(Ok(Ok(()))) | None) => Err(loop_err),
860            (Err(loop_err), Some(Ok(Err(writer_err)))) => {
861                warn!(
862                    connection_id = connection_id.get(),
863                    writer_error = %writer_err,
864                    "writer failed while closing after connection error"
865                );
866                Err(loop_err)
867            }
868            (Err(loop_err), Some(Err(join_err))) => {
869                warn!(
870                    connection_id = connection_id.get(),
871                    join_error = %join_err,
872                    "writer task join failed while closing after connection error"
873                );
874                Err(loop_err)
875            }
876            (Ok(ConnectionLoopExit::PeerClosed), Some(Ok(Ok(()))) | None) => Ok(()),
877            (Ok(ConnectionLoopExit::PeerClosed), Some(Ok(Err(writer_err)))) => {
878                Err(ConnectionError::FrameIo(writer_err))
879            }
880            (Ok(ConnectionLoopExit::PeerClosed), Some(Err(join_err))) => Err(join_err),
881            (Ok(ConnectionLoopExit::CloseRequested(_)), _) => {
882                unreachable!("close requests are handled before normal writer result matching")
883            }
884        }
885    };
886
887    match &result {
888        Ok(()) => {
889            if let Some(reason) = close_reason.as_deref() {
890                debug!(
891                    connection_id = connection_id.get(),
892                    close_reason = reason,
893                    "subc connection closed by request"
894                );
895            } else {
896                debug!(
897                    connection_id = connection_id.get(),
898                    "subc connection closed"
899                );
900            }
901        }
902        Err(err) => debug!(
903            connection_id = connection_id.get(),
904            error = %err,
905            "subc connection exited with error"
906        ),
907    }
908
909    result
910}
911
912async fn finish_connection_writer(
913    mut writer: JoinHandle<Result<(), FrameIoError>>,
914    connection_id: u64,
915) -> Option<Result<Result<(), FrameIoError>, ConnectionError>> {
916    // Even after inbound EOF or an error, the peer may keep its receive side
917    // open without reading. Give queued frames the same grace on every exit.
918    match timeout(CLOSE_DRAIN_GRACE, &mut writer).await {
919        Ok(result) => Some(result.map_err(ConnectionError::WriterTask)),
920        Err(_) => {
921            warn!(
922                connection_id,
923                grace = ?CLOSE_DRAIN_GRACE,
924                "connection writer did not drain during teardown; aborting writer task"
925            );
926            writer.abort();
927            let _ = writer.await;
928            None
929        }
930    }
931}
932
933async fn connection_loop<R>(
934    read_half: &mut R,
935    router: Arc<Router>,
936    ctx: RouteCtx,
937    mut close_receiver: ConnectionCloseReceiver,
938    route_open_tasks: &mut JoinSet<Result<(), RouterError>>,
939    privileged_tasks: &mut JoinSet<Result<(), RouterError>>,
940    origin: LocalOrigin,
941) -> Result<ConnectionLoopExit, ConnectionError>
942where
943    R: AsyncRead + Unpin,
944{
945    let mut activity_tasks = JoinSet::new();
946    loop {
947        while let Some(result) = activity_tasks.try_join_next() {
948            finish_route_open_task(result)?;
949        }
950        while let Some(result) = privileged_tasks.try_join_next() {
951            finish_route_open_task(result)?;
952        }
953        while let Some(result) = route_open_tasks.try_join_next() {
954            finish_route_open_task(result)?;
955        }
956
957        // Keep the same read future when a route.open completes: read_frame owns
958        // partial header/body buffers that would be lost if that future were dropped.
959        let read = read_frame(&mut *read_half);
960        tokio::pin!(read);
961        let frame = loop {
962            tokio::select! {
963                close = &mut close_receiver => {
964                    return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
965                }
966                result = route_open_tasks.join_next(), if !route_open_tasks.is_empty() => {
967                    finish_route_open_task(
968                        result.expect("a non-empty route.open JoinSet has a next task")
969                    )?;
970                }
971                result = activity_tasks.join_next(), if !activity_tasks.is_empty() => {
972                    finish_route_open_task(result.expect("non-empty activity task set"))?;
973                }
974                result = privileged_tasks.join_next(), if !privileged_tasks.is_empty() => {
975                    finish_route_open_task(result.expect("non-empty privileged task set"))?;
976                }
977                result = &mut read => {
978                    break match result.map_err(ConnectionError::FrameIo)? {
979                        Some(frame) => frame,
980                        None => return Ok(ConnectionLoopExit::PeerClosed),
981                    };
982                }
983            }
984        };
985
986        if origin == LocalOrigin::Local && frame.header.ty == subc_protocol::FrameType::Hello {
987            // A module registers over TCP only. Refusing here, before the router
988            // sees the frame, is what guarantees the local endpoint leaves no
989            // registration, catalog entry or route behind, whatever the HELLO
990            // carries.
991            let refusal = crate::control::control_error_frame(
992                &frame,
993                crate::local_listener::LOCAL_ENDPOINT_CLIENTS_ONLY,
994                "the local endpoint serves clients only; modules register over TCP",
995            )
996            .map_err(ConnectionError::Router)?;
997            // The connection ends as soon as the refusal is sent, so the bytes
998            // must reach the socket first: a client that has not yet read its
999            // reply would otherwise see only the end of the stream.
1000            let send_result = tokio::select! {
1001                close = &mut close_receiver => {
1002                    return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
1003                }
1004                result = send_refusal(&ctx.egress, refusal) => result,
1005            };
1006            send_result.map_err(ConnectionError::Router)?;
1007            return Ok(ConnectionLoopExit::PeerClosed);
1008        }
1009
1010        if frame.header.channel == 0
1011            && frame.header.ty == subc_protocol::FrameType::Request
1012            && serde_json::from_slice::<serde_json::Value>(&frame.body)
1013                .ok()
1014                .is_some_and(|body| {
1015                    matches!(
1016                        body["op"].as_str(),
1017                        Some(
1018                            subc_control::ops::ACTIVITY_RECORD
1019                                | subc_control::ops::ACTIVITY_QUERY
1020                                | subc_control::ops::ACTIVITY_SUBSCRIBE
1021                        )
1022                    )
1023                })
1024        {
1025            // Bound outstanding activity tasks as well as the disk queue,
1026            // so the reader never waits for either capacity when the disk stalls.
1027            if activity_tasks.len() >= 128 {
1028                let response = crate::control::control_error_frame(
1029                    &frame,
1030                    subc_control::ACTIVITY_UNAVAILABLE,
1031                    "activity queue full",
1032                )
1033                .map_err(ConnectionError::Router)?;
1034                let send_result = tokio::select! {
1035                    close = &mut close_receiver => {
1036                        return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
1037                    }
1038                    result = ctx.egress.send(response) => result,
1039                };
1040                send_result.map_err(ConnectionError::Router)?;
1041            } else {
1042                let task_router = Arc::clone(&router);
1043                let task_ctx = ctx.clone();
1044                activity_tasks.spawn(async move {
1045                    route_open_tail(task_router, task_ctx, frame, Instant::now()).await
1046                });
1047            }
1048            continue;
1049        }
1050
1051        if let Some(target_module_id) = router.route_open_target(&frame) {
1052            // A task can finish while the reader is waiting for the next frame.
1053            // Reap it before checking admission so completed work never occupies
1054            // one of the deliberately scarce connection slots.
1055            while let Some(result) = route_open_tasks.try_join_next() {
1056                finish_route_open_task(result)?;
1057            }
1058
1059            if route_open_tasks.len() >= MAX_PENDING_ROUTE_OPENS_PER_CONNECTION {
1060                // Never wait for capacity here: doing so would recreate the same
1061                // reader head-of-line stall with a smaller constant.
1062                let refusal = router
1063                    .route_open_capacity_refusal(
1064                        &ctx,
1065                        &frame,
1066                        &target_module_id,
1067                        route_open_tasks.len(),
1068                        MAX_PENDING_ROUTE_OPENS_PER_CONNECTION,
1069                    )
1070                    .map_err(ConnectionError::Router)?;
1071                let send_result = tokio::select! {
1072                    close = &mut close_receiver => {
1073                        return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
1074                    }
1075                    result = ctx.egress.send(refusal) => result,
1076                };
1077                send_result.map_err(ConnectionError::Router)?;
1078                continue;
1079            }
1080
1081            let task_router = Arc::clone(&router);
1082            let task_ctx = ctx.clone();
1083            // Start slow-dispatch timing before spawn so it covers the task's
1084            // full scheduler and handler lifetime, as inline timing did.
1085            let dispatch_started_at = Instant::now();
1086            route_open_tasks.spawn(async move {
1087                route_open_tail(task_router, task_ctx, frame, dispatch_started_at).await
1088            });
1089            continue;
1090        }
1091
1092        if router.defers_privileged_control(&ctx, &frame) {
1093            let task_router = Arc::clone(&router);
1094            let task_ctx = ctx.clone();
1095            let started = Instant::now();
1096            privileged_tasks
1097                .spawn(async move { route_open_tail(task_router, task_ctx, frame, started).await });
1098            continue;
1099        }
1100
1101        // Other frames still route serially: routing can backpressure reads,
1102        // and a connection close cancels the in-progress route.
1103        let route_result = tokio::select! {
1104            close = &mut close_receiver => {
1105                return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
1106            }
1107            result = router.route_for_connection(&ctx, frame) => result,
1108        };
1109
1110        if let Err(err) = route_result {
1111            if let Some(error_frame) = err.to_error_frame() {
1112                warn!(
1113                    connection_id = ctx.connection_id.get(),
1114                    error = %err,
1115                    "routing failure recovered with ERROR frame"
1116                );
1117                let send_result = tokio::select! {
1118                    close = &mut close_receiver => {
1119                        return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
1120                    }
1121                    result = ctx.egress.send(error_frame) => result,
1122                };
1123                send_result.map_err(ConnectionError::Router)?;
1124            } else {
1125                debug!(
1126                    connection_id = ctx.connection_id.get(),
1127                    error = %err,
1128                    "fatal routing failure"
1129                );
1130                return Err(ConnectionError::Router(err));
1131            }
1132        }
1133    }
1134}
1135
1136/// Send one frame so that a connection ending immediately afterwards still
1137/// delivers it: on Unix, wait for the writer's flush acknowledgement; elsewhere
1138/// queue it, because those writers carry no acknowledgement.
1139async fn send_refusal(sink: &FrameSink, frame: crate::Frame) -> Result<(), RouterError> {
1140    // The flush acknowledgement comes from the writer's `write_frame`, which
1141    // only the Unix builds expose; on Windows the refusal is queued for the pipe
1142    // writer instead.
1143    #[cfg(unix)]
1144    {
1145        sink.send_flushed(frame).await
1146    }
1147    #[cfg(not(unix))]
1148    {
1149        sink.send(frame).await
1150    }
1151}
1152
1153async fn route_open_tail(
1154    router: Arc<Router>,
1155    ctx: RouteCtx,
1156    frame: crate::Frame,
1157    dispatch_started_at: Instant,
1158) -> Result<(), RouterError> {
1159    match router
1160        .route_for_connection_started(&ctx, frame, Some(dispatch_started_at))
1161        .await
1162    {
1163        Ok(()) => Ok(()),
1164        Err(err) => {
1165            let Some(error_frame) = err.to_error_frame() else {
1166                return Err(err);
1167            };
1168            warn!(
1169                connection_id = ctx.connection_id.get(),
1170                error = %err,
1171                "routing failure recovered with ERROR frame"
1172            );
1173            ctx.egress.send(error_frame).await
1174        }
1175    }
1176}
1177
1178fn finish_route_open_task(
1179    result: Result<Result<(), RouterError>, tokio::task::JoinError>,
1180) -> Result<(), ConnectionError> {
1181    match result {
1182        Ok(Ok(())) => Ok(()),
1183        Ok(Err(err)) => Err(ConnectionError::Router(err)),
1184        Err(err) => Err(ConnectionError::Router(RouterError::backend(
1185            0,
1186            0,
1187            format!("route.open task failed: {err}"),
1188        ))),
1189    }
1190}
1191
1192fn close_reason(
1193    result: Result<CloseReason, tokio::sync::oneshot::error::RecvError>,
1194) -> CloseReason {
1195    result.unwrap_or_else(|_| {
1196        CloseReason::new(
1197            "close_registry_dropped",
1198            "connection close registration was dropped without a reason",
1199        )
1200    })
1201}
1202
1203/// The outbound queue for one daemon connection: a sink bounded by
1204/// [`CONNECTION_EGRESS_BYTE_BUDGET`] and [`CONNECTION_EGRESS_FRAME_CAP`], and
1205/// the receiver its writer drains.
1206pub(crate) fn connection_egress() -> (FrameSink, mpsc::Receiver<crate::router::OutboundFrame>) {
1207    let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(CONNECTION_EGRESS_FRAME_CAP);
1208    (
1209        FrameSink::with_byte_budget(tx, CONNECTION_EGRESS_BYTE_BUDGET),
1210        rx,
1211    )
1212}
1213
1214async fn drain_writer<W>(
1215    write_half: W,
1216    mut rx: mpsc::Receiver<crate::router::OutboundFrame>,
1217) -> Result<(), FrameIoError>
1218where
1219    W: AsyncWrite + Unpin,
1220{
1221    let mut writer = BufWriter::new(write_half);
1222    while let Some(outbound) = rx.recv().await {
1223        write_outbound(&mut writer, outbound).await?;
1224        while let Ok(outbound) = rx.try_recv() {
1225            write_outbound(&mut writer, outbound).await?;
1226        }
1227        writer.flush().await.map_err(FrameIoError::Io)?;
1228    }
1229    writer.flush().await.map_err(FrameIoError::Io)?;
1230    Ok(())
1231}
1232
1233/// Reply-path half of slow-control diagnosis: a channel-0 reply that sat in
1234/// the writer queue past the threshold is reported with its queue residency
1235/// and its own write duration separated, because "writer task not scheduled"
1236/// and "socket write blocked" are different defects and the sum hides which.
1237/// Data-plane frames are exempt: their latency is the client's own flow
1238/// control, and logging them would drown the control signal in bulk traffic.
1239async fn write_outbound<W>(
1240    writer: &mut BufWriter<W>,
1241    outbound: crate::router::OutboundFrame,
1242) -> Result<(), FrameIoError>
1243where
1244    W: AsyncWrite + Unpin,
1245{
1246    const SLOW_REPLY_QUEUE: Duration = Duration::from_millis(1000);
1247    let queued = outbound.enqueued_at.elapsed();
1248    // Held until this frame has been written, then dropped at the end of this
1249    // function, which gives its bytes back to the connection's egress budget.
1250    let charge = outbound.charge;
1251    if let Some(charge) = &charge {
1252        charge.taken_by_writer();
1253    }
1254    let frame = outbound.frame;
1255    if frame.header.channel == 0 && queued >= SLOW_REPLY_QUEUE {
1256        let write_started = std::time::Instant::now();
1257        let result = write_frame(writer, &frame).await;
1258        tracing::warn!(
1259            corr = frame.header.corr,
1260            queued_ms = queued.as_millis() as u64,
1261            write_ms = write_started.elapsed().as_millis() as u64,
1262            "slow control reply write"
1263        );
1264        result?;
1265    } else {
1266        write_frame(writer, &frame).await?;
1267    }
1268    if let Some(flushed) = outbound.flushed {
1269        writer.flush().await.map_err(FrameIoError::Io)?;
1270        let _ = flushed.send(());
1271    }
1272    Ok(())
1273}
1274
1275#[cfg(all(test, unix))]
1276#[tokio::test]
1277async fn shutdown_notice_ack_waits_for_socket_flush() {
1278    let (socket, mut peer) = tokio::io::duplex(1);
1279    let (tx, rx) = mpsc::channel(1);
1280    let sink = FrameSink::new(tx);
1281    let writer = tokio::spawn(drain_writer(socket, rx));
1282    let frame = crate::Frame::build(
1283        subc_protocol::FrameType::Push,
1284        subc_protocol::Flags::new(false, subc_protocol::Priority::Interactive, false),
1285        0,
1286        0,
1287        0,
1288        b"notice".to_vec(),
1289    )
1290    .unwrap();
1291    let send = sink.send_flushed(frame);
1292    tokio::pin!(send);
1293    assert!(
1294        timeout(Duration::from_millis(20), &mut send).await.is_err(),
1295        "queueing bytes is not a socket flush acknowledgement"
1296    );
1297    let (sent, received) = timeout(Duration::from_secs(1), async {
1298        tokio::join!(&mut send, read_frame(&mut peer))
1299    })
1300    .await
1301    .unwrap();
1302    sent.unwrap();
1303    assert_eq!(received.unwrap().unwrap().body, b"notice");
1304    writer.abort();
1305}
1306
1307#[derive(Debug)]
1308pub enum ServerError {
1309    NoListeners,
1310    Accept {
1311        local_addr: Option<SocketAddr>,
1312        source: io::Error,
1313    },
1314    /// The local endpoint (a Unix-domain socket or a named pipe) could not be
1315    /// created or could not accept.
1316    LocalAccept {
1317        endpoint: String,
1318        source: io::Error,
1319    },
1320}
1321
1322impl fmt::Display for ServerError {
1323    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1324        match self {
1325            Self::NoListeners => write!(f, "no TCP listeners were provided"),
1326            Self::Accept { local_addr, source } => match local_addr {
1327                Some(addr) => write!(f, "failed to accept TCP connection on {addr}: {source}"),
1328                None => write!(f, "failed to accept TCP connection: {source}"),
1329            },
1330            Self::LocalAccept { endpoint, source } => {
1331                write!(f, "local endpoint {endpoint} failed: {source}")
1332            }
1333        }
1334    }
1335}
1336
1337impl Error for ServerError {
1338    fn source(&self) -> Option<&(dyn Error + 'static)> {
1339        match self {
1340            Self::Accept { source, .. } | Self::LocalAccept { source, .. } => Some(source),
1341            Self::NoListeners => None,
1342        }
1343    }
1344}
1345
1346#[derive(Debug)]
1347pub enum ConnectionError {
1348    Auth(AuthError),
1349    UnauthenticatedCapacity,
1350    FrameIo(FrameIoError),
1351    Router(RouterError),
1352    WriterTask(tokio::task::JoinError),
1353}
1354
1355impl ConnectionError {
1356    fn is_quiet_reject(&self) -> bool {
1357        matches!(self, Self::Auth(_) | Self::UnauthenticatedCapacity)
1358    }
1359
1360    fn pre_auth_reject_stage(&self) -> Option<PreAuthRejectStage> {
1361        match self {
1362            Self::UnauthenticatedCapacity => Some(PreAuthRejectStage::WaitingForHandshakeSlot),
1363            Self::Auth(AuthError::Timeout { .. }) => Some(PreAuthRejectStage::HandshakeDeadline),
1364            Self::Auth(
1365                AuthError::InvalidClientAuth
1366                | AuthError::InvalidServerProof
1367                | AuthError::DaemonIdMismatch,
1368            ) => Some(PreAuthRejectStage::InvalidProof),
1369            Self::Auth(AuthError::MessageTooLarge { .. } | AuthError::JsonDecode { .. }) => {
1370                Some(PreAuthRejectStage::MalformedMessage)
1371            }
1372            Self::Auth(AuthError::UnexpectedEof { .. }) => Some(PreAuthRejectStage::PeerClosed),
1373            Self::Auth(AuthError::Io { source, .. })
1374                if matches!(
1375                    source.kind(),
1376                    io::ErrorKind::BrokenPipe
1377                        | io::ErrorKind::ConnectionAborted
1378                        | io::ErrorKind::ConnectionReset
1379                        | io::ErrorKind::UnexpectedEof
1380                ) =>
1381            {
1382                Some(PreAuthRejectStage::PeerClosed)
1383            }
1384            Self::Auth(_) => Some(PreAuthRejectStage::OtherAuthenticationFailure),
1385            Self::FrameIo(_) | Self::Router(_) | Self::WriterTask(_) => None,
1386        }
1387    }
1388
1389    fn auth_stage(&self) -> Option<AuthStage> {
1390        match self {
1391            Self::Auth(
1392                AuthError::Io { stage, .. }
1393                | AuthError::Timeout { stage, .. }
1394                | AuthError::UnexpectedEof { stage, .. }
1395                | AuthError::MessageTooLarge { stage, .. }
1396                | AuthError::JsonEncode { stage, .. }
1397                | AuthError::JsonDecode { stage, .. },
1398            ) => Some(*stage),
1399            Self::Auth(
1400                AuthError::Random(_)
1401                | AuthError::KeyTooShort { .. }
1402                | AuthError::InvalidServerProof
1403                | AuthError::DaemonIdMismatch
1404                | AuthError::InvalidClientAuth,
1405            )
1406            | Self::UnauthenticatedCapacity
1407            | Self::FrameIo(_)
1408            | Self::Router(_)
1409            | Self::WriterTask(_) => None,
1410        }
1411    }
1412}
1413
1414impl fmt::Display for ConnectionError {
1415    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1416        match self {
1417            Self::Auth(err) => write!(f, "connection auth failed: {err}"),
1418            Self::UnauthenticatedCapacity => write!(
1419                f,
1420                "too many concurrent unauthenticated subc TCP connections"
1421            ),
1422            Self::FrameIo(err) => write!(f, "frame connection error: {err}"),
1423            Self::Router(err) => write!(f, "router connection error: {err}"),
1424            Self::WriterTask(err) => write!(f, "connection writer task failed: {err}"),
1425        }
1426    }
1427}
1428
1429impl Error for ConnectionError {
1430    fn source(&self) -> Option<&(dyn Error + 'static)> {
1431        match self {
1432            Self::Auth(err) => Some(err),
1433            Self::FrameIo(err) => Some(err),
1434            Self::Router(err) => Some(err),
1435            Self::WriterTask(err) => Some(err),
1436            Self::UnauthenticatedCapacity => None,
1437        }
1438    }
1439}
1440
1441#[cfg(test)]
1442mod tests {
1443    use std::{
1444        future::Future,
1445        pin::Pin,
1446        sync::atomic::{AtomicUsize, Ordering},
1447        task::{Context, Poll},
1448    };
1449
1450    use super::*;
1451    use serde::{de::DeserializeOwned, Serialize};
1452    use subc_protocol::{
1453        DecodeError, ErrorBody, Flags, FrameType, Priority, HEADER_LEN, PROTOCOL_VERSION,
1454    };
1455    use tokio::io::{duplex, AsyncReadExt, AsyncWriteExt, ReadBuf};
1456
1457    use subc_transport::{
1458        authenticate_client, compute_proof, ClientAuth, ClientHello, ConnectionInfo, Endpoint,
1459        ServerProof, CLIENT_AUTH_DOMAIN, NONCE_LEN, SCHEMA_VERSION,
1460    };
1461
1462    use crate::{ControlHandler, EchoBackend, Frame, ReadStage, Registry};
1463
1464    const TEST_DEADLINE: Duration = Duration::from_secs(2);
1465    const TEST_DAEMON_VER: &str = "test-subc-server";
1466
1467    /// Modules size their replies against the published limit, and the router
1468    /// refuses any module frame over the egress budget, so the two must be the
1469    /// same number. This fails if the budget is ever given its own value.
1470    #[test]
1471    fn egress_budget_is_the_published_delivered_frame_limit() {
1472        assert_eq!(
1473            CONNECTION_EGRESS_BYTE_BUDGET,
1474            subc_protocol::MAX_DELIVERED_FRAME_LEN
1475        );
1476    }
1477
1478    struct ObservedStream {
1479        inner: tokio::io::DuplexStream,
1480        write_blocked: Arc<tokio::sync::Notify>,
1481        dropped: Arc<AtomicUsize>,
1482        read_error: Option<tokio::sync::oneshot::Receiver<()>>,
1483    }
1484
1485    impl AsyncRead for ObservedStream {
1486        fn poll_read(
1487            mut self: Pin<&mut Self>,
1488            cx: &mut Context<'_>,
1489            buf: &mut ReadBuf<'_>,
1490        ) -> Poll<io::Result<()>> {
1491            if let Some(receiver) = self.read_error.as_mut() {
1492                match Pin::new(receiver).poll(cx) {
1493                    Poll::Ready(Ok(())) => {
1494                        self.read_error = None;
1495                        return Poll::Ready(Err(io::ErrorKind::ConnectionReset.into()));
1496                    }
1497                    Poll::Ready(Err(_)) => self.read_error = None,
1498                    Poll::Pending => {}
1499                }
1500            }
1501            Pin::new(&mut self.inner).poll_read(cx, buf)
1502        }
1503    }
1504
1505    impl AsyncWrite for ObservedStream {
1506        fn poll_write(
1507            mut self: Pin<&mut Self>,
1508            cx: &mut Context<'_>,
1509            buf: &[u8],
1510        ) -> Poll<io::Result<usize>> {
1511            let result = Pin::new(&mut self.inner).poll_write(cx, buf);
1512            if result.is_pending() {
1513                self.write_blocked.notify_one();
1514            }
1515            result
1516        }
1517
1518        fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1519            Pin::new(&mut self.inner).poll_flush(cx)
1520        }
1521
1522        fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1523            Pin::new(&mut self.inner).poll_shutdown(cx)
1524        }
1525    }
1526
1527    impl Drop for ObservedStream {
1528        fn drop(&mut self) {
1529            self.dropped.fetch_add(1, Ordering::SeqCst);
1530        }
1531    }
1532
1533    struct BlockedWriterConnection {
1534        client: tokio::io::DuplexStream,
1535        server: JoinHandle<Result<(), ConnectionError>>,
1536        stream_dropped: Arc<AtomicUsize>,
1537        read_error: Option<tokio::sync::oneshot::Sender<()>>,
1538        router: Arc<Router>,
1539    }
1540
1541    impl BlockedWriterConnection {
1542        async fn new() -> Self {
1543            let (mut client, inner) = duplex(256);
1544            let write_blocked = Arc::new(tokio::sync::Notify::new());
1545            let stream_dropped = Arc::new(AtomicUsize::new(0));
1546            let (read_error, receiver) = tokio::sync::oneshot::channel();
1547            let stream = ObservedStream {
1548                inner,
1549                write_blocked: Arc::clone(&write_blocked),
1550                dropped: Arc::clone(&stream_dropped),
1551                read_error: Some(receiver),
1552            };
1553            let (auth, conn) = test_auth();
1554            let router = echo_router();
1555            let server = tokio::spawn(handle_connection(stream, Arc::clone(&router), auth));
1556            authenticate(&mut client, &conn).await;
1557
1558            // Ignore any backpressure observed during authentication. The echo
1559            // reply is larger than the duplex capacity and the client never reads it.
1560            let _ = timeout(Duration::ZERO, write_blocked.notified()).await;
1561            write_frame(&mut client, &request(7, 1, &[b'x'; 1024]))
1562                .await
1563                .unwrap();
1564            timeout(TEST_DEADLINE, write_blocked.notified())
1565                .await
1566                .expect("writer should block on the unread echo reply");
1567            assert!(!server.is_finished());
1568            assert_eq!(stream_dropped.load(Ordering::SeqCst), 0);
1569            Self {
1570                client,
1571                server,
1572                stream_dropped,
1573                read_error: Some(read_error),
1574                router,
1575            }
1576        }
1577
1578        async fn finish(&mut self) -> Result<(), ConnectionError> {
1579            let started = tokio::time::Instant::now();
1580            let result = timeout(CLOSE_DRAIN_GRACE + Duration::from_secs(1), &mut self.server)
1581                .await
1582                .expect("connection teardown exceeded writer drain grace plus margin")
1583                .expect("connection task should not panic");
1584            assert!(started.elapsed() >= CLOSE_DRAIN_GRACE);
1585            assert_eq!(
1586                self.stream_dropped.load(Ordering::SeqCst),
1587                1,
1588                "writer must release its stream half before teardown completes"
1589            );
1590            result
1591        }
1592
1593        fn request_close(&self) {
1594            // Each fixture owns a fresh router, so its only connection has ID 1.
1595            assert!(self.router.forwarding().request_connection_close(
1596                crate::registry::ConnectionId::new(1),
1597                CloseReason::new("test_close", "test requested close"),
1598            ));
1599        }
1600    }
1601
1602    #[tokio::test(start_paused = true)]
1603    async fn peer_eof_bounds_blocked_writer_teardown() {
1604        let mut connection = BlockedWriterConnection::new().await;
1605        // Half-close only the client-to-server direction: the unread receive
1606        // buffer must stay alive so the server's writer cannot finish naturally.
1607        connection.client.shutdown().await.unwrap();
1608        connection.finish().await.unwrap();
1609    }
1610
1611    #[tokio::test(start_paused = true)]
1612    async fn read_error_bounds_blocked_writer_teardown() {
1613        let mut connection = BlockedWriterConnection::new().await;
1614        connection.read_error.take().unwrap().send(()).unwrap();
1615        let error = connection.finish().await.unwrap_err();
1616        assert!(matches!(
1617            error,
1618            ConnectionError::FrameIo(FrameIoError::Io(error))
1619                if error.kind() == io::ErrorKind::ConnectionReset
1620        ));
1621    }
1622
1623    #[tokio::test(start_paused = true)]
1624    async fn close_requested_bounds_blocked_writer_teardown() {
1625        let mut connection = BlockedWriterConnection::new().await;
1626        connection.request_close();
1627        connection.finish().await.unwrap();
1628    }
1629
1630    #[tokio::test(start_paused = true)]
1631    async fn close_requested_flushes_queued_reply_before_grace() {
1632        let mut connection = BlockedWriterConnection::new().await;
1633        connection.request_close();
1634        let started = tokio::time::Instant::now();
1635        let reply = timeout(CLOSE_DRAIN_GRACE, read_frame(&mut connection.client))
1636            .await
1637            .expect("queued reply should flush before the grace expires")
1638            .unwrap()
1639            .unwrap();
1640        assert_eq!(reply.body, vec![b'x'; 1024]);
1641        timeout(CLOSE_DRAIN_GRACE, &mut connection.server)
1642            .await
1643            .unwrap()
1644            .unwrap()
1645            .unwrap();
1646        assert!(started.elapsed() < CLOSE_DRAIN_GRACE);
1647        assert_eq!(connection.stream_dropped.load(Ordering::SeqCst), 1);
1648    }
1649
1650    /// Reply-path stamp, slow polarity: a channel-0 reply whose queue residency
1651    /// exceeds the threshold must produce the slow-control-reply-write WARN with
1652    /// queued_ms reflecting the residency. Uses a backdated stamp rather than a
1653    /// real stall so the test is fast and deterministic.
1654    #[tokio::test]
1655    async fn stale_queued_control_reply_logs_slow_reply_write() {
1656        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::WARN);
1657        let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(4);
1658        let reply = Frame::build_with_version(
1659            PROTOCOL_VERSION,
1660            FrameType::Error,
1661            Flags::new(false, Priority::Interactive, false),
1662            0,
1663            0,
1664            7,
1665            serde_json::to_vec(&ErrorBody {
1666                code: "test".into(),
1667                message: "reply".into(),
1668                detail: None,
1669            })
1670            .expect("body encodes"),
1671        )
1672        .expect("frame builds");
1673        tx.send(crate::router::OutboundFrame {
1674            frame: reply,
1675            enqueued_at: std::time::Instant::now() - Duration::from_millis(1500),
1676            flushed: None,
1677            charge: None,
1678        })
1679        .await
1680        .expect("queued");
1681        drop(tx);
1682        let (write_half, mut read_half) = duplex(64 * 1024);
1683        drain_writer(write_half, rx).await.expect("writer drains");
1684        let mut sink = Vec::new();
1685        read_half.read_to_end(&mut sink).await.expect("read");
1686        assert!(!sink.is_empty(), "frame reached the socket");
1687        let captured = crate::router::test_log::captured_logs(&logs);
1688        assert!(
1689            captured.contains("slow control reply write") && captured.contains("corr=7"),
1690            "expected slow reply WARN naming corr, got: {captured}"
1691        );
1692        let queued_ms: u64 = captured
1693            .split("queued_ms=")
1694            .nth(1)
1695            .and_then(|s| s.split_whitespace().next())
1696            .and_then(|s| s.parse().ok())
1697            .expect("queued_ms present");
1698        assert!(
1699            queued_ms >= 1500,
1700            "queued_ms reflects residency: {queued_ms}"
1701        );
1702    }
1703
1704    /// Fast polarity: a promptly-drained control reply and a stale DATA-PLANE
1705    /// frame must both stay silent — the WARN is channel-0-only by design, and
1706    /// a healthy queue must add zero log volume.
1707    #[tokio::test]
1708    async fn fresh_control_and_stale_data_frames_log_nothing() {
1709        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::WARN);
1710        let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(4);
1711        let control = Frame::build_with_version(
1712            PROTOCOL_VERSION,
1713            FrameType::Error,
1714            Flags::new(false, Priority::Interactive, false),
1715            0,
1716            0,
1717            8,
1718            serde_json::to_vec(&ErrorBody {
1719                code: "test".into(),
1720                message: "fresh".into(),
1721                detail: None,
1722            })
1723            .expect("body encodes"),
1724        )
1725        .expect("frame builds");
1726        tx.send(crate::router::OutboundFrame {
1727            frame: control,
1728            enqueued_at: std::time::Instant::now(),
1729            flushed: None,
1730            charge: None,
1731        })
1732        .await
1733        .expect("queued");
1734        let data = Frame::build_with_version(
1735            PROTOCOL_VERSION,
1736            FrameType::Error,
1737            Flags::new(false, Priority::Interactive, false),
1738            9,
1739            1,
1740            9,
1741            serde_json::to_vec(&ErrorBody {
1742                code: "test".into(),
1743                message: "data".into(),
1744                detail: None,
1745            })
1746            .expect("body encodes"),
1747        )
1748        .expect("frame builds");
1749        tx.send(crate::router::OutboundFrame {
1750            frame: data,
1751            enqueued_at: std::time::Instant::now() - Duration::from_millis(5000),
1752            flushed: None,
1753            charge: None,
1754        })
1755        .await
1756        .expect("queued");
1757        drop(tx);
1758        let (write_half, _read_half) = duplex(64 * 1024);
1759        drain_writer(write_half, rx).await.expect("writer drains");
1760        let captured = crate::router::test_log::captured_logs(&logs);
1761        assert!(
1762            !captured.contains("slow control reply write"),
1763            "no WARN for fresh control or stale data frames, got: {captured}"
1764        );
1765    }
1766
1767    struct CountingReader {
1768        bytes: Vec<u8>,
1769        offset: usize,
1770        first_read_end: Option<usize>,
1771        reads: Arc<AtomicUsize>,
1772    }
1773
1774    impl CountingReader {
1775        fn new(bytes: Vec<u8>, first_read_end: Option<usize>) -> (Self, Arc<AtomicUsize>) {
1776            let reads = Arc::new(AtomicUsize::new(0));
1777            (
1778                Self {
1779                    bytes,
1780                    offset: 0,
1781                    first_read_end,
1782                    reads: Arc::clone(&reads),
1783                },
1784                reads,
1785            )
1786        }
1787    }
1788
1789    impl AsyncRead for CountingReader {
1790        fn poll_read(
1791            mut self: Pin<&mut Self>,
1792            _cx: &mut Context<'_>,
1793            buf: &mut ReadBuf<'_>,
1794        ) -> Poll<io::Result<()>> {
1795            self.reads.fetch_add(1, Ordering::Relaxed);
1796            let available = self.bytes.len().saturating_sub(self.offset);
1797            let first_read_remaining = self
1798                .first_read_end
1799                .filter(|end| self.offset < *end)
1800                .map_or(available, |end| end - self.offset);
1801            let count = available.min(first_read_remaining).min(buf.remaining());
1802            let end = self.offset + count;
1803            buf.put_slice(&self.bytes[self.offset..end]);
1804            self.offset = end;
1805            Poll::Ready(Ok(()))
1806        }
1807    }
1808
1809    fn encode_frames(frames: &[Frame]) -> Vec<u8> {
1810        let mut bytes = Vec::new();
1811        for frame in frames {
1812            bytes.extend_from_slice(&frame.header.encode());
1813            bytes.extend_from_slice(&frame.body);
1814        }
1815        bytes
1816    }
1817
1818    async fn read_frames<R>(reader: &mut R, count: usize) -> Vec<Frame>
1819    where
1820        R: AsyncRead + Unpin,
1821    {
1822        let mut frames = Vec::with_capacity(count);
1823        for _ in 0..count {
1824            frames.push(read_frame(reader).await.unwrap().unwrap());
1825        }
1826        frames
1827    }
1828
1829    fn request(channel: u16, corr: u64, body: &[u8]) -> Frame {
1830        Frame::build(
1831            FrameType::Request,
1832            Flags::new(true, Priority::Interactive, false),
1833            channel,
1834            0,
1835            corr,
1836            body.to_vec(),
1837        )
1838        .unwrap()
1839    }
1840
1841    fn echo_router() -> Arc<Router> {
1842        let mut router = Router::with_default_self_handler();
1843        router.register_backend(7, EchoBackend).unwrap();
1844        router.register_backend(9, EchoBackend).unwrap();
1845        Arc::new(router)
1846    }
1847
1848    fn test_auth() -> (ServerAuth, ConnectionInfo) {
1849        test_auth_with_limit(4)
1850    }
1851
1852    fn test_auth_with_limit(max_unauthenticated: usize) -> (ServerAuth, ConnectionInfo) {
1853        test_auth_with_deadline_and_limit(TEST_DEADLINE, max_unauthenticated)
1854    }
1855
1856    fn test_auth_with_deadline_and_limit(
1857        deadline: Duration,
1858        max_unauthenticated: usize,
1859    ) -> (ServerAuth, ConnectionInfo) {
1860        let key = vec![0x42; 32];
1861        let daemon_id = [0x24; 16];
1862        let conn = ConnectionInfo {
1863            schema: SCHEMA_VERSION,
1864            wire_version: None,
1865            endpoints: vec![Endpoint {
1866                host: "127.0.0.1".to_owned(),
1867                port: 1,
1868            }],
1869            key: key.clone(),
1870            daemon_id,
1871            pid: std::process::id(),
1872            daemon_ver: TEST_DAEMON_VER.to_owned(),
1873        };
1874        (
1875            ServerAuth::with_limits(
1876                key,
1877                daemon_id,
1878                TEST_DAEMON_VER,
1879                deadline,
1880                max_unauthenticated,
1881            ),
1882            conn,
1883        )
1884    }
1885
1886    async fn authenticate<S>(stream: &mut S, conn: &ConnectionInfo)
1887    where
1888        S: AsyncRead + AsyncWrite + Unpin,
1889    {
1890        authenticate_client(stream, conn, TEST_DEADLINE)
1891            .await
1892            .expect("test client should authenticate")
1893    }
1894
1895    async fn write_auth_message<S, T>(stream: &mut S, value: &T)
1896    where
1897        S: AsyncWrite + Unpin,
1898        T: Serialize,
1899    {
1900        let body = serde_json::to_vec(value).expect("auth message encodes");
1901        stream
1902            .write_all(&(body.len() as u32).to_le_bytes())
1903            .await
1904            .expect("auth message length writes");
1905        stream
1906            .write_all(&body)
1907            .await
1908            .expect("auth message body writes");
1909    }
1910
1911    async fn read_auth_message<S, T>(stream: &mut S) -> T
1912    where
1913        S: AsyncRead + Unpin,
1914        T: DeserializeOwned,
1915    {
1916        let mut length = [0; 4];
1917        stream
1918            .read_exact(&mut length)
1919            .await
1920            .expect("auth message length reads");
1921        let mut body = vec![0; u32::from_le_bytes(length) as usize];
1922        stream
1923            .read_exact(&mut body)
1924            .await
1925            .expect("auth message body reads");
1926        serde_json::from_slice(&body).expect("auth message decodes")
1927    }
1928
1929    async fn send_wrong_key_client_auth<S>(stream: &mut S, conn: &ConnectionInfo)
1930    where
1931        S: AsyncRead + AsyncWrite + Unpin,
1932    {
1933        let hello = ClientHello {
1934            client_nonce: [0x57; NONCE_LEN],
1935            role: "client".to_owned(),
1936        };
1937        write_auth_message(stream, &hello).await;
1938        let proof: ServerProof = read_auth_message(stream).await;
1939        let mut wrong_key = conn.key.clone();
1940        wrong_key[0] ^= 0xff;
1941        let client_auth = ClientAuth {
1942            client_auth: compute_proof(
1943                &wrong_key,
1944                CLIENT_AUTH_DOMAIN,
1945                &hello.client_nonce,
1946                &proof.server_nonce,
1947                &conn.daemon_id,
1948            ),
1949        };
1950        write_auth_message(stream, &client_auth).await;
1951    }
1952
1953    fn assert_pre_auth_log(logs: &Arc<std::sync::Mutex<Vec<u8>>>, stage: &str, port: u16) {
1954        let captured = crate::router::test_log::captured_logs(logs);
1955        assert!(
1956            captured.contains(&format!("stage=\"{stage}\"")),
1957            "expected rejection stage {stage}, got: {captured}"
1958        );
1959        assert!(
1960            captured.contains(&format!("peer_addr=127.0.0.1:{port}")),
1961            "expected peer address, got: {captured}"
1962        );
1963        assert!(
1964            captured.contains("elapsed_ms=") && captured.contains("suppressed_since_last="),
1965            "expected elapsed time and suppression count, got: {captured}"
1966        );
1967        assert!(
1968            !captured.contains("WARN"),
1969            "pre-auth rejection must not be logged as a warning: {captured}"
1970        );
1971    }
1972
1973    #[tokio::test]
1974    async fn logs_handshake_deadline_pre_auth_rejection() {
1975        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1976        let deadline = Duration::from_millis(60);
1977        let (auth, _) = test_auth_with_deadline_and_limit(deadline, 4);
1978        let (client, server_stream) = duplex(512);
1979        let port = 41001;
1980        let server = handle_accepted_connection(
1981            server_stream,
1982            echo_router(),
1983            auth,
1984            SocketAddr::from(([127, 0, 0, 1], port)),
1985            Instant::now(),
1986        );
1987
1988        let error = tokio::time::timeout(deadline * 3, server)
1989            .await
1990            .expect("idle handshake should end at its deadline")
1991            .expect_err("idle handshake must be rejected");
1992        drop(client);
1993        assert!(matches!(
1994            error,
1995            ConnectionError::Auth(AuthError::Timeout {
1996                stage: AuthStage::ClientHello,
1997                ..
1998            })
1999        ));
2000        let captured = crate::router::test_log::captured_logs(&logs);
2001        assert!(
2002            captured.contains("reason=\"handshake_deadline_expired\""),
2003            "{captured}"
2004        );
2005        assert_pre_auth_log(&logs, "handshake_deadline", port);
2006    }
2007
2008    #[tokio::test]
2009    async fn logs_invalid_client_proof_pre_auth_rejection() {
2010        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2011        let (auth, conn) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
2012        let (mut client, server_stream) = duplex(512);
2013        let port = 41002;
2014        let server = handle_accepted_connection(
2015            server_stream,
2016            echo_router(),
2017            auth,
2018            SocketAddr::from(([127, 0, 0, 1], port)),
2019            Instant::now(),
2020        );
2021
2022        let (error, ()) = tokio::join!(server, send_wrong_key_client_auth(&mut client, &conn));
2023        assert!(matches!(
2024            error,
2025            Err(ConnectionError::Auth(AuthError::InvalidClientAuth))
2026        ));
2027        let captured = crate::router::test_log::captured_logs(&logs);
2028        assert!(
2029            captured.contains("reason=\"client_proof_mismatch\""),
2030            "{captured}"
2031        );
2032        assert_pre_auth_log(&logs, "client_authentication", port);
2033    }
2034
2035    #[tokio::test]
2036    async fn logs_malformed_pre_auth_message() {
2037        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2038        let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
2039        let (mut client, server_stream) = duplex(512);
2040        let port = 41003;
2041        let server = handle_accepted_connection(
2042            server_stream,
2043            echo_router(),
2044            auth,
2045            SocketAddr::from(([127, 0, 0, 1], port)),
2046            Instant::now(),
2047        );
2048        let send_malformed_message = async {
2049            let body = b"{";
2050            client
2051                .write_all(&(body.len() as u32).to_le_bytes())
2052                .await
2053                .expect("message length writes");
2054            client.write_all(body).await.expect("message body writes");
2055        };
2056
2057        let (error, ()) = tokio::join!(server, send_malformed_message);
2058        assert!(matches!(
2059            error,
2060            Err(ConnectionError::Auth(AuthError::JsonDecode {
2061                stage: AuthStage::ClientHello,
2062                ..
2063            }))
2064        ));
2065        let captured = crate::router::test_log::captured_logs(&logs);
2066        assert!(
2067            captured.contains("reason=\"malformed_or_oversized_message\""),
2068            "{captured}"
2069        );
2070        assert_pre_auth_log(&logs, "malformed_message", port);
2071    }
2072
2073    #[tokio::test]
2074    async fn logs_peer_close_during_handshake() {
2075        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2076        let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
2077        let (client, server_stream) = duplex(512);
2078        let port = 41004;
2079        drop(client);
2080
2081        let error = handle_accepted_connection(
2082            server_stream,
2083            echo_router(),
2084            auth,
2085            SocketAddr::from(([127, 0, 0, 1], port)),
2086            Instant::now(),
2087        )
2088        .await
2089        .expect_err("closed peer must be rejected");
2090        assert!(matches!(
2091            error,
2092            ConnectionError::Auth(AuthError::UnexpectedEof {
2093                stage: AuthStage::ClientHello,
2094                actual: 0,
2095                ..
2096            })
2097        ));
2098        let captured = crate::router::test_log::captured_logs(&logs);
2099        assert!(
2100            captured.contains("reason=\"peer_closed_before_authentication_completed\""),
2101            "{captured}"
2102        );
2103        assert_pre_auth_log(&logs, "peer_closed", port);
2104    }
2105
2106    // Rejection-log rate limits belong to one listener. A process-wide limiter
2107    // let one listener's rejects silence another's for a second, which hid the
2108    // second listener's first rejection entirely.
2109    #[tokio::test]
2110    async fn each_listener_rate_limits_its_own_rejection_logs() {
2111        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2112        for port in [41011, 41012] {
2113            let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
2114            let (client, server_stream) = duplex(512);
2115            drop(client);
2116            handle_accepted_connection(
2117                server_stream,
2118                echo_router(),
2119                auth,
2120                SocketAddr::from(([127, 0, 0, 1], port)),
2121                Instant::now(),
2122            )
2123            .await
2124            .expect_err("closed peer must be rejected");
2125        }
2126        let captured = crate::router::test_log::captured_logs(&logs);
2127        for port in [41011, 41012] {
2128            assert!(
2129                captured.contains(&format!("127.0.0.1:{port}")),
2130                "listener on port {port} logged nothing: {captured}"
2131            );
2132        }
2133    }
2134
2135    #[tokio::test]
2136    async fn logs_unauthenticated_capacity_rejection() {
2137        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2138        let deadline = Duration::from_millis(60);
2139        let (auth, _) = test_auth_with_deadline_and_limit(deadline, 1);
2140        let held_slot = auth
2141            .unauthenticated
2142            .clone()
2143            .try_acquire_owned()
2144            .expect("the only pre-auth slot is available");
2145        let (client, server_stream) = duplex(512);
2146        let port = 41005;
2147        let server = handle_accepted_connection(
2148            server_stream,
2149            echo_router(),
2150            auth,
2151            SocketAddr::from(([127, 0, 0, 1], port)),
2152            Instant::now(),
2153        );
2154
2155        let error = tokio::time::timeout(deadline * 3, server)
2156            .await
2157            .expect("queued connection should end at the capacity deadline")
2158            .expect_err("connection without a free slot must be rejected");
2159        drop(held_slot);
2160        drop(client);
2161        assert!(matches!(error, ConnectionError::UnauthenticatedCapacity));
2162        let captured = crate::router::test_log::captured_logs(&logs);
2163        assert!(
2164            captured.contains("reason=\"capacity_wait_timed_out\""),
2165            "{captured}"
2166        );
2167        assert_pre_auth_log(&logs, "waiting_for_handshake_slot", port);
2168    }
2169
2170    #[test]
2171    fn pre_auth_rejection_limiter_suppresses_bursts_and_reports_count() {
2172        let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
2173        let limiter = PreAuthRejectLimiter::new();
2174        let capacity = ConnectionError::UnauthenticatedCapacity;
2175        let handshake_timeout = ConnectionError::Auth(AuthError::Timeout {
2176            stage: AuthStage::ClientHello,
2177            deadline: Duration::from_millis(200),
2178        });
2179        let peer_addr = SocketAddr::from(([127, 0, 0, 1], 41006));
2180
2181        log_pre_auth_rejection(
2182            &capacity,
2183            peer_addr,
2184            Duration::from_millis(1),
2185            Duration::ZERO,
2186            &limiter,
2187        );
2188        for _ in 0..4 {
2189            log_pre_auth_rejection(
2190                &capacity,
2191                peer_addr,
2192                Duration::from_millis(2),
2193                Duration::from_millis(100),
2194                &limiter,
2195            );
2196        }
2197        log_pre_auth_rejection(
2198            &handshake_timeout,
2199            peer_addr,
2200            Duration::from_millis(3),
2201            Duration::from_millis(100),
2202            &limiter,
2203        );
2204        log_pre_auth_rejection(
2205            &capacity,
2206            peer_addr,
2207            Duration::from_millis(4),
2208            Duration::from_secs(1),
2209            &limiter,
2210        );
2211
2212        let captured = crate::router::test_log::captured_logs(&logs);
2213        assert_eq!(
2214            captured
2215                .matches("subc TCP connection rejected before routing")
2216                .count(),
2217            3,
2218            "one line per stage in a burst, then a later capacity line: {captured}"
2219        );
2220        assert!(
2221            captured.contains("suppressed_since_last=4"),
2222            "later capacity line should report the four hidden rejects: {captured}"
2223        );
2224        assert!(
2225            captured.contains("stage=\"handshake_deadline\""),
2226            "{captured}"
2227        );
2228    }
2229
2230    #[tokio::test]
2231    async fn buffered_frame_reader_coalesces_reads_and_preserves_short_reads() {
2232        let frames = vec![
2233            request(7, 1, b"first"),
2234            request(9, 2, b"second"),
2235            request(7, 3, b"third"),
2236            request(9, 4, b"fourth"),
2237        ];
2238        let bytes = encode_frames(&frames);
2239
2240        let (mut direct, direct_reads) = CountingReader::new(bytes.clone(), None);
2241        assert_eq!(read_frames(&mut direct, frames.len()).await, frames);
2242        assert_eq!(direct_reads.load(Ordering::Relaxed), frames.len() * 3);
2243
2244        let (buffered_source, buffered_reads) = CountingReader::new(bytes, None);
2245        let mut buffered = BufReader::new(buffered_source);
2246        assert_eq!(read_frames(&mut buffered, frames.len()).await, frames);
2247        assert_eq!(buffered_reads.load(Ordering::Relaxed), 1);
2248
2249        let split_frame = request(7, 5, b"split-body");
2250        let split_bytes = encode_frames(std::slice::from_ref(&split_frame));
2251        let (split_source, split_reads) = CountingReader::new(split_bytes, Some(10));
2252        let mut split_reader = BufReader::new(split_source);
2253        assert_eq!(
2254            read_frame(&mut split_reader).await.unwrap(),
2255            Some(split_frame)
2256        );
2257        assert_eq!(split_reads.load(Ordering::Relaxed), 2);
2258    }
2259
2260    #[tokio::test]
2261    async fn interleaved_channels_on_one_stream_demux_byte_identically_after_auth() {
2262        let (mut client, server_stream) = duplex(4096);
2263        let (auth, conn) = test_auth();
2264        let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2265        authenticate(&mut client, &conn).await;
2266        let frames = [
2267            request(7, 1, b"chan7-first\0opaque"),
2268            request(9, 2, b"chan9-middle-{json?}"),
2269            request(7, 3, b"chan7-second\xffbytes"),
2270        ];
2271
2272        for frame in &frames {
2273            crate::write_frame(&mut client, frame).await.unwrap();
2274        }
2275
2276        for expected in &frames {
2277            let response = crate::read_frame(&mut client).await.unwrap().unwrap();
2278            assert_eq!(response.header.ty, FrameType::Response);
2279            assert_eq!(response.header.channel, expected.header.channel);
2280            assert_eq!(response.header.corr, expected.header.corr);
2281            assert_eq!(response.body, expected.body);
2282        }
2283
2284        drop(client);
2285        server.await.unwrap().unwrap();
2286    }
2287
2288    #[tokio::test]
2289    async fn channel_zero_goes_to_subc_self_handler_after_auth() {
2290        let (mut client, server_stream) = duplex(512);
2291        let (auth, conn) = test_auth();
2292        let server = tokio::spawn(handle_connection(
2293            server_stream,
2294            Arc::new(Router::with_default_self_handler()),
2295            auth,
2296        ));
2297        authenticate(&mut client, &conn).await;
2298        let ping = Frame::build(
2299            FrameType::Ping,
2300            Flags::new(false, Priority::Passive, false),
2301            0,
2302            0,
2303            55,
2304            Vec::new(),
2305        )
2306        .unwrap();
2307
2308        crate::write_frame(&mut client, &ping).await.unwrap();
2309        let response = crate::read_frame(&mut client).await.unwrap().unwrap();
2310
2311        assert_eq!(response.header.ty, FrameType::Pong);
2312        assert_eq!(response.header.channel, 0);
2313        assert_eq!(response.header.corr, 55);
2314        assert!(response.body.is_empty());
2315
2316        drop(client);
2317        server.await.unwrap().unwrap();
2318    }
2319
2320    #[tokio::test]
2321    async fn unauthenticated_connection_is_rejected_before_routing() {
2322        let (mut client, server_stream) = duplex(512);
2323        let (auth, _conn) = test_auth();
2324        let registry = Arc::new(Registry::default());
2325        let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2326            Arc::clone(&registry),
2327        ))));
2328        let server = tokio::spawn(handle_connection(server_stream, router, auth));
2329        let ping = Frame::build(
2330            FrameType::Ping,
2331            Flags::new(false, Priority::Passive, false),
2332            0,
2333            0,
2334            66,
2335            Vec::new(),
2336        )
2337        .unwrap();
2338
2339        crate::write_frame(&mut client, &ping).await.unwrap();
2340        if let Ok(Ok(Some(frame))) =
2341            tokio::time::timeout(Duration::from_millis(200), crate::read_frame(&mut client)).await
2342        {
2343            panic!("unauthenticated frame reached router: {frame:?}");
2344        }
2345
2346        let err = server.await.unwrap().unwrap_err();
2347        assert!(matches!(err, ConnectionError::Auth(_)));
2348        assert_eq!(registry.active_registration_count().unwrap(), 0);
2349    }
2350
2351    #[tokio::test]
2352    async fn over_cap_peer_queues_for_a_slot_and_authenticates_when_one_frees() {
2353        // Restart-herd contract: an over-cap pre-auth connection WAITS (bounded by
2354        // the auth deadline) instead of being reset. When the slot-holder finishes,
2355        // the queued peer must complete a normal handshake — the 2026-07-14 aft
2356        // outage was exactly a supervised child being reset out of this lottery.
2357        let (mut first_client, first_server_stream) = duplex(2048);
2358        let (mut second_client, second_server_stream) = duplex(2048);
2359        let (auth, conn) = test_auth_with_limit(1);
2360        let registry = Arc::new(Registry::default());
2361        let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2362            Arc::clone(&registry),
2363        ))));
2364
2365        let first_server = tokio::spawn(handle_connection(
2366            first_server_stream,
2367            Arc::clone(&router),
2368            auth.clone(),
2369        ));
2370
2371        let second_server = tokio::spawn(handle_connection(
2372            second_server_stream,
2373            Arc::clone(&router),
2374            auth.clone(),
2375        ));
2376
2377        // The second peer must still be pending (not reset) while the slot is held.
2378        tokio::time::sleep(Duration::from_millis(50)).await;
2379        assert!(
2380            !second_server.is_finished(),
2381            "queued peer must not be reset"
2382        );
2383
2384        // First peer completes its handshake, freeing the slot; the queued second
2385        // peer then authenticates normally.
2386        authenticate(&mut first_client, &conn).await;
2387        authenticate(&mut second_client, &conn).await;
2388
2389        drop(first_client);
2390        drop(second_client);
2391        let _ = first_server.await;
2392        let _ = second_server.await;
2393        assert_eq!(registry.active_registration_count().unwrap(), 0);
2394    }
2395
2396    #[tokio::test]
2397    async fn over_cap_peer_is_rejected_when_no_slot_frees_within_deadline() {
2398        // The deadline stays the DoS bound: if no slot frees, the queued peer is
2399        // rejected at the deadline with a closed stream. The slot is held DIRECTLY
2400        // (not by another connection) so nothing can free it mid-test — a squatting
2401        // connection's own auth deadline would release the slot at exactly the
2402        // waiter's timeout, making the outcome racy.
2403        let (mut second_client, second_server_stream) = duplex(512);
2404        let (auth, conn) = test_auth_with_limit(1);
2405        let registry = Arc::new(Registry::default());
2406        let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2407            Arc::clone(&registry),
2408        ))));
2409
2410        let held_slot = auth
2411            .unauthenticated
2412            .clone()
2413            .try_acquire_owned()
2414            .expect("sole pre-auth slot");
2415
2416        let second_server = tokio::spawn(handle_connection(
2417            second_server_stream,
2418            Arc::clone(&router),
2419            auth.clone(),
2420        ));
2421        let second_err = tokio::time::timeout(TEST_DEADLINE * 2, second_server)
2422            .await
2423            .expect("capacity reject should settle at the deadline")
2424            .expect("second connection task should not panic")
2425            .expect_err("queued peer must be rejected when no slot frees");
2426        drop(held_slot);
2427        assert!(matches!(
2428            second_err,
2429            ConnectionError::UnauthenticatedCapacity
2430        ));
2431        let mut closed = [0u8; 1];
2432        assert_eq!(
2433            second_client.read(&mut closed).await.unwrap(),
2434            0,
2435            "capacity-rejected peer should observe a closed stream"
2436        );
2437        assert_eq!(registry.active_registration_count().unwrap(), 0);
2438
2439        let (mut authed_client, authed_server_stream) = duplex(2048);
2440        let authed_server = tokio::spawn(handle_connection(
2441            authed_server_stream,
2442            Arc::clone(&router),
2443            auth,
2444        ));
2445        authenticate(&mut authed_client, &conn).await;
2446        let ping = Frame::build(
2447            FrameType::Ping,
2448            Flags::new(false, Priority::Passive, false),
2449            0,
2450            0,
2451            77,
2452            Vec::new(),
2453        )
2454        .unwrap();
2455        crate::write_frame(&mut authed_client, &ping).await.unwrap();
2456        let pong = crate::read_frame(&mut authed_client)
2457            .await
2458            .unwrap()
2459            .unwrap();
2460        assert_eq!(pong.header.ty, FrameType::Pong);
2461        assert_eq!(pong.header.channel, 0);
2462        assert_eq!(pong.header.corr, 77);
2463        assert_eq!(registry.active_registration_count().unwrap(), 0);
2464
2465        drop(authed_client);
2466        authed_server.await.unwrap().unwrap();
2467    }
2468
2469    #[tokio::test]
2470    async fn bind_ack_during_partial_frame_preserves_next_request() {
2471        use subc_control::ClientControlRequest;
2472        use subc_protocol::{
2473            manifest::{
2474                Concurrency, ExecutionMode, IdentityScope, ModuleManifest, ProviderRole, Tool,
2475            },
2476            session::{ModuleControlRequest, ModuleControlResponse},
2477            BindIdentity, ModuleHelloBody, RouteTarget,
2478        };
2479
2480        let mut configured_router = Router::with_default_self_handler();
2481        configured_router.register_backend(7, EchoBackend).unwrap();
2482        let router = Arc::new(configured_router);
2483        let (auth, conn) = test_auth();
2484        let (mut module, module_stream) = duplex(4096);
2485        let module_server = tokio::spawn(handle_connection(
2486            module_stream,
2487            Arc::clone(&router),
2488            auth.clone(),
2489        ));
2490        authenticate(&mut module, &conn).await;
2491        let manifest = ModuleManifest::builder("frame-test", "0.1.0")
2492            .protocol_ver(PROTOCOL_VERSION)
2493            .provides(vec![ProviderRole::ToolProvider {
2494                tools: vec![Tool {
2495                    name: "read".into(),
2496                    description: None,
2497                    execution_mode: ExecutionMode::Pure,
2498                    schema: serde_json::json!({"type": "object"}),
2499                }],
2500                identity_scope: vec![IdentityScope::Project, IdentityScope::Session],
2501                concurrency: Concurrency::ModuleManaged,
2502                emits_push: true,
2503                sub_supervises: true,
2504            }])
2505            .build();
2506        let hello = Frame::build(
2507            FrameType::Hello,
2508            Flags::new(false, Priority::Passive, false),
2509            0,
2510            0,
2511            1,
2512            serde_json::to_vec(&ModuleHelloBody {
2513                manifest,
2514                protocol_ver: PROTOCOL_VERSION,
2515                control_ops: None,
2516                launch_nonce: None,
2517            })
2518            .unwrap(),
2519        )
2520        .unwrap();
2521        crate::write_frame(&mut module, &hello).await.unwrap();
2522        assert_eq!(
2523            read_frame(&mut module).await.unwrap().unwrap().header.ty,
2524            FrameType::HelloAck
2525        );
2526
2527        let (mut client, client_stream) = duplex(4096);
2528        let client_server = tokio::spawn(handle_connection(client_stream, router, auth));
2529        authenticate(&mut client, &conn).await;
2530        let open = Frame::build(
2531            FrameType::Request,
2532            Flags::new(false, Priority::Passive, false),
2533            0,
2534            0,
2535            2,
2536            serde_json::to_vec(&ClientControlRequest::RouteOpen {
2537                target: RouteTarget::ToolProvider {
2538                    module_id: "frame-test".into(),
2539                },
2540                identity: BindIdentity::new(std::env::current_dir().unwrap(), "unit", "session"),
2541                consumer_identity: None,
2542                consumer_capabilities: None,
2543                role_versions: None,
2544                admission_facts: None,
2545                scope: None,
2546            })
2547            .unwrap(),
2548        )
2549        .unwrap();
2550        crate::write_frame(&mut client, &open).await.unwrap();
2551        let bind = timeout(TEST_DEADLINE, read_frame(&mut module))
2552            .await
2553            .unwrap()
2554            .unwrap()
2555            .unwrap();
2556        assert!(matches!(
2557            serde_json::from_slice::<ModuleControlRequest>(&bind.body).unwrap(),
2558            ModuleControlRequest::RouteBind { .. }
2559        ));
2560
2561        let ping = request(7, 3, b"partial-frame-body");
2562        client.write_all(&ping.header.encode()).await.unwrap();
2563        // Give the connection reader time to consume the header while the bind is pending.
2564        tokio::time::sleep(Duration::from_millis(30)).await;
2565        let ack = Frame::build(
2566            FrameType::Response,
2567            Flags::new(false, Priority::Passive, false),
2568            0,
2569            0,
2570            bind.header.corr,
2571            serde_json::to_vec(&ModuleControlResponse::RouteBindAck {}).unwrap(),
2572        )
2573        .unwrap();
2574        crate::write_frame(&mut module, &ack).await.unwrap();
2575        let opened = timeout(TEST_DEADLINE, read_frame(&mut client))
2576            .await
2577            .unwrap()
2578            .unwrap();
2579        if opened.is_none() {
2580            panic!("client closed: {:?}", client_server.await);
2581        }
2582        let opened = opened.unwrap();
2583        assert_eq!(opened.header.corr, 2);
2584        client.write_all(&ping.body).await.unwrap();
2585        let pong = timeout(TEST_DEADLINE, read_frame(&mut client))
2586            .await
2587            .expect("partial frame must reach the router after the bind ack")
2588            .expect("frame must decode")
2589            .expect("connection must remain open");
2590        assert_eq!(pong.header.ty, FrameType::Response);
2591        assert_eq!(pong.header.corr, 3);
2592        assert_eq!(pong.body, ping.body);
2593        drop(client);
2594        drop(module);
2595        client_server.await.unwrap().unwrap();
2596        let _ = module_server.await.unwrap();
2597    }
2598
2599    #[tokio::test]
2600    async fn full_module_egress_health_probe_does_not_block_same_client_other_route() {
2601        use crate::ConnectionId;
2602        use subc_control::{ClientControlRequest, ClientControlResponse};
2603        use subc_protocol::{
2604            manifest::{
2605                Concurrency, ExecutionMode, IdentityScope, ModuleManifest, ProviderRole, Tool,
2606            },
2607            session::ModuleControlResponse,
2608            BindIdentity, ModuleHelloBody, RouteTarget,
2609        };
2610
2611        let probe_timeout = Duration::from_millis(100);
2612        let handler = Arc::new(
2613            ControlHandler::new(Arc::new(Registry::default()))
2614                .with_health_probe_timeout(probe_timeout),
2615        );
2616        let router = Arc::new(Router::with_control_handler(Arc::clone(&handler)));
2617        let hello = |name: &str| {
2618            Frame::build(
2619                FrameType::Hello,
2620                Flags::new(false, Priority::Passive, false),
2621                0,
2622                0,
2623                1,
2624                serde_json::to_vec(&ModuleHelloBody {
2625                    manifest: ModuleManifest::builder(name, "0.1.0")
2626                        .protocol_ver(PROTOCOL_VERSION)
2627                        .provides(vec![ProviderRole::ToolProvider {
2628                            tools: vec![Tool {
2629                                name: "read".into(),
2630                                description: None,
2631                                execution_mode: ExecutionMode::Pure,
2632                                schema: serde_json::json!({"type": "object"}),
2633                            }],
2634                            identity_scope: vec![IdentityScope::Project, IdentityScope::Session],
2635                            concurrency: Concurrency::ModuleManaged,
2636                            emits_push: false,
2637                            sub_supervises: false,
2638                        }])
2639                        .build(),
2640                    protocol_ver: PROTOCOL_VERSION,
2641                    control_ops: Some(vec!["health.check".to_owned()]),
2642                    launch_nonce: None,
2643                })
2644                .unwrap(),
2645            )
2646            .unwrap()
2647        };
2648        let (blocked_tx, mut blocked_rx) = mpsc::channel(1);
2649        let blocked_ctx = RouteCtx {
2650            connection_id: ConnectionId::new(90001),
2651            egress: FrameSink::new(blocked_tx),
2652        };
2653        handler
2654            .handle_control_frame(&blocked_ctx, hello("blocked"))
2655            .await
2656            .unwrap();
2657        assert_eq!(
2658            blocked_rx.try_recv().unwrap().header.ty,
2659            FrameType::HelloAck
2660        );
2661        let filler = request(0, 99, b"queue filler");
2662        blocked_ctx.egress.try_send(filler.clone()).unwrap();
2663        assert!(
2664            blocked_ctx.egress.try_send(filler).is_err(),
2665            "module queue must actually be full"
2666        );
2667
2668        let (healthy_tx, mut healthy_rx) = mpsc::channel(8);
2669        let healthy_ctx = RouteCtx {
2670            connection_id: ConnectionId::new(90002),
2671            egress: FrameSink::new(healthy_tx),
2672        };
2673        handler
2674            .handle_control_frame(&healthy_ctx, hello("healthy"))
2675            .await
2676            .unwrap();
2677        assert_eq!(
2678            healthy_rx.try_recv().unwrap().header.ty,
2679            FrameType::HelloAck
2680        );
2681        let (auth, conn) = test_auth();
2682        let (mut client, client_stream) = duplex(4096);
2683        let client_server =
2684            tokio::spawn(handle_connection(client_stream, Arc::clone(&router), auth));
2685        authenticate(&mut client, &conn).await;
2686        let control_request = |corr, body: ClientControlRequest| {
2687            Frame::build(
2688                FrameType::Request,
2689                Flags::new(false, Priority::Interactive, false),
2690                0,
2691                0,
2692                corr,
2693                serde_json::to_vec(&body).unwrap(),
2694            )
2695            .unwrap()
2696        };
2697        crate::write_frame(
2698            &mut client,
2699            &control_request(
2700                2,
2701                ClientControlRequest::RouteOpen {
2702                    target: RouteTarget::ToolProvider {
2703                        module_id: "healthy".into(),
2704                    },
2705                    identity: BindIdentity::new(
2706                        std::env::current_dir().unwrap(),
2707                        "unit",
2708                        "session",
2709                    ),
2710                    consumer_identity: None,
2711                    consumer_capabilities: None,
2712                    role_versions: None,
2713                    admission_facts: None,
2714                    scope: None,
2715                },
2716            ),
2717        )
2718        .await
2719        .unwrap();
2720        let bind = timeout(TEST_DEADLINE, healthy_rx.recv())
2721            .await
2722            .unwrap()
2723            .unwrap();
2724        handler
2725            .handle_control_frame(
2726                &healthy_ctx,
2727                Frame::build(
2728                    FrameType::Response,
2729                    Flags::new(false, Priority::Passive, false),
2730                    0,
2731                    0,
2732                    bind.header.corr,
2733                    serde_json::to_vec(&ModuleControlResponse::RouteBindAck {}).unwrap(),
2734                )
2735                .unwrap(),
2736            )
2737            .await
2738            .unwrap();
2739        let opened = timeout(TEST_DEADLINE, read_frame(&mut client))
2740            .await
2741            .unwrap()
2742            .unwrap()
2743            .unwrap();
2744        let (channel, epoch) =
2745            match serde_json::from_slice::<ClientControlResponse>(&opened.body).unwrap() {
2746                ClientControlResponse::RouteOpen {
2747                    route_channel,
2748                    route_epoch,
2749                } => (route_channel, route_epoch),
2750                other => panic!("expected a healthy module route, got {other:?}"),
2751            };
2752
2753        let probe = control_request(
2754            3,
2755            ClientControlRequest::SupervisorHealthProbe {
2756                module_id: "blocked".into(),
2757            },
2758        );
2759        let mut ping = request(channel, 4, b"healthy request");
2760        ping.header.epoch = epoch;
2761        crate::write_frame(&mut client, &probe).await.unwrap();
2762        crate::write_frame(&mut client, &ping).await.unwrap();
2763        timeout(probe_timeout + Duration::from_secs(1), async {
2764            let timed_out = read_frame(&mut client).await.unwrap().unwrap();
2765            assert_eq!(timed_out.header.corr, 3);
2766            assert_eq!(timed_out.header.ty, FrameType::Error);
2767            let error: ErrorBody = serde_json::from_slice(&timed_out.body).unwrap();
2768            assert_eq!(error.code, "module_timeout");
2769            let forwarded = healthy_rx.recv().await.unwrap();
2770            assert_eq!(forwarded.header.corr, ping.header.corr);
2771            assert_eq!(forwarded.body, ping.body);
2772            let reply = Frame::build(
2773                FrameType::Response,
2774                ping.header.flags,
2775                forwarded.header.channel,
2776                forwarded.header.epoch,
2777                forwarded.header.corr,
2778                b"healthy response".to_vec(),
2779            )
2780            .unwrap();
2781            router
2782                .route_for_connection(&healthy_ctx, reply)
2783                .await
2784                .unwrap();
2785            let pong = read_frame(&mut client).await.unwrap().unwrap();
2786            assert_eq!(pong.header.ty, FrameType::Response);
2787            assert_eq!(pong.header.channel, channel);
2788            assert_eq!(pong.header.epoch, epoch);
2789            assert_eq!(pong.header.corr, 4);
2790            assert_eq!(pong.body, b"healthy response");
2791        })
2792        .await
2793        .expect(
2794            "the same client's healthy route must answer within the probe deadline plus margin",
2795        );
2796        assert_eq!(
2797            blocked_rx.len(),
2798            1,
2799            "the blocked module must remain undrained"
2800        );
2801        assert_eq!(blocked_rx.try_recv().unwrap().header.corr, 99);
2802        assert!(
2803            blocked_rx.try_recv().is_err(),
2804            "a timed-out probe must not be enqueued later"
2805        );
2806        drop(client);
2807        client_server.await.unwrap().unwrap();
2808    }
2809
2810    #[tokio::test]
2811    async fn aborted_accept_does_not_end_listener() {
2812        let listener = Arc::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
2813        let addr = listener.local_addr().unwrap();
2814        let (auth, conn) = test_auth();
2815        let mut attempts = 0;
2816        let server = tokio::spawn(serve_listener_with_accept(
2817            Some(addr),
2818            echo_router(),
2819            auth,
2820            move || {
2821                attempts += 1;
2822                let result = if attempts == 1 {
2823                    Some(io::Error::from(io::ErrorKind::ConnectionAborted))
2824                } else {
2825                    None
2826                };
2827                let listener = Arc::clone(&listener);
2828                async move {
2829                    match result {
2830                        Some(err) => Err(err),
2831                        None => listener.accept().await,
2832                    }
2833                }
2834            },
2835        ));
2836        let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
2837        authenticate(&mut client, &conn).await;
2838        let ping = Frame::build(
2839            FrameType::Ping,
2840            Flags::new(false, Priority::Passive, false),
2841            0,
2842            0,
2843            77,
2844            Vec::new(),
2845        )
2846        .unwrap();
2847        crate::write_frame(&mut client, &ping).await.unwrap();
2848        let pong = timeout(TEST_DEADLINE, read_frame(&mut client))
2849            .await
2850            .unwrap()
2851            .unwrap()
2852            .unwrap();
2853        assert_eq!(pong.header.ty, FrameType::Pong);
2854        assert!(
2855            !server.is_finished(),
2856            "a temporary accept failure must not stop the listener"
2857        );
2858        server.abort();
2859    }
2860
2861    #[tokio::test]
2862    async fn exhausted_accept_backs_off_then_serves_connection() {
2863        let listener = Arc::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
2864        let addr = listener.local_addr().unwrap();
2865        let (auth, conn) = test_auth();
2866        let mut attempts = 0;
2867        let server = tokio::spawn(serve_listener_with_accept(
2868            Some(addr),
2869            echo_router(),
2870            auth,
2871            move || {
2872                attempts += 1;
2873                // The platform's "too many open files" code: EMFILE on Unix,
2874                // WSAEMFILE on Windows. A bare 24 is ERROR_BAD_LENGTH on Windows.
2875                #[cfg(unix)]
2876                let emfile = rustix::io::Errno::MFILE.raw_os_error();
2877                #[cfg(windows)]
2878                let emfile = 10024;
2879                let error = (attempts == 1).then(|| io::Error::from_raw_os_error(emfile));
2880                let listener = Arc::clone(&listener);
2881                async move {
2882                    match error {
2883                        Some(err) => Err(err),
2884                        None => listener.accept().await,
2885                    }
2886                }
2887            },
2888        ));
2889        let started = Instant::now();
2890        let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
2891        authenticate(&mut client, &conn).await;
2892        assert!(
2893            started.elapsed() >= Duration::from_millis(50),
2894            "fd exhaustion must back off before retrying"
2895        );
2896        assert!(!server.is_finished());
2897        server.abort();
2898    }
2899
2900    #[tokio::test]
2901    async fn fatal_accept_error_still_stops_listener() {
2902        let (auth, _) = test_auth();
2903        let err = serve_listener_with_accept(None, echo_router(), auth, || async {
2904            Err(io::Error::from(io::ErrorKind::PermissionDenied))
2905        })
2906        .await
2907        .unwrap_err();
2908        assert!(
2909            matches!(err, ServerError::Accept { source, .. } if source.kind() == io::ErrorKind::PermissionDenied)
2910        );
2911    }
2912
2913    #[tokio::test]
2914    async fn serve_listeners_with_no_listeners_returns_typed_error() {
2915        let (auth, _conn) = test_auth();
2916        let err = serve_listeners(Vec::new(), echo_router(), auth)
2917            .await
2918            .expect_err("empty listener set must fail loudly");
2919        assert!(matches!(err, ServerError::NoListeners));
2920    }
2921
2922    #[tokio::test]
2923    async fn malformed_header_returns_typed_error_no_panic() {
2924        let (mut client, server_stream) = duplex(128);
2925        let (auth, conn) = test_auth();
2926        let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2927        authenticate(&mut client, &conn).await;
2928        let mut header = [0u8; HEADER_LEN];
2929        header[4] = PROTOCOL_VERSION;
2930        header[5] = 250;
2931
2932        client.write_all(&header).await.unwrap();
2933        drop(client);
2934
2935        let err = server.await.unwrap().unwrap_err();
2936        assert!(matches!(
2937            err,
2938            ConnectionError::FrameIo(FrameIoError::DecodeHeader(DecodeError::UnknownFrameType {
2939                byte: 250
2940            }))
2941        ));
2942    }
2943
2944    #[tokio::test]
2945    async fn truncated_body_returns_typed_error_no_panic() {
2946        let (mut client, server_stream) = duplex(128);
2947        let (auth, conn) = test_auth();
2948        let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2949        authenticate(&mut client, &conn).await;
2950        let frame = request(7, 8, b"abcd");
2951
2952        client.write_all(&frame.header.encode()).await.unwrap();
2953        client.write_all(b"ab").await.unwrap();
2954        drop(client);
2955
2956        let err = server.await.unwrap().unwrap_err();
2957        assert!(matches!(
2958            err,
2959            ConnectionError::FrameIo(FrameIoError::UnexpectedEof {
2960                stage: ReadStage::Body,
2961                expected: 4,
2962                actual: 2
2963            })
2964        ));
2965    }
2966
2967    #[tokio::test]
2968    async fn unknown_channel_is_returned_as_error_frame_and_connection_continues() {
2969        let (mut client, server_stream) = duplex(1024);
2970        let (auth, conn) = test_auth();
2971        let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2972        authenticate(&mut client, &conn).await;
2973        let unknown = request(42, 10, b"lost");
2974        let known = request(7, 11, b"still-routes");
2975
2976        crate::write_frame(&mut client, &unknown).await.unwrap();
2977        crate::write_frame(&mut client, &known).await.unwrap();
2978
2979        let error = crate::read_frame(&mut client).await.unwrap().unwrap();
2980        assert_eq!(error.header.ty, FrameType::Error);
2981        assert_eq!(error.header.channel, 42);
2982        assert_eq!(error.header.corr, 10);
2983        let error_body: ErrorBody = serde_json::from_slice(&error.body).unwrap();
2984        assert_eq!(error_body.code, "unknown_channel");
2985
2986        let response = crate::read_frame(&mut client).await.unwrap().unwrap();
2987        assert_eq!(response.header.ty, FrameType::Response);
2988        assert_eq!(response.header.channel, 7);
2989        assert_eq!(response.header.corr, 11);
2990        assert_eq!(response.body, b"still-routes");
2991
2992        drop(client);
2993        server.await.unwrap().unwrap();
2994    }
2995}