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
33pub const CONNECTION_EGRESS_BYTE_BUDGET: usize = 4 * 1024 * 1024;
45pub const CONNECTION_EGRESS_FRAME_CAP: usize = 32 * 1024;
54pub const MAX_PENDING_ROUTE_OPENS_PER_CONNECTION: usize = 8;
60pub(crate) const MAX_PENDING_ROUTE_BINDS_PER_TARGET: usize =
64 MAX_PENDING_ROUTE_OPENS_PER_CONNECTION * 2;
65pub const DEFAULT_AUTH_DEADLINE: Duration = Duration::from_secs(2);
66pub const DEFAULT_MAX_UNAUTHENTICATED_CONNECTIONS: usize = 256;
71const CLOSE_DRAIN_GRACE: Duration = Duration::from_secs(2);
72const PRE_AUTH_REJECT_LOG_INTERVAL: Duration = Duration::from_secs(1);
73
74#[derive(Clone, Copy, Debug)]
75enum PreAuthRejectStage {
76 WaitingForHandshakeSlot,
77 HandshakeDeadline,
78 InvalidProof,
79 MalformedMessage,
80 PeerClosed,
81 OtherAuthenticationFailure,
82}
83
84impl PreAuthRejectStage {
85 const fn index(self) -> usize {
86 match self {
87 Self::WaitingForHandshakeSlot => 0,
88 Self::HandshakeDeadline => 1,
89 Self::InvalidProof => 2,
90 Self::MalformedMessage => 3,
91 Self::PeerClosed => 4,
92 Self::OtherAuthenticationFailure => 5,
93 }
94 }
95
96 const fn stage(self) -> &'static str {
97 match self {
98 Self::WaitingForHandshakeSlot => "waiting_for_handshake_slot",
99 Self::HandshakeDeadline => "handshake_deadline",
100 Self::InvalidProof => "client_authentication",
101 Self::MalformedMessage => "malformed_message",
102 Self::PeerClosed => "peer_closed",
103 Self::OtherAuthenticationFailure => "authentication",
104 }
105 }
106
107 const fn reason(self) -> &'static str {
108 match self {
109 Self::WaitingForHandshakeSlot => "capacity_wait_timed_out",
110 Self::HandshakeDeadline => "handshake_deadline_expired",
111 Self::InvalidProof => "client_proof_mismatch",
112 Self::MalformedMessage => "malformed_or_oversized_message",
113 Self::PeerClosed => "peer_closed_before_authentication_completed",
114 Self::OtherAuthenticationFailure => "authentication_failed_before_routing",
115 }
116 }
117}
118
119#[derive(Debug)]
120struct PreAuthRejectRateLimit {
121 next_allowed_ns: AtomicU64,
122 suppressed: AtomicU64,
123}
124
125impl PreAuthRejectRateLimit {
126 const fn new() -> Self {
127 Self {
128 next_allowed_ns: AtomicU64::new(0),
129 suppressed: AtomicU64::new(0),
130 }
131 }
132}
133
134#[derive(Debug)]
137struct PreAuthRejectLog {
138 epoch: Instant,
139 limiter: PreAuthRejectLimiter,
140}
141
142impl PreAuthRejectLog {
143 fn new() -> Self {
144 Self {
145 epoch: Instant::now(),
146 limiter: PreAuthRejectLimiter::new(),
147 }
148 }
149}
150
151#[derive(Debug)]
153struct PreAuthRejectLimiter {
154 stages: [PreAuthRejectRateLimit; 6],
155}
156
157impl PreAuthRejectLimiter {
158 fn new() -> Self {
159 Self {
160 stages: std::array::from_fn(|_| PreAuthRejectRateLimit::new()),
161 }
162 }
163
164 fn allow_at(&self, stage: PreAuthRejectStage, now: Duration) -> Option<u64> {
165 let slot = &self.stages[stage.index()];
166 let now_ns = now.as_nanos().min(u64::MAX as u128) as u64;
167 let interval_ns = PRE_AUTH_REJECT_LOG_INTERVAL.as_nanos() as u64;
168 let mut next_allowed_ns = slot.next_allowed_ns.load(AtomicOrdering::Relaxed);
169
170 loop {
171 if now_ns < next_allowed_ns {
172 slot.suppressed.fetch_add(1, AtomicOrdering::Relaxed);
173 return None;
174 }
175
176 match slot.next_allowed_ns.compare_exchange_weak(
177 next_allowed_ns,
178 now_ns.saturating_add(interval_ns),
179 AtomicOrdering::Relaxed,
180 AtomicOrdering::Relaxed,
181 ) {
182 Ok(_) => return Some(slot.suppressed.swap(0, AtomicOrdering::Relaxed)),
183 Err(actual) => next_allowed_ns = actual,
184 }
185 }
186 }
187}
188
189fn log_pre_auth_rejection(
190 error: &ConnectionError,
191 peer_addr: SocketAddr,
192 elapsed: Duration,
193 now: Duration,
194 limiter: &PreAuthRejectLimiter,
195) {
196 let Some(stage) = error.pre_auth_reject_stage() else {
197 return;
198 };
199 let Some(suppressed_since_last) = limiter.allow_at(stage, now) else {
200 return;
201 };
202 let elapsed_ms = elapsed.as_millis().min(u64::MAX as u128) as u64;
203
204 info!(
205 peer_addr = %peer_addr,
206 elapsed_ms,
207 stage = stage.stage(),
208 reason = stage.reason(),
209 auth_stage = ?error.auth_stage(),
210 suppressed_since_last,
211 error = %error,
212 "subc TCP connection rejected before routing"
213 );
214}
215
216async fn handle_accepted_connection<S>(
217 stream: S,
218 router: Arc<Router>,
219 auth: ServerAuth,
220 peer_addr: SocketAddr,
221 accepted_at: Instant,
222) -> Result<(), ConnectionError>
223where
224 S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
225{
226 let reject_log = Arc::clone(&auth.pre_auth_reject_log);
227 let result = handle_connection(stream, router, auth).await;
228 if let Err(error) = &result {
229 if error.is_quiet_reject() {
230 log_pre_auth_rejection(
231 error,
232 peer_addr,
233 accepted_at.elapsed(),
234 reject_log.epoch.elapsed(),
235 &reject_log.limiter,
236 );
237 } else {
238 warn!(?peer_addr, error = %error, "subc connection ended with error");
239 }
240 }
241 result
242}
243
244#[derive(Clone)]
247pub struct ServerAuth {
248 key: Arc<[u8]>,
249 daemon_id: [u8; DAEMON_ID_LEN],
250 daemon_ver: Arc<str>,
251 deadline: Duration,
252 unauthenticated: Arc<Semaphore>,
253 connected_clients: ConnectedClients,
254 pre_auth_reject_log: Arc<PreAuthRejectLog>,
255}
256
257impl ServerAuth {
258 pub fn new(
259 key: Vec<u8>,
260 daemon_id: [u8; DAEMON_ID_LEN],
261 daemon_ver: impl Into<String>,
262 ) -> Self {
263 Self::with_limits(
264 key,
265 daemon_id,
266 daemon_ver,
267 DEFAULT_AUTH_DEADLINE,
268 DEFAULT_MAX_UNAUTHENTICATED_CONNECTIONS,
269 )
270 }
271
272 pub fn with_limits(
274 key: Vec<u8>,
275 daemon_id: [u8; DAEMON_ID_LEN],
276 daemon_ver: impl Into<String>,
277 deadline: Duration,
278 max_unauthenticated: usize,
279 ) -> Self {
280 Self {
281 key: Arc::from(key),
282 daemon_id,
283 daemon_ver: Arc::from(daemon_ver.into()),
284 deadline,
285 unauthenticated: Arc::new(Semaphore::new(max_unauthenticated.max(1))),
286 connected_clients: ConnectedClients::new(),
287 pre_auth_reject_log: Arc::new(PreAuthRejectLog::new()),
288 }
289 }
290
291 pub fn with_connected_clients(mut self, connected_clients: ConnectedClients) -> Self {
292 self.connected_clients = connected_clients;
293 self
294 }
295}
296
297impl fmt::Debug for ServerAuth {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 f.debug_struct("ServerAuth")
300 .field("key", &"<redacted>")
301 .field("daemon_id", &self.daemon_id)
302 .field("daemon_ver", &self.daemon_ver)
303 .field("deadline", &self.deadline)
304 .finish_non_exhaustive()
305 }
306}
307
308pub async fn serve_listener(
311 listener: TcpListener,
312 router: Arc<Router>,
313 auth: ServerAuth,
314) -> Result<(), ServerError> {
315 serve_listener_with_accept(listener.local_addr().ok(), router, auth, || {
316 listener.accept()
317 })
318 .await
319}
320
321async fn serve_listener_with_accept<A, F>(
322 local_addr: Option<SocketAddr>,
323 router: Arc<Router>,
324 auth: ServerAuth,
325 mut accept: A,
326) -> Result<(), ServerError>
327where
328 A: FnMut() -> F,
329 F: std::future::Future<Output = io::Result<(tokio::net::TcpStream, SocketAddr)>>,
330{
331 loop {
332 let (stream, peer_addr) = match accept().await {
333 Ok(accepted) => accepted,
334 Err(source) => {
335 let kind = source.kind();
336 let exhausted = accept_resource_exhausted(&source);
340 if matches!(
341 kind,
342 io::ErrorKind::ConnectionAborted
343 | io::ErrorKind::ConnectionReset
344 | io::ErrorKind::Interrupted
345 ) || exhausted
346 {
347 warn!(?local_addr, error = %source, "temporary TCP accept failure");
348 if exhausted {
349 tokio::time::sleep(Duration::from_millis(75)).await;
350 }
351 continue;
352 }
353 return Err(ServerError::Accept { local_addr, source });
354 }
355 };
356 let accepted_at = Instant::now();
357 if let Err(source) = stream.set_nodelay(true) {
373 warn!(?peer_addr, error = %source, "could not disable Nagle on accepted connection");
374 }
375 debug!(?peer_addr, ?local_addr, "accepted subc TCP connection");
376 let router = Arc::clone(&router);
377 let auth = auth.clone();
378 tokio::spawn(async move {
379 let _ = handle_accepted_connection(stream, router, auth, peer_addr, accepted_at).await;
380 });
381 }
382}
383
384fn accept_resource_exhausted(error: &io::Error) -> bool {
385 #[cfg(unix)]
390 {
391 use rustix::io::Errno;
392 let exhausted = [Errno::NFILE, Errno::MFILE, Errno::NOBUFS];
393 error
394 .raw_os_error()
395 .is_some_and(|code| exhausted.iter().any(|e| e.raw_os_error() == code))
396 }
397 #[cfg(windows)]
398 {
399 matches!(error.raw_os_error(), Some(10024 | 10055))
400 }
401 #[cfg(not(any(unix, windows)))]
402 {
403 let _ = error;
404 false
405 }
406}
407
408pub async fn serve_listeners(
410 listeners: Vec<TcpListener>,
411 router: Arc<Router>,
412 auth: ServerAuth,
413) -> Result<(), ServerError> {
414 if listeners.is_empty() {
415 return Err(ServerError::NoListeners);
416 }
417
418 let (tx, mut rx) = mpsc::channel(listeners.len());
419 let mut accept_tasks = AbortTasksOnDrop::default();
420 for listener in listeners {
421 let router = Arc::clone(&router);
422 let auth = auth.clone();
423 let tx = tx.clone();
424 accept_tasks.push(tokio::spawn(async move {
425 let result = serve_listener(listener, router, auth).await;
426 let _ = tx.send(result).await;
427 }));
428 }
429 drop(tx);
430
431 rx.recv().await.unwrap_or(Ok(()))
432}
433
434#[derive(Default)]
435struct AbortTasksOnDrop {
436 handles: Vec<JoinHandle<()>>,
437}
438
439impl AbortTasksOnDrop {
440 fn push(&mut self, handle: JoinHandle<()>) {
441 self.handles.push(handle);
442 }
443}
444
445impl Drop for AbortTasksOnDrop {
446 fn drop(&mut self) {
447 for handle in &self.handles {
448 if !handle.is_finished() {
449 handle.abort();
450 }
451 }
452 }
453}
454
455#[derive(Debug)]
456enum ConnectionLoopExit {
457 PeerClosed,
458 CloseRequested(CloseReason),
459}
460
461pub async fn handle_connection<S>(
470 mut stream: S,
471 router: Arc<Router>,
472 auth: ServerAuth,
473) -> Result<(), ConnectionError>
474where
475 S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
476{
477 let endpoints = (&stream as &dyn Any)
480 .downcast_ref::<tokio::net::TcpStream>()
481 .and_then(|tcp| tcp.local_addr().ok().zip(tcp.peer_addr().ok()));
482 let permit =
502 match tokio::time::timeout(auth.deadline, auth.unauthenticated.clone().acquire_owned())
503 .await
504 {
505 Ok(Ok(permit)) => permit,
506 Ok(Err(_)) | Err(_) => {
507 let _ = stream.shutdown().await;
508 return Err(ConnectionError::UnauthenticatedCapacity);
509 }
510 };
511
512 let authenticated = authenticate_server(
513 &mut stream,
514 auth.key.as_ref(),
515 &auth.daemon_id,
516 auth.daemon_ver.as_ref(),
517 auth.deadline,
518 )
519 .await
520 .map_err(ConnectionError::Auth)?;
521 drop(permit);
522
523 let mut connection = router.begin_connection();
524 let connection_id = connection.id();
525 router.record_caller_endpoints(connection_id, endpoints);
526 let _connected_client = (authenticated.role != WATCHDOG_CLIENT_ROLE)
541 .then(|| auth.connected_clients.open(connection_id));
542 let close_receiver = connection.take_close_receiver();
543 debug!(
544 connection_id = connection_id.get(),
545 "subc authenticated connection opened"
546 );
547
548 let (read_half, write_half) = tokio::io::split(stream);
549 let mut read_half = BufReader::new(read_half);
553 let (egress, rx) = connection_egress();
554 let writer = tokio::spawn(drain_writer(write_half, rx));
555
556 let ctx = RouteCtx {
557 connection_id,
558 egress: egress.clone(),
559 };
560
561 let mut route_open_tasks = JoinSet::new();
562 let mut privileged_tasks = JoinSet::new();
563 let loop_result = connection_loop(
564 &mut read_half,
565 Arc::clone(&router),
566 ctx.clone(),
567 close_receiver,
568 &mut route_open_tasks,
569 &mut privileged_tasks,
570 )
571 .await;
572
573 route_open_tasks.shutdown().await;
581 privileged_tasks.shutdown().await;
584
585 drop(ctx);
586 drop(egress);
587 drop(connection);
588
589 let close_reason = match &loop_result {
590 Ok(ConnectionLoopExit::CloseRequested(reason)) => Some(reason.to_string()),
591 Ok(ConnectionLoopExit::PeerClosed) | Err(_) => None,
592 };
593 let writer_result = finish_connection_writer(writer, connection_id.get()).await;
594
595 let result = if let Some(reason) = close_reason.as_deref() {
596 match writer_result {
597 Some(Ok(Ok(()))) | None => Ok(()),
598 Some(Ok(Err(writer_err))) => {
599 debug!(
600 connection_id = connection_id.get(),
601 close_reason = reason,
602 writer_error = %writer_err,
603 "writer failed after requested connection close"
604 );
605 Ok(())
606 }
607 Some(Err(join_err)) => {
608 warn!(
609 connection_id = connection_id.get(),
610 close_reason = reason,
611 join_error = %join_err,
612 "writer task join failed after requested connection close"
613 );
614 Ok(())
615 }
616 }
617 } else {
618 match (loop_result, writer_result) {
619 (Err(loop_err), Some(Ok(Ok(()))) | None) => Err(loop_err),
620 (Err(loop_err), Some(Ok(Err(writer_err)))) => {
621 warn!(
622 connection_id = connection_id.get(),
623 writer_error = %writer_err,
624 "writer failed while closing after connection error"
625 );
626 Err(loop_err)
627 }
628 (Err(loop_err), Some(Err(join_err))) => {
629 warn!(
630 connection_id = connection_id.get(),
631 join_error = %join_err,
632 "writer task join failed while closing after connection error"
633 );
634 Err(loop_err)
635 }
636 (Ok(ConnectionLoopExit::PeerClosed), Some(Ok(Ok(()))) | None) => Ok(()),
637 (Ok(ConnectionLoopExit::PeerClosed), Some(Ok(Err(writer_err)))) => {
638 Err(ConnectionError::FrameIo(writer_err))
639 }
640 (Ok(ConnectionLoopExit::PeerClosed), Some(Err(join_err))) => Err(join_err),
641 (Ok(ConnectionLoopExit::CloseRequested(_)), _) => {
642 unreachable!("close requests are handled before normal writer result matching")
643 }
644 }
645 };
646
647 match &result {
648 Ok(()) => {
649 if let Some(reason) = close_reason.as_deref() {
650 debug!(
651 connection_id = connection_id.get(),
652 close_reason = reason,
653 "subc connection closed by request"
654 );
655 } else {
656 debug!(
657 connection_id = connection_id.get(),
658 "subc connection closed"
659 );
660 }
661 }
662 Err(err) => debug!(
663 connection_id = connection_id.get(),
664 error = %err,
665 "subc connection exited with error"
666 ),
667 }
668
669 result
670}
671
672async fn finish_connection_writer(
673 mut writer: JoinHandle<Result<(), FrameIoError>>,
674 connection_id: u64,
675) -> Option<Result<Result<(), FrameIoError>, ConnectionError>> {
676 match timeout(CLOSE_DRAIN_GRACE, &mut writer).await {
679 Ok(result) => Some(result.map_err(ConnectionError::WriterTask)),
680 Err(_) => {
681 warn!(
682 connection_id,
683 grace = ?CLOSE_DRAIN_GRACE,
684 "connection writer did not drain during teardown; aborting writer task"
685 );
686 writer.abort();
687 let _ = writer.await;
688 None
689 }
690 }
691}
692
693async fn connection_loop<R>(
694 read_half: &mut R,
695 router: Arc<Router>,
696 ctx: RouteCtx,
697 mut close_receiver: ConnectionCloseReceiver,
698 route_open_tasks: &mut JoinSet<Result<(), RouterError>>,
699 privileged_tasks: &mut JoinSet<Result<(), RouterError>>,
700) -> Result<ConnectionLoopExit, ConnectionError>
701where
702 R: AsyncRead + Unpin,
703{
704 let mut activity_tasks = JoinSet::new();
705 loop {
706 while let Some(result) = activity_tasks.try_join_next() {
707 finish_route_open_task(result)?;
708 }
709 while let Some(result) = privileged_tasks.try_join_next() {
710 finish_route_open_task(result)?;
711 }
712 while let Some(result) = route_open_tasks.try_join_next() {
713 finish_route_open_task(result)?;
714 }
715
716 let read = read_frame(&mut *read_half);
719 tokio::pin!(read);
720 let frame = loop {
721 tokio::select! {
722 close = &mut close_receiver => {
723 return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
724 }
725 result = route_open_tasks.join_next(), if !route_open_tasks.is_empty() => {
726 finish_route_open_task(
727 result.expect("a non-empty route.open JoinSet has a next task")
728 )?;
729 }
730 result = activity_tasks.join_next(), if !activity_tasks.is_empty() => {
731 finish_route_open_task(result.expect("non-empty activity task set"))?;
732 }
733 result = privileged_tasks.join_next(), if !privileged_tasks.is_empty() => {
734 finish_route_open_task(result.expect("non-empty privileged task set"))?;
735 }
736 result = &mut read => {
737 break match result.map_err(ConnectionError::FrameIo)? {
738 Some(frame) => frame,
739 None => return Ok(ConnectionLoopExit::PeerClosed),
740 };
741 }
742 }
743 };
744
745 if frame.header.channel == 0
746 && frame.header.ty == subc_protocol::FrameType::Request
747 && serde_json::from_slice::<serde_json::Value>(&frame.body)
748 .ok()
749 .is_some_and(|body| {
750 matches!(
751 body["op"].as_str(),
752 Some(
753 subc_control::ops::ACTIVITY_RECORD
754 | subc_control::ops::ACTIVITY_QUERY
755 | subc_control::ops::ACTIVITY_SUBSCRIBE
756 )
757 )
758 })
759 {
760 if activity_tasks.len() >= 128 {
763 let response = crate::control::control_error_frame(
764 &frame,
765 subc_control::ACTIVITY_UNAVAILABLE,
766 "activity queue full",
767 )
768 .map_err(ConnectionError::Router)?;
769 let send_result = tokio::select! {
770 close = &mut close_receiver => {
771 return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
772 }
773 result = ctx.egress.send(response) => result,
774 };
775 send_result.map_err(ConnectionError::Router)?;
776 } else {
777 let task_router = Arc::clone(&router);
778 let task_ctx = ctx.clone();
779 activity_tasks.spawn(async move {
780 route_open_tail(task_router, task_ctx, frame, Instant::now()).await
781 });
782 }
783 continue;
784 }
785
786 if let Some(target_module_id) = router.route_open_target(&frame) {
787 while let Some(result) = route_open_tasks.try_join_next() {
791 finish_route_open_task(result)?;
792 }
793
794 if route_open_tasks.len() >= MAX_PENDING_ROUTE_OPENS_PER_CONNECTION {
795 let refusal = router
798 .route_open_capacity_refusal(
799 &ctx,
800 &frame,
801 &target_module_id,
802 route_open_tasks.len(),
803 MAX_PENDING_ROUTE_OPENS_PER_CONNECTION,
804 )
805 .map_err(ConnectionError::Router)?;
806 let send_result = tokio::select! {
807 close = &mut close_receiver => {
808 return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
809 }
810 result = ctx.egress.send(refusal) => result,
811 };
812 send_result.map_err(ConnectionError::Router)?;
813 continue;
814 }
815
816 let task_router = Arc::clone(&router);
817 let task_ctx = ctx.clone();
818 let dispatch_started_at = Instant::now();
821 route_open_tasks.spawn(async move {
822 route_open_tail(task_router, task_ctx, frame, dispatch_started_at).await
823 });
824 continue;
825 }
826
827 if router.defers_privileged_control(&ctx, &frame) {
828 let task_router = Arc::clone(&router);
829 let task_ctx = ctx.clone();
830 let started = Instant::now();
831 privileged_tasks
832 .spawn(async move { route_open_tail(task_router, task_ctx, frame, started).await });
833 continue;
834 }
835
836 let route_result = tokio::select! {
839 close = &mut close_receiver => {
840 return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
841 }
842 result = router.route_for_connection(&ctx, frame) => result,
843 };
844
845 if let Err(err) = route_result {
846 if let Some(error_frame) = err.to_error_frame() {
847 warn!(
848 connection_id = ctx.connection_id.get(),
849 error = %err,
850 "routing failure recovered with ERROR frame"
851 );
852 let send_result = tokio::select! {
853 close = &mut close_receiver => {
854 return Ok(ConnectionLoopExit::CloseRequested(close_reason(close)));
855 }
856 result = ctx.egress.send(error_frame) => result,
857 };
858 send_result.map_err(ConnectionError::Router)?;
859 } else {
860 debug!(
861 connection_id = ctx.connection_id.get(),
862 error = %err,
863 "fatal routing failure"
864 );
865 return Err(ConnectionError::Router(err));
866 }
867 }
868 }
869}
870
871async fn route_open_tail(
872 router: Arc<Router>,
873 ctx: RouteCtx,
874 frame: crate::Frame,
875 dispatch_started_at: Instant,
876) -> Result<(), RouterError> {
877 match router
878 .route_for_connection_started(&ctx, frame, Some(dispatch_started_at))
879 .await
880 {
881 Ok(()) => Ok(()),
882 Err(err) => {
883 let Some(error_frame) = err.to_error_frame() else {
884 return Err(err);
885 };
886 warn!(
887 connection_id = ctx.connection_id.get(),
888 error = %err,
889 "routing failure recovered with ERROR frame"
890 );
891 ctx.egress.send(error_frame).await
892 }
893 }
894}
895
896fn finish_route_open_task(
897 result: Result<Result<(), RouterError>, tokio::task::JoinError>,
898) -> Result<(), ConnectionError> {
899 match result {
900 Ok(Ok(())) => Ok(()),
901 Ok(Err(err)) => Err(ConnectionError::Router(err)),
902 Err(err) => Err(ConnectionError::Router(RouterError::backend(
903 0,
904 0,
905 format!("route.open task failed: {err}"),
906 ))),
907 }
908}
909
910fn close_reason(
911 result: Result<CloseReason, tokio::sync::oneshot::error::RecvError>,
912) -> CloseReason {
913 result.unwrap_or_else(|_| {
914 CloseReason::new(
915 "close_registry_dropped",
916 "connection close registration was dropped without a reason",
917 )
918 })
919}
920
921pub(crate) fn connection_egress() -> (FrameSink, mpsc::Receiver<crate::router::OutboundFrame>) {
925 let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(CONNECTION_EGRESS_FRAME_CAP);
926 (
927 FrameSink::with_byte_budget(tx, CONNECTION_EGRESS_BYTE_BUDGET),
928 rx,
929 )
930}
931
932async fn drain_writer<W>(
933 write_half: W,
934 mut rx: mpsc::Receiver<crate::router::OutboundFrame>,
935) -> Result<(), FrameIoError>
936where
937 W: AsyncWrite + Unpin,
938{
939 let mut writer = BufWriter::new(write_half);
940 while let Some(outbound) = rx.recv().await {
941 write_outbound(&mut writer, outbound).await?;
942 while let Ok(outbound) = rx.try_recv() {
943 write_outbound(&mut writer, outbound).await?;
944 }
945 writer.flush().await.map_err(FrameIoError::Io)?;
946 }
947 writer.flush().await.map_err(FrameIoError::Io)?;
948 Ok(())
949}
950
951async fn write_outbound<W>(
958 writer: &mut BufWriter<W>,
959 outbound: crate::router::OutboundFrame,
960) -> Result<(), FrameIoError>
961where
962 W: AsyncWrite + Unpin,
963{
964 const SLOW_REPLY_QUEUE: Duration = Duration::from_millis(1000);
965 let queued = outbound.enqueued_at.elapsed();
966 let charge = outbound.charge;
969 if let Some(charge) = &charge {
970 charge.taken_by_writer();
971 }
972 let frame = outbound.frame;
973 if frame.header.channel == 0 && queued >= SLOW_REPLY_QUEUE {
974 let write_started = std::time::Instant::now();
975 let result = write_frame(writer, &frame).await;
976 tracing::warn!(
977 corr = frame.header.corr,
978 queued_ms = queued.as_millis() as u64,
979 write_ms = write_started.elapsed().as_millis() as u64,
980 "slow control reply write"
981 );
982 result?;
983 } else {
984 write_frame(writer, &frame).await?;
985 }
986 if let Some(flushed) = outbound.flushed {
987 writer.flush().await.map_err(FrameIoError::Io)?;
988 let _ = flushed.send(());
989 }
990 Ok(())
991}
992
993#[cfg(all(test, unix))]
994#[tokio::test]
995async fn shutdown_notice_ack_waits_for_socket_flush() {
996 let (socket, mut peer) = tokio::io::duplex(1);
997 let (tx, rx) = mpsc::channel(1);
998 let sink = FrameSink::new(tx);
999 let writer = tokio::spawn(drain_writer(socket, rx));
1000 let frame = crate::Frame::build(
1001 subc_protocol::FrameType::Push,
1002 subc_protocol::Flags::new(false, subc_protocol::Priority::Interactive, false),
1003 0,
1004 0,
1005 0,
1006 b"notice".to_vec(),
1007 )
1008 .unwrap();
1009 let send = sink.send_flushed(frame);
1010 tokio::pin!(send);
1011 assert!(
1012 timeout(Duration::from_millis(20), &mut send).await.is_err(),
1013 "queueing bytes is not a socket flush acknowledgement"
1014 );
1015 let (sent, received) = timeout(Duration::from_secs(1), async {
1016 tokio::join!(&mut send, read_frame(&mut peer))
1017 })
1018 .await
1019 .unwrap();
1020 sent.unwrap();
1021 assert_eq!(received.unwrap().unwrap().body, b"notice");
1022 writer.abort();
1023}
1024
1025#[derive(Debug)]
1026pub enum ServerError {
1027 NoListeners,
1028 Accept {
1029 local_addr: Option<SocketAddr>,
1030 source: io::Error,
1031 },
1032}
1033
1034impl fmt::Display for ServerError {
1035 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1036 match self {
1037 Self::NoListeners => write!(f, "no TCP listeners were provided"),
1038 Self::Accept { local_addr, source } => match local_addr {
1039 Some(addr) => write!(f, "failed to accept TCP connection on {addr}: {source}"),
1040 None => write!(f, "failed to accept TCP connection: {source}"),
1041 },
1042 }
1043 }
1044}
1045
1046impl Error for ServerError {
1047 fn source(&self) -> Option<&(dyn Error + 'static)> {
1048 match self {
1049 Self::Accept { source, .. } => Some(source),
1050 Self::NoListeners => None,
1051 }
1052 }
1053}
1054
1055#[derive(Debug)]
1056pub enum ConnectionError {
1057 Auth(AuthError),
1058 UnauthenticatedCapacity,
1059 FrameIo(FrameIoError),
1060 Router(RouterError),
1061 WriterTask(tokio::task::JoinError),
1062}
1063
1064impl ConnectionError {
1065 fn is_quiet_reject(&self) -> bool {
1066 matches!(self, Self::Auth(_) | Self::UnauthenticatedCapacity)
1067 }
1068
1069 fn pre_auth_reject_stage(&self) -> Option<PreAuthRejectStage> {
1070 match self {
1071 Self::UnauthenticatedCapacity => Some(PreAuthRejectStage::WaitingForHandshakeSlot),
1072 Self::Auth(AuthError::Timeout { .. }) => Some(PreAuthRejectStage::HandshakeDeadline),
1073 Self::Auth(
1074 AuthError::InvalidClientAuth
1075 | AuthError::InvalidServerProof
1076 | AuthError::DaemonIdMismatch,
1077 ) => Some(PreAuthRejectStage::InvalidProof),
1078 Self::Auth(AuthError::MessageTooLarge { .. } | AuthError::JsonDecode { .. }) => {
1079 Some(PreAuthRejectStage::MalformedMessage)
1080 }
1081 Self::Auth(AuthError::UnexpectedEof { .. }) => Some(PreAuthRejectStage::PeerClosed),
1082 Self::Auth(AuthError::Io { source, .. })
1083 if matches!(
1084 source.kind(),
1085 io::ErrorKind::BrokenPipe
1086 | io::ErrorKind::ConnectionAborted
1087 | io::ErrorKind::ConnectionReset
1088 | io::ErrorKind::UnexpectedEof
1089 ) =>
1090 {
1091 Some(PreAuthRejectStage::PeerClosed)
1092 }
1093 Self::Auth(_) => Some(PreAuthRejectStage::OtherAuthenticationFailure),
1094 Self::FrameIo(_) | Self::Router(_) | Self::WriterTask(_) => None,
1095 }
1096 }
1097
1098 fn auth_stage(&self) -> Option<AuthStage> {
1099 match self {
1100 Self::Auth(
1101 AuthError::Io { stage, .. }
1102 | AuthError::Timeout { stage, .. }
1103 | AuthError::UnexpectedEof { stage, .. }
1104 | AuthError::MessageTooLarge { stage, .. }
1105 | AuthError::JsonEncode { stage, .. }
1106 | AuthError::JsonDecode { stage, .. },
1107 ) => Some(*stage),
1108 Self::Auth(
1109 AuthError::Random(_)
1110 | AuthError::KeyTooShort { .. }
1111 | AuthError::InvalidServerProof
1112 | AuthError::DaemonIdMismatch
1113 | AuthError::InvalidClientAuth,
1114 )
1115 | Self::UnauthenticatedCapacity
1116 | Self::FrameIo(_)
1117 | Self::Router(_)
1118 | Self::WriterTask(_) => None,
1119 }
1120 }
1121}
1122
1123impl fmt::Display for ConnectionError {
1124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1125 match self {
1126 Self::Auth(err) => write!(f, "connection auth failed: {err}"),
1127 Self::UnauthenticatedCapacity => write!(
1128 f,
1129 "too many concurrent unauthenticated subc TCP connections"
1130 ),
1131 Self::FrameIo(err) => write!(f, "frame connection error: {err}"),
1132 Self::Router(err) => write!(f, "router connection error: {err}"),
1133 Self::WriterTask(err) => write!(f, "connection writer task failed: {err}"),
1134 }
1135 }
1136}
1137
1138impl Error for ConnectionError {
1139 fn source(&self) -> Option<&(dyn Error + 'static)> {
1140 match self {
1141 Self::Auth(err) => Some(err),
1142 Self::FrameIo(err) => Some(err),
1143 Self::Router(err) => Some(err),
1144 Self::WriterTask(err) => Some(err),
1145 Self::UnauthenticatedCapacity => None,
1146 }
1147 }
1148}
1149
1150#[cfg(test)]
1151mod tests {
1152 use std::{
1153 future::Future,
1154 pin::Pin,
1155 sync::atomic::{AtomicUsize, Ordering},
1156 task::{Context, Poll},
1157 };
1158
1159 use super::*;
1160 use serde::{de::DeserializeOwned, Serialize};
1161 use subc_protocol::{
1162 DecodeError, ErrorBody, Flags, FrameType, Priority, HEADER_LEN, PROTOCOL_VERSION,
1163 };
1164 use tokio::io::{duplex, AsyncReadExt, AsyncWriteExt, ReadBuf};
1165
1166 use subc_transport::{
1167 authenticate_client, compute_proof, ClientAuth, ClientHello, ConnectionInfo, Endpoint,
1168 ServerProof, CLIENT_AUTH_DOMAIN, NONCE_LEN, SCHEMA_VERSION,
1169 };
1170
1171 use crate::{ControlHandler, EchoBackend, Frame, ReadStage, Registry};
1172
1173 const TEST_DEADLINE: Duration = Duration::from_secs(2);
1174 const TEST_DAEMON_VER: &str = "test-subc-server";
1175
1176 struct ObservedStream {
1177 inner: tokio::io::DuplexStream,
1178 write_blocked: Arc<tokio::sync::Notify>,
1179 dropped: Arc<AtomicUsize>,
1180 read_error: Option<tokio::sync::oneshot::Receiver<()>>,
1181 }
1182
1183 impl AsyncRead for ObservedStream {
1184 fn poll_read(
1185 mut self: Pin<&mut Self>,
1186 cx: &mut Context<'_>,
1187 buf: &mut ReadBuf<'_>,
1188 ) -> Poll<io::Result<()>> {
1189 if let Some(receiver) = self.read_error.as_mut() {
1190 match Pin::new(receiver).poll(cx) {
1191 Poll::Ready(Ok(())) => {
1192 self.read_error = None;
1193 return Poll::Ready(Err(io::ErrorKind::ConnectionReset.into()));
1194 }
1195 Poll::Ready(Err(_)) => self.read_error = None,
1196 Poll::Pending => {}
1197 }
1198 }
1199 Pin::new(&mut self.inner).poll_read(cx, buf)
1200 }
1201 }
1202
1203 impl AsyncWrite for ObservedStream {
1204 fn poll_write(
1205 mut self: Pin<&mut Self>,
1206 cx: &mut Context<'_>,
1207 buf: &[u8],
1208 ) -> Poll<io::Result<usize>> {
1209 let result = Pin::new(&mut self.inner).poll_write(cx, buf);
1210 if result.is_pending() {
1211 self.write_blocked.notify_one();
1212 }
1213 result
1214 }
1215
1216 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1217 Pin::new(&mut self.inner).poll_flush(cx)
1218 }
1219
1220 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
1221 Pin::new(&mut self.inner).poll_shutdown(cx)
1222 }
1223 }
1224
1225 impl Drop for ObservedStream {
1226 fn drop(&mut self) {
1227 self.dropped.fetch_add(1, Ordering::SeqCst);
1228 }
1229 }
1230
1231 struct BlockedWriterConnection {
1232 client: tokio::io::DuplexStream,
1233 server: JoinHandle<Result<(), ConnectionError>>,
1234 stream_dropped: Arc<AtomicUsize>,
1235 read_error: Option<tokio::sync::oneshot::Sender<()>>,
1236 router: Arc<Router>,
1237 }
1238
1239 impl BlockedWriterConnection {
1240 async fn new() -> Self {
1241 let (mut client, inner) = duplex(256);
1242 let write_blocked = Arc::new(tokio::sync::Notify::new());
1243 let stream_dropped = Arc::new(AtomicUsize::new(0));
1244 let (read_error, receiver) = tokio::sync::oneshot::channel();
1245 let stream = ObservedStream {
1246 inner,
1247 write_blocked: Arc::clone(&write_blocked),
1248 dropped: Arc::clone(&stream_dropped),
1249 read_error: Some(receiver),
1250 };
1251 let (auth, conn) = test_auth();
1252 let router = echo_router();
1253 let server = tokio::spawn(handle_connection(stream, Arc::clone(&router), auth));
1254 authenticate(&mut client, &conn).await;
1255
1256 let _ = timeout(Duration::ZERO, write_blocked.notified()).await;
1259 write_frame(&mut client, &request(7, 1, &[b'x'; 1024]))
1260 .await
1261 .unwrap();
1262 timeout(TEST_DEADLINE, write_blocked.notified())
1263 .await
1264 .expect("writer should block on the unread echo reply");
1265 assert!(!server.is_finished());
1266 assert_eq!(stream_dropped.load(Ordering::SeqCst), 0);
1267 Self {
1268 client,
1269 server,
1270 stream_dropped,
1271 read_error: Some(read_error),
1272 router,
1273 }
1274 }
1275
1276 async fn finish(&mut self) -> Result<(), ConnectionError> {
1277 let started = tokio::time::Instant::now();
1278 let result = timeout(CLOSE_DRAIN_GRACE + Duration::from_secs(1), &mut self.server)
1279 .await
1280 .expect("connection teardown exceeded writer drain grace plus margin")
1281 .expect("connection task should not panic");
1282 assert!(started.elapsed() >= CLOSE_DRAIN_GRACE);
1283 assert_eq!(
1284 self.stream_dropped.load(Ordering::SeqCst),
1285 1,
1286 "writer must release its stream half before teardown completes"
1287 );
1288 result
1289 }
1290
1291 fn request_close(&self) {
1292 assert!(self.router.forwarding().request_connection_close(
1294 crate::registry::ConnectionId::new(1),
1295 CloseReason::new("test_close", "test requested close"),
1296 ));
1297 }
1298 }
1299
1300 #[tokio::test(start_paused = true)]
1301 async fn peer_eof_bounds_blocked_writer_teardown() {
1302 let mut connection = BlockedWriterConnection::new().await;
1303 connection.client.shutdown().await.unwrap();
1306 connection.finish().await.unwrap();
1307 }
1308
1309 #[tokio::test(start_paused = true)]
1310 async fn read_error_bounds_blocked_writer_teardown() {
1311 let mut connection = BlockedWriterConnection::new().await;
1312 connection.read_error.take().unwrap().send(()).unwrap();
1313 let error = connection.finish().await.unwrap_err();
1314 assert!(matches!(
1315 error,
1316 ConnectionError::FrameIo(FrameIoError::Io(error))
1317 if error.kind() == io::ErrorKind::ConnectionReset
1318 ));
1319 }
1320
1321 #[tokio::test(start_paused = true)]
1322 async fn close_requested_bounds_blocked_writer_teardown() {
1323 let mut connection = BlockedWriterConnection::new().await;
1324 connection.request_close();
1325 connection.finish().await.unwrap();
1326 }
1327
1328 #[tokio::test(start_paused = true)]
1329 async fn close_requested_flushes_queued_reply_before_grace() {
1330 let mut connection = BlockedWriterConnection::new().await;
1331 connection.request_close();
1332 let started = tokio::time::Instant::now();
1333 let reply = timeout(CLOSE_DRAIN_GRACE, read_frame(&mut connection.client))
1334 .await
1335 .expect("queued reply should flush before the grace expires")
1336 .unwrap()
1337 .unwrap();
1338 assert_eq!(reply.body, vec![b'x'; 1024]);
1339 timeout(CLOSE_DRAIN_GRACE, &mut connection.server)
1340 .await
1341 .unwrap()
1342 .unwrap()
1343 .unwrap();
1344 assert!(started.elapsed() < CLOSE_DRAIN_GRACE);
1345 assert_eq!(connection.stream_dropped.load(Ordering::SeqCst), 1);
1346 }
1347
1348 #[tokio::test]
1353 async fn stale_queued_control_reply_logs_slow_reply_write() {
1354 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::WARN);
1355 let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(4);
1356 let reply = Frame::build_with_version(
1357 PROTOCOL_VERSION,
1358 FrameType::Error,
1359 Flags::new(false, Priority::Interactive, false),
1360 0,
1361 0,
1362 7,
1363 serde_json::to_vec(&ErrorBody {
1364 code: "test".into(),
1365 message: "reply".into(),
1366 detail: None,
1367 })
1368 .expect("body encodes"),
1369 )
1370 .expect("frame builds");
1371 tx.send(crate::router::OutboundFrame {
1372 frame: reply,
1373 enqueued_at: std::time::Instant::now() - Duration::from_millis(1500),
1374 flushed: None,
1375 charge: None,
1376 })
1377 .await
1378 .expect("queued");
1379 drop(tx);
1380 let (write_half, mut read_half) = duplex(64 * 1024);
1381 drain_writer(write_half, rx).await.expect("writer drains");
1382 let mut sink = Vec::new();
1383 read_half.read_to_end(&mut sink).await.expect("read");
1384 assert!(!sink.is_empty(), "frame reached the socket");
1385 let captured = crate::router::test_log::captured_logs(&logs);
1386 assert!(
1387 captured.contains("slow control reply write") && captured.contains("corr=7"),
1388 "expected slow reply WARN naming corr, got: {captured}"
1389 );
1390 let queued_ms: u64 = captured
1391 .split("queued_ms=")
1392 .nth(1)
1393 .and_then(|s| s.split_whitespace().next())
1394 .and_then(|s| s.parse().ok())
1395 .expect("queued_ms present");
1396 assert!(
1397 queued_ms >= 1500,
1398 "queued_ms reflects residency: {queued_ms}"
1399 );
1400 }
1401
1402 #[tokio::test]
1406 async fn fresh_control_and_stale_data_frames_log_nothing() {
1407 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::WARN);
1408 let (tx, rx) = mpsc::channel::<crate::router::OutboundFrame>(4);
1409 let control = Frame::build_with_version(
1410 PROTOCOL_VERSION,
1411 FrameType::Error,
1412 Flags::new(false, Priority::Interactive, false),
1413 0,
1414 0,
1415 8,
1416 serde_json::to_vec(&ErrorBody {
1417 code: "test".into(),
1418 message: "fresh".into(),
1419 detail: None,
1420 })
1421 .expect("body encodes"),
1422 )
1423 .expect("frame builds");
1424 tx.send(crate::router::OutboundFrame {
1425 frame: control,
1426 enqueued_at: std::time::Instant::now(),
1427 flushed: None,
1428 charge: None,
1429 })
1430 .await
1431 .expect("queued");
1432 let data = Frame::build_with_version(
1433 PROTOCOL_VERSION,
1434 FrameType::Error,
1435 Flags::new(false, Priority::Interactive, false),
1436 9,
1437 1,
1438 9,
1439 serde_json::to_vec(&ErrorBody {
1440 code: "test".into(),
1441 message: "data".into(),
1442 detail: None,
1443 })
1444 .expect("body encodes"),
1445 )
1446 .expect("frame builds");
1447 tx.send(crate::router::OutboundFrame {
1448 frame: data,
1449 enqueued_at: std::time::Instant::now() - Duration::from_millis(5000),
1450 flushed: None,
1451 charge: None,
1452 })
1453 .await
1454 .expect("queued");
1455 drop(tx);
1456 let (write_half, _read_half) = duplex(64 * 1024);
1457 drain_writer(write_half, rx).await.expect("writer drains");
1458 let captured = crate::router::test_log::captured_logs(&logs);
1459 assert!(
1460 !captured.contains("slow control reply write"),
1461 "no WARN for fresh control or stale data frames, got: {captured}"
1462 );
1463 }
1464
1465 struct CountingReader {
1466 bytes: Vec<u8>,
1467 offset: usize,
1468 first_read_end: Option<usize>,
1469 reads: Arc<AtomicUsize>,
1470 }
1471
1472 impl CountingReader {
1473 fn new(bytes: Vec<u8>, first_read_end: Option<usize>) -> (Self, Arc<AtomicUsize>) {
1474 let reads = Arc::new(AtomicUsize::new(0));
1475 (
1476 Self {
1477 bytes,
1478 offset: 0,
1479 first_read_end,
1480 reads: Arc::clone(&reads),
1481 },
1482 reads,
1483 )
1484 }
1485 }
1486
1487 impl AsyncRead for CountingReader {
1488 fn poll_read(
1489 mut self: Pin<&mut Self>,
1490 _cx: &mut Context<'_>,
1491 buf: &mut ReadBuf<'_>,
1492 ) -> Poll<io::Result<()>> {
1493 self.reads.fetch_add(1, Ordering::Relaxed);
1494 let available = self.bytes.len().saturating_sub(self.offset);
1495 let first_read_remaining = self
1496 .first_read_end
1497 .filter(|end| self.offset < *end)
1498 .map_or(available, |end| end - self.offset);
1499 let count = available.min(first_read_remaining).min(buf.remaining());
1500 let end = self.offset + count;
1501 buf.put_slice(&self.bytes[self.offset..end]);
1502 self.offset = end;
1503 Poll::Ready(Ok(()))
1504 }
1505 }
1506
1507 fn encode_frames(frames: &[Frame]) -> Vec<u8> {
1508 let mut bytes = Vec::new();
1509 for frame in frames {
1510 bytes.extend_from_slice(&frame.header.encode());
1511 bytes.extend_from_slice(&frame.body);
1512 }
1513 bytes
1514 }
1515
1516 async fn read_frames<R>(reader: &mut R, count: usize) -> Vec<Frame>
1517 where
1518 R: AsyncRead + Unpin,
1519 {
1520 let mut frames = Vec::with_capacity(count);
1521 for _ in 0..count {
1522 frames.push(read_frame(reader).await.unwrap().unwrap());
1523 }
1524 frames
1525 }
1526
1527 fn request(channel: u16, corr: u64, body: &[u8]) -> Frame {
1528 Frame::build(
1529 FrameType::Request,
1530 Flags::new(true, Priority::Interactive, false),
1531 channel,
1532 0,
1533 corr,
1534 body.to_vec(),
1535 )
1536 .unwrap()
1537 }
1538
1539 fn echo_router() -> Arc<Router> {
1540 let mut router = Router::with_default_self_handler();
1541 router.register_backend(7, EchoBackend).unwrap();
1542 router.register_backend(9, EchoBackend).unwrap();
1543 Arc::new(router)
1544 }
1545
1546 fn test_auth() -> (ServerAuth, ConnectionInfo) {
1547 test_auth_with_limit(4)
1548 }
1549
1550 fn test_auth_with_limit(max_unauthenticated: usize) -> (ServerAuth, ConnectionInfo) {
1551 test_auth_with_deadline_and_limit(TEST_DEADLINE, max_unauthenticated)
1552 }
1553
1554 fn test_auth_with_deadline_and_limit(
1555 deadline: Duration,
1556 max_unauthenticated: usize,
1557 ) -> (ServerAuth, ConnectionInfo) {
1558 let key = vec![0x42; 32];
1559 let daemon_id = [0x24; 16];
1560 let conn = ConnectionInfo {
1561 schema: SCHEMA_VERSION,
1562 wire_version: None,
1563 endpoints: vec![Endpoint {
1564 host: "127.0.0.1".to_owned(),
1565 port: 1,
1566 }],
1567 key: key.clone(),
1568 daemon_id,
1569 pid: std::process::id(),
1570 daemon_ver: TEST_DAEMON_VER.to_owned(),
1571 };
1572 (
1573 ServerAuth::with_limits(
1574 key,
1575 daemon_id,
1576 TEST_DAEMON_VER,
1577 deadline,
1578 max_unauthenticated,
1579 ),
1580 conn,
1581 )
1582 }
1583
1584 async fn authenticate<S>(stream: &mut S, conn: &ConnectionInfo)
1585 where
1586 S: AsyncRead + AsyncWrite + Unpin,
1587 {
1588 authenticate_client(stream, conn, TEST_DEADLINE)
1589 .await
1590 .expect("test client should authenticate")
1591 }
1592
1593 async fn write_auth_message<S, T>(stream: &mut S, value: &T)
1594 where
1595 S: AsyncWrite + Unpin,
1596 T: Serialize,
1597 {
1598 let body = serde_json::to_vec(value).expect("auth message encodes");
1599 stream
1600 .write_all(&(body.len() as u32).to_le_bytes())
1601 .await
1602 .expect("auth message length writes");
1603 stream
1604 .write_all(&body)
1605 .await
1606 .expect("auth message body writes");
1607 }
1608
1609 async fn read_auth_message<S, T>(stream: &mut S) -> T
1610 where
1611 S: AsyncRead + Unpin,
1612 T: DeserializeOwned,
1613 {
1614 let mut length = [0; 4];
1615 stream
1616 .read_exact(&mut length)
1617 .await
1618 .expect("auth message length reads");
1619 let mut body = vec![0; u32::from_le_bytes(length) as usize];
1620 stream
1621 .read_exact(&mut body)
1622 .await
1623 .expect("auth message body reads");
1624 serde_json::from_slice(&body).expect("auth message decodes")
1625 }
1626
1627 async fn send_wrong_key_client_auth<S>(stream: &mut S, conn: &ConnectionInfo)
1628 where
1629 S: AsyncRead + AsyncWrite + Unpin,
1630 {
1631 let hello = ClientHello {
1632 client_nonce: [0x57; NONCE_LEN],
1633 role: "client".to_owned(),
1634 };
1635 write_auth_message(stream, &hello).await;
1636 let proof: ServerProof = read_auth_message(stream).await;
1637 let mut wrong_key = conn.key.clone();
1638 wrong_key[0] ^= 0xff;
1639 let client_auth = ClientAuth {
1640 client_auth: compute_proof(
1641 &wrong_key,
1642 CLIENT_AUTH_DOMAIN,
1643 &hello.client_nonce,
1644 &proof.server_nonce,
1645 &conn.daemon_id,
1646 ),
1647 };
1648 write_auth_message(stream, &client_auth).await;
1649 }
1650
1651 fn assert_pre_auth_log(logs: &Arc<std::sync::Mutex<Vec<u8>>>, stage: &str, port: u16) {
1652 let captured = crate::router::test_log::captured_logs(logs);
1653 assert!(
1654 captured.contains(&format!("stage=\"{stage}\"")),
1655 "expected rejection stage {stage}, got: {captured}"
1656 );
1657 assert!(
1658 captured.contains(&format!("peer_addr=127.0.0.1:{port}")),
1659 "expected peer address, got: {captured}"
1660 );
1661 assert!(
1662 captured.contains("elapsed_ms=") && captured.contains("suppressed_since_last="),
1663 "expected elapsed time and suppression count, got: {captured}"
1664 );
1665 assert!(
1666 !captured.contains("WARN"),
1667 "pre-auth rejection must not be logged as a warning: {captured}"
1668 );
1669 }
1670
1671 #[tokio::test]
1672 async fn logs_handshake_deadline_pre_auth_rejection() {
1673 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1674 let deadline = Duration::from_millis(60);
1675 let (auth, _) = test_auth_with_deadline_and_limit(deadline, 4);
1676 let (client, server_stream) = duplex(512);
1677 let port = 41001;
1678 let server = handle_accepted_connection(
1679 server_stream,
1680 echo_router(),
1681 auth,
1682 SocketAddr::from(([127, 0, 0, 1], port)),
1683 Instant::now(),
1684 );
1685
1686 let error = tokio::time::timeout(deadline * 3, server)
1687 .await
1688 .expect("idle handshake should end at its deadline")
1689 .expect_err("idle handshake must be rejected");
1690 drop(client);
1691 assert!(matches!(
1692 error,
1693 ConnectionError::Auth(AuthError::Timeout {
1694 stage: AuthStage::ClientHello,
1695 ..
1696 })
1697 ));
1698 let captured = crate::router::test_log::captured_logs(&logs);
1699 assert!(
1700 captured.contains("reason=\"handshake_deadline_expired\""),
1701 "{captured}"
1702 );
1703 assert_pre_auth_log(&logs, "handshake_deadline", port);
1704 }
1705
1706 #[tokio::test]
1707 async fn logs_invalid_client_proof_pre_auth_rejection() {
1708 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1709 let (auth, conn) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
1710 let (mut client, server_stream) = duplex(512);
1711 let port = 41002;
1712 let server = handle_accepted_connection(
1713 server_stream,
1714 echo_router(),
1715 auth,
1716 SocketAddr::from(([127, 0, 0, 1], port)),
1717 Instant::now(),
1718 );
1719
1720 let (error, ()) = tokio::join!(server, send_wrong_key_client_auth(&mut client, &conn));
1721 assert!(matches!(
1722 error,
1723 Err(ConnectionError::Auth(AuthError::InvalidClientAuth))
1724 ));
1725 let captured = crate::router::test_log::captured_logs(&logs);
1726 assert!(
1727 captured.contains("reason=\"client_proof_mismatch\""),
1728 "{captured}"
1729 );
1730 assert_pre_auth_log(&logs, "client_authentication", port);
1731 }
1732
1733 #[tokio::test]
1734 async fn logs_malformed_pre_auth_message() {
1735 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1736 let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
1737 let (mut client, server_stream) = duplex(512);
1738 let port = 41003;
1739 let server = handle_accepted_connection(
1740 server_stream,
1741 echo_router(),
1742 auth,
1743 SocketAddr::from(([127, 0, 0, 1], port)),
1744 Instant::now(),
1745 );
1746 let send_malformed_message = async {
1747 let body = b"{";
1748 client
1749 .write_all(&(body.len() as u32).to_le_bytes())
1750 .await
1751 .expect("message length writes");
1752 client.write_all(body).await.expect("message body writes");
1753 };
1754
1755 let (error, ()) = tokio::join!(server, send_malformed_message);
1756 assert!(matches!(
1757 error,
1758 Err(ConnectionError::Auth(AuthError::JsonDecode {
1759 stage: AuthStage::ClientHello,
1760 ..
1761 }))
1762 ));
1763 let captured = crate::router::test_log::captured_logs(&logs);
1764 assert!(
1765 captured.contains("reason=\"malformed_or_oversized_message\""),
1766 "{captured}"
1767 );
1768 assert_pre_auth_log(&logs, "malformed_message", port);
1769 }
1770
1771 #[tokio::test]
1772 async fn logs_peer_close_during_handshake() {
1773 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1774 let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
1775 let (client, server_stream) = duplex(512);
1776 let port = 41004;
1777 drop(client);
1778
1779 let error = handle_accepted_connection(
1780 server_stream,
1781 echo_router(),
1782 auth,
1783 SocketAddr::from(([127, 0, 0, 1], port)),
1784 Instant::now(),
1785 )
1786 .await
1787 .expect_err("closed peer must be rejected");
1788 assert!(matches!(
1789 error,
1790 ConnectionError::Auth(AuthError::UnexpectedEof {
1791 stage: AuthStage::ClientHello,
1792 actual: 0,
1793 ..
1794 })
1795 ));
1796 let captured = crate::router::test_log::captured_logs(&logs);
1797 assert!(
1798 captured.contains("reason=\"peer_closed_before_authentication_completed\""),
1799 "{captured}"
1800 );
1801 assert_pre_auth_log(&logs, "peer_closed", port);
1802 }
1803
1804 #[tokio::test]
1808 async fn each_listener_rate_limits_its_own_rejection_logs() {
1809 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1810 for port in [41011, 41012] {
1811 let (auth, _) = test_auth_with_deadline_and_limit(Duration::from_millis(200), 4);
1812 let (client, server_stream) = duplex(512);
1813 drop(client);
1814 handle_accepted_connection(
1815 server_stream,
1816 echo_router(),
1817 auth,
1818 SocketAddr::from(([127, 0, 0, 1], port)),
1819 Instant::now(),
1820 )
1821 .await
1822 .expect_err("closed peer must be rejected");
1823 }
1824 let captured = crate::router::test_log::captured_logs(&logs);
1825 for port in [41011, 41012] {
1826 assert!(
1827 captured.contains(&format!("127.0.0.1:{port}")),
1828 "listener on port {port} logged nothing: {captured}"
1829 );
1830 }
1831 }
1832
1833 #[tokio::test]
1834 async fn logs_unauthenticated_capacity_rejection() {
1835 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1836 let deadline = Duration::from_millis(60);
1837 let (auth, _) = test_auth_with_deadline_and_limit(deadline, 1);
1838 let held_slot = auth
1839 .unauthenticated
1840 .clone()
1841 .try_acquire_owned()
1842 .expect("the only pre-auth slot is available");
1843 let (client, server_stream) = duplex(512);
1844 let port = 41005;
1845 let server = handle_accepted_connection(
1846 server_stream,
1847 echo_router(),
1848 auth,
1849 SocketAddr::from(([127, 0, 0, 1], port)),
1850 Instant::now(),
1851 );
1852
1853 let error = tokio::time::timeout(deadline * 3, server)
1854 .await
1855 .expect("queued connection should end at the capacity deadline")
1856 .expect_err("connection without a free slot must be rejected");
1857 drop(held_slot);
1858 drop(client);
1859 assert!(matches!(error, ConnectionError::UnauthenticatedCapacity));
1860 let captured = crate::router::test_log::captured_logs(&logs);
1861 assert!(
1862 captured.contains("reason=\"capacity_wait_timed_out\""),
1863 "{captured}"
1864 );
1865 assert_pre_auth_log(&logs, "waiting_for_handshake_slot", port);
1866 }
1867
1868 #[test]
1869 fn pre_auth_rejection_limiter_suppresses_bursts_and_reports_count() {
1870 let (logs, _guard) = crate::router::test_log::log_capture(tracing::Level::INFO);
1871 let limiter = PreAuthRejectLimiter::new();
1872 let capacity = ConnectionError::UnauthenticatedCapacity;
1873 let handshake_timeout = ConnectionError::Auth(AuthError::Timeout {
1874 stage: AuthStage::ClientHello,
1875 deadline: Duration::from_millis(200),
1876 });
1877 let peer_addr = SocketAddr::from(([127, 0, 0, 1], 41006));
1878
1879 log_pre_auth_rejection(
1880 &capacity,
1881 peer_addr,
1882 Duration::from_millis(1),
1883 Duration::ZERO,
1884 &limiter,
1885 );
1886 for _ in 0..4 {
1887 log_pre_auth_rejection(
1888 &capacity,
1889 peer_addr,
1890 Duration::from_millis(2),
1891 Duration::from_millis(100),
1892 &limiter,
1893 );
1894 }
1895 log_pre_auth_rejection(
1896 &handshake_timeout,
1897 peer_addr,
1898 Duration::from_millis(3),
1899 Duration::from_millis(100),
1900 &limiter,
1901 );
1902 log_pre_auth_rejection(
1903 &capacity,
1904 peer_addr,
1905 Duration::from_millis(4),
1906 Duration::from_secs(1),
1907 &limiter,
1908 );
1909
1910 let captured = crate::router::test_log::captured_logs(&logs);
1911 assert_eq!(
1912 captured
1913 .matches("subc TCP connection rejected before routing")
1914 .count(),
1915 3,
1916 "one line per stage in a burst, then a later capacity line: {captured}"
1917 );
1918 assert!(
1919 captured.contains("suppressed_since_last=4"),
1920 "later capacity line should report the four hidden rejects: {captured}"
1921 );
1922 assert!(
1923 captured.contains("stage=\"handshake_deadline\""),
1924 "{captured}"
1925 );
1926 }
1927
1928 #[tokio::test]
1929 async fn buffered_frame_reader_coalesces_reads_and_preserves_short_reads() {
1930 let frames = vec![
1931 request(7, 1, b"first"),
1932 request(9, 2, b"second"),
1933 request(7, 3, b"third"),
1934 request(9, 4, b"fourth"),
1935 ];
1936 let bytes = encode_frames(&frames);
1937
1938 let (mut direct, direct_reads) = CountingReader::new(bytes.clone(), None);
1939 assert_eq!(read_frames(&mut direct, frames.len()).await, frames);
1940 assert_eq!(direct_reads.load(Ordering::Relaxed), frames.len() * 3);
1941
1942 let (buffered_source, buffered_reads) = CountingReader::new(bytes, None);
1943 let mut buffered = BufReader::new(buffered_source);
1944 assert_eq!(read_frames(&mut buffered, frames.len()).await, frames);
1945 assert_eq!(buffered_reads.load(Ordering::Relaxed), 1);
1946
1947 let split_frame = request(7, 5, b"split-body");
1948 let split_bytes = encode_frames(std::slice::from_ref(&split_frame));
1949 let (split_source, split_reads) = CountingReader::new(split_bytes, Some(10));
1950 let mut split_reader = BufReader::new(split_source);
1951 assert_eq!(
1952 read_frame(&mut split_reader).await.unwrap(),
1953 Some(split_frame)
1954 );
1955 assert_eq!(split_reads.load(Ordering::Relaxed), 2);
1956 }
1957
1958 #[tokio::test]
1959 async fn interleaved_channels_on_one_stream_demux_byte_identically_after_auth() {
1960 let (mut client, server_stream) = duplex(4096);
1961 let (auth, conn) = test_auth();
1962 let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
1963 authenticate(&mut client, &conn).await;
1964 let frames = [
1965 request(7, 1, b"chan7-first\0opaque"),
1966 request(9, 2, b"chan9-middle-{json?}"),
1967 request(7, 3, b"chan7-second\xffbytes"),
1968 ];
1969
1970 for frame in &frames {
1971 crate::write_frame(&mut client, frame).await.unwrap();
1972 }
1973
1974 for expected in &frames {
1975 let response = crate::read_frame(&mut client).await.unwrap().unwrap();
1976 assert_eq!(response.header.ty, FrameType::Response);
1977 assert_eq!(response.header.channel, expected.header.channel);
1978 assert_eq!(response.header.corr, expected.header.corr);
1979 assert_eq!(response.body, expected.body);
1980 }
1981
1982 drop(client);
1983 server.await.unwrap().unwrap();
1984 }
1985
1986 #[tokio::test]
1987 async fn channel_zero_goes_to_subc_self_handler_after_auth() {
1988 let (mut client, server_stream) = duplex(512);
1989 let (auth, conn) = test_auth();
1990 let server = tokio::spawn(handle_connection(
1991 server_stream,
1992 Arc::new(Router::with_default_self_handler()),
1993 auth,
1994 ));
1995 authenticate(&mut client, &conn).await;
1996 let ping = Frame::build(
1997 FrameType::Ping,
1998 Flags::new(false, Priority::Passive, false),
1999 0,
2000 0,
2001 55,
2002 Vec::new(),
2003 )
2004 .unwrap();
2005
2006 crate::write_frame(&mut client, &ping).await.unwrap();
2007 let response = crate::read_frame(&mut client).await.unwrap().unwrap();
2008
2009 assert_eq!(response.header.ty, FrameType::Pong);
2010 assert_eq!(response.header.channel, 0);
2011 assert_eq!(response.header.corr, 55);
2012 assert!(response.body.is_empty());
2013
2014 drop(client);
2015 server.await.unwrap().unwrap();
2016 }
2017
2018 #[tokio::test]
2019 async fn unauthenticated_connection_is_rejected_before_routing() {
2020 let (mut client, server_stream) = duplex(512);
2021 let (auth, _conn) = test_auth();
2022 let registry = Arc::new(Registry::default());
2023 let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2024 Arc::clone(®istry),
2025 ))));
2026 let server = tokio::spawn(handle_connection(server_stream, router, auth));
2027 let ping = Frame::build(
2028 FrameType::Ping,
2029 Flags::new(false, Priority::Passive, false),
2030 0,
2031 0,
2032 66,
2033 Vec::new(),
2034 )
2035 .unwrap();
2036
2037 crate::write_frame(&mut client, &ping).await.unwrap();
2038 if let Ok(Ok(Some(frame))) =
2039 tokio::time::timeout(Duration::from_millis(200), crate::read_frame(&mut client)).await
2040 {
2041 panic!("unauthenticated frame reached router: {frame:?}");
2042 }
2043
2044 let err = server.await.unwrap().unwrap_err();
2045 assert!(matches!(err, ConnectionError::Auth(_)));
2046 assert_eq!(registry.active_registration_count().unwrap(), 0);
2047 }
2048
2049 #[tokio::test]
2050 async fn over_cap_peer_queues_for_a_slot_and_authenticates_when_one_frees() {
2051 let (mut first_client, first_server_stream) = duplex(2048);
2056 let (mut second_client, second_server_stream) = duplex(2048);
2057 let (auth, conn) = test_auth_with_limit(1);
2058 let registry = Arc::new(Registry::default());
2059 let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2060 Arc::clone(®istry),
2061 ))));
2062
2063 let first_server = tokio::spawn(handle_connection(
2064 first_server_stream,
2065 Arc::clone(&router),
2066 auth.clone(),
2067 ));
2068
2069 let second_server = tokio::spawn(handle_connection(
2070 second_server_stream,
2071 Arc::clone(&router),
2072 auth.clone(),
2073 ));
2074
2075 tokio::time::sleep(Duration::from_millis(50)).await;
2077 assert!(
2078 !second_server.is_finished(),
2079 "queued peer must not be reset"
2080 );
2081
2082 authenticate(&mut first_client, &conn).await;
2085 authenticate(&mut second_client, &conn).await;
2086
2087 drop(first_client);
2088 drop(second_client);
2089 let _ = first_server.await;
2090 let _ = second_server.await;
2091 assert_eq!(registry.active_registration_count().unwrap(), 0);
2092 }
2093
2094 #[tokio::test]
2095 async fn over_cap_peer_is_rejected_when_no_slot_frees_within_deadline() {
2096 let (mut second_client, second_server_stream) = duplex(512);
2102 let (auth, conn) = test_auth_with_limit(1);
2103 let registry = Arc::new(Registry::default());
2104 let router = Arc::new(Router::with_control_handler(Arc::new(ControlHandler::new(
2105 Arc::clone(®istry),
2106 ))));
2107
2108 let held_slot = auth
2109 .unauthenticated
2110 .clone()
2111 .try_acquire_owned()
2112 .expect("sole pre-auth slot");
2113
2114 let second_server = tokio::spawn(handle_connection(
2115 second_server_stream,
2116 Arc::clone(&router),
2117 auth.clone(),
2118 ));
2119 let second_err = tokio::time::timeout(TEST_DEADLINE * 2, second_server)
2120 .await
2121 .expect("capacity reject should settle at the deadline")
2122 .expect("second connection task should not panic")
2123 .expect_err("queued peer must be rejected when no slot frees");
2124 drop(held_slot);
2125 assert!(matches!(
2126 second_err,
2127 ConnectionError::UnauthenticatedCapacity
2128 ));
2129 let mut closed = [0u8; 1];
2130 assert_eq!(
2131 second_client.read(&mut closed).await.unwrap(),
2132 0,
2133 "capacity-rejected peer should observe a closed stream"
2134 );
2135 assert_eq!(registry.active_registration_count().unwrap(), 0);
2136
2137 let (mut authed_client, authed_server_stream) = duplex(2048);
2138 let authed_server = tokio::spawn(handle_connection(
2139 authed_server_stream,
2140 Arc::clone(&router),
2141 auth,
2142 ));
2143 authenticate(&mut authed_client, &conn).await;
2144 let ping = Frame::build(
2145 FrameType::Ping,
2146 Flags::new(false, Priority::Passive, false),
2147 0,
2148 0,
2149 77,
2150 Vec::new(),
2151 )
2152 .unwrap();
2153 crate::write_frame(&mut authed_client, &ping).await.unwrap();
2154 let pong = crate::read_frame(&mut authed_client)
2155 .await
2156 .unwrap()
2157 .unwrap();
2158 assert_eq!(pong.header.ty, FrameType::Pong);
2159 assert_eq!(pong.header.channel, 0);
2160 assert_eq!(pong.header.corr, 77);
2161 assert_eq!(registry.active_registration_count().unwrap(), 0);
2162
2163 drop(authed_client);
2164 authed_server.await.unwrap().unwrap();
2165 }
2166
2167 #[tokio::test]
2168 async fn bind_ack_during_partial_frame_preserves_next_request() {
2169 use subc_control::ClientControlRequest;
2170 use subc_protocol::{
2171 manifest::{
2172 Concurrency, ExecutionMode, IdentityScope, ModuleManifest, ProviderRole, Tool,
2173 },
2174 session::{ModuleControlRequest, ModuleControlResponse},
2175 BindIdentity, ModuleHelloBody, RouteTarget,
2176 };
2177
2178 let mut configured_router = Router::with_default_self_handler();
2179 configured_router.register_backend(7, EchoBackend).unwrap();
2180 let router = Arc::new(configured_router);
2181 let (auth, conn) = test_auth();
2182 let (mut module, module_stream) = duplex(4096);
2183 let module_server = tokio::spawn(handle_connection(
2184 module_stream,
2185 Arc::clone(&router),
2186 auth.clone(),
2187 ));
2188 authenticate(&mut module, &conn).await;
2189 let manifest = ModuleManifest::builder("frame-test", "0.1.0")
2190 .protocol_ver(PROTOCOL_VERSION)
2191 .provides(vec![ProviderRole::ToolProvider {
2192 tools: vec![Tool {
2193 name: "read".into(),
2194 description: None,
2195 execution_mode: ExecutionMode::Pure,
2196 schema: serde_json::json!({"type": "object"}),
2197 }],
2198 identity_scope: vec![IdentityScope::Project, IdentityScope::Session],
2199 concurrency: Concurrency::ModuleManaged,
2200 emits_push: true,
2201 sub_supervises: true,
2202 }])
2203 .build();
2204 let hello = Frame::build(
2205 FrameType::Hello,
2206 Flags::new(false, Priority::Passive, false),
2207 0,
2208 0,
2209 1,
2210 serde_json::to_vec(&ModuleHelloBody {
2211 manifest,
2212 protocol_ver: PROTOCOL_VERSION,
2213 control_ops: None,
2214 launch_nonce: None,
2215 })
2216 .unwrap(),
2217 )
2218 .unwrap();
2219 crate::write_frame(&mut module, &hello).await.unwrap();
2220 assert_eq!(
2221 read_frame(&mut module).await.unwrap().unwrap().header.ty,
2222 FrameType::HelloAck
2223 );
2224
2225 let (mut client, client_stream) = duplex(4096);
2226 let client_server = tokio::spawn(handle_connection(client_stream, router, auth));
2227 authenticate(&mut client, &conn).await;
2228 let open = Frame::build(
2229 FrameType::Request,
2230 Flags::new(false, Priority::Passive, false),
2231 0,
2232 0,
2233 2,
2234 serde_json::to_vec(&ClientControlRequest::RouteOpen {
2235 target: RouteTarget::ToolProvider {
2236 module_id: "frame-test".into(),
2237 },
2238 identity: BindIdentity::new(std::env::current_dir().unwrap(), "unit", "session"),
2239 consumer_identity: None,
2240 consumer_capabilities: None,
2241 role_versions: None,
2242 admission_facts: None,
2243 scope: None,
2244 })
2245 .unwrap(),
2246 )
2247 .unwrap();
2248 crate::write_frame(&mut client, &open).await.unwrap();
2249 let bind = timeout(TEST_DEADLINE, read_frame(&mut module))
2250 .await
2251 .unwrap()
2252 .unwrap()
2253 .unwrap();
2254 assert!(matches!(
2255 serde_json::from_slice::<ModuleControlRequest>(&bind.body).unwrap(),
2256 ModuleControlRequest::RouteBind { .. }
2257 ));
2258
2259 let ping = request(7, 3, b"partial-frame-body");
2260 client.write_all(&ping.header.encode()).await.unwrap();
2261 tokio::time::sleep(Duration::from_millis(30)).await;
2263 let ack = Frame::build(
2264 FrameType::Response,
2265 Flags::new(false, Priority::Passive, false),
2266 0,
2267 0,
2268 bind.header.corr,
2269 serde_json::to_vec(&ModuleControlResponse::RouteBindAck {}).unwrap(),
2270 )
2271 .unwrap();
2272 crate::write_frame(&mut module, &ack).await.unwrap();
2273 let opened = timeout(TEST_DEADLINE, read_frame(&mut client))
2274 .await
2275 .unwrap()
2276 .unwrap();
2277 if opened.is_none() {
2278 panic!("client closed: {:?}", client_server.await);
2279 }
2280 let opened = opened.unwrap();
2281 assert_eq!(opened.header.corr, 2);
2282 client.write_all(&ping.body).await.unwrap();
2283 let pong = timeout(TEST_DEADLINE, read_frame(&mut client))
2284 .await
2285 .expect("partial frame must reach the router after the bind ack")
2286 .expect("frame must decode")
2287 .expect("connection must remain open");
2288 assert_eq!(pong.header.ty, FrameType::Response);
2289 assert_eq!(pong.header.corr, 3);
2290 assert_eq!(pong.body, ping.body);
2291 drop(client);
2292 drop(module);
2293 client_server.await.unwrap().unwrap();
2294 let _ = module_server.await.unwrap();
2295 }
2296
2297 #[tokio::test]
2298 async fn full_module_egress_health_probe_does_not_block_same_client_other_route() {
2299 use crate::ConnectionId;
2300 use subc_control::{ClientControlRequest, ClientControlResponse};
2301 use subc_protocol::{
2302 manifest::{
2303 Concurrency, ExecutionMode, IdentityScope, ModuleManifest, ProviderRole, Tool,
2304 },
2305 session::ModuleControlResponse,
2306 BindIdentity, ModuleHelloBody, RouteTarget,
2307 };
2308
2309 let probe_timeout = Duration::from_millis(100);
2310 let handler = Arc::new(
2311 ControlHandler::new(Arc::new(Registry::default()))
2312 .with_health_probe_timeout(probe_timeout),
2313 );
2314 let router = Arc::new(Router::with_control_handler(Arc::clone(&handler)));
2315 let hello = |name: &str| {
2316 Frame::build(
2317 FrameType::Hello,
2318 Flags::new(false, Priority::Passive, false),
2319 0,
2320 0,
2321 1,
2322 serde_json::to_vec(&ModuleHelloBody {
2323 manifest: ModuleManifest::builder(name, "0.1.0")
2324 .protocol_ver(PROTOCOL_VERSION)
2325 .provides(vec![ProviderRole::ToolProvider {
2326 tools: vec![Tool {
2327 name: "read".into(),
2328 description: None,
2329 execution_mode: ExecutionMode::Pure,
2330 schema: serde_json::json!({"type": "object"}),
2331 }],
2332 identity_scope: vec![IdentityScope::Project, IdentityScope::Session],
2333 concurrency: Concurrency::ModuleManaged,
2334 emits_push: false,
2335 sub_supervises: false,
2336 }])
2337 .build(),
2338 protocol_ver: PROTOCOL_VERSION,
2339 control_ops: Some(vec!["health.check".to_owned()]),
2340 launch_nonce: None,
2341 })
2342 .unwrap(),
2343 )
2344 .unwrap()
2345 };
2346 let (blocked_tx, mut blocked_rx) = mpsc::channel(1);
2347 let blocked_ctx = RouteCtx {
2348 connection_id: ConnectionId::new(90001),
2349 egress: FrameSink::new(blocked_tx),
2350 };
2351 handler
2352 .handle_control_frame(&blocked_ctx, hello("blocked"))
2353 .await
2354 .unwrap();
2355 assert_eq!(
2356 blocked_rx.try_recv().unwrap().header.ty,
2357 FrameType::HelloAck
2358 );
2359 let filler = request(0, 99, b"queue filler");
2360 blocked_ctx.egress.try_send(filler.clone()).unwrap();
2361 assert!(
2362 blocked_ctx.egress.try_send(filler).is_err(),
2363 "module queue must actually be full"
2364 );
2365
2366 let (healthy_tx, mut healthy_rx) = mpsc::channel(8);
2367 let healthy_ctx = RouteCtx {
2368 connection_id: ConnectionId::new(90002),
2369 egress: FrameSink::new(healthy_tx),
2370 };
2371 handler
2372 .handle_control_frame(&healthy_ctx, hello("healthy"))
2373 .await
2374 .unwrap();
2375 assert_eq!(
2376 healthy_rx.try_recv().unwrap().header.ty,
2377 FrameType::HelloAck
2378 );
2379 let (auth, conn) = test_auth();
2380 let (mut client, client_stream) = duplex(4096);
2381 let client_server =
2382 tokio::spawn(handle_connection(client_stream, Arc::clone(&router), auth));
2383 authenticate(&mut client, &conn).await;
2384 let control_request = |corr, body: ClientControlRequest| {
2385 Frame::build(
2386 FrameType::Request,
2387 Flags::new(false, Priority::Interactive, false),
2388 0,
2389 0,
2390 corr,
2391 serde_json::to_vec(&body).unwrap(),
2392 )
2393 .unwrap()
2394 };
2395 crate::write_frame(
2396 &mut client,
2397 &control_request(
2398 2,
2399 ClientControlRequest::RouteOpen {
2400 target: RouteTarget::ToolProvider {
2401 module_id: "healthy".into(),
2402 },
2403 identity: BindIdentity::new(
2404 std::env::current_dir().unwrap(),
2405 "unit",
2406 "session",
2407 ),
2408 consumer_identity: None,
2409 consumer_capabilities: None,
2410 role_versions: None,
2411 admission_facts: None,
2412 scope: None,
2413 },
2414 ),
2415 )
2416 .await
2417 .unwrap();
2418 let bind = timeout(TEST_DEADLINE, healthy_rx.recv())
2419 .await
2420 .unwrap()
2421 .unwrap();
2422 handler
2423 .handle_control_frame(
2424 &healthy_ctx,
2425 Frame::build(
2426 FrameType::Response,
2427 Flags::new(false, Priority::Passive, false),
2428 0,
2429 0,
2430 bind.header.corr,
2431 serde_json::to_vec(&ModuleControlResponse::RouteBindAck {}).unwrap(),
2432 )
2433 .unwrap(),
2434 )
2435 .await
2436 .unwrap();
2437 let opened = timeout(TEST_DEADLINE, read_frame(&mut client))
2438 .await
2439 .unwrap()
2440 .unwrap()
2441 .unwrap();
2442 let (channel, epoch) =
2443 match serde_json::from_slice::<ClientControlResponse>(&opened.body).unwrap() {
2444 ClientControlResponse::RouteOpen {
2445 route_channel,
2446 route_epoch,
2447 } => (route_channel, route_epoch),
2448 other => panic!("expected a healthy module route, got {other:?}"),
2449 };
2450
2451 let probe = control_request(
2452 3,
2453 ClientControlRequest::SupervisorHealthProbe {
2454 module_id: "blocked".into(),
2455 },
2456 );
2457 let mut ping = request(channel, 4, b"healthy request");
2458 ping.header.epoch = epoch;
2459 crate::write_frame(&mut client, &probe).await.unwrap();
2460 crate::write_frame(&mut client, &ping).await.unwrap();
2461 timeout(probe_timeout + Duration::from_secs(1), async {
2462 let timed_out = read_frame(&mut client).await.unwrap().unwrap();
2463 assert_eq!(timed_out.header.corr, 3);
2464 assert_eq!(timed_out.header.ty, FrameType::Error);
2465 let error: ErrorBody = serde_json::from_slice(&timed_out.body).unwrap();
2466 assert_eq!(error.code, "module_timeout");
2467 let forwarded = healthy_rx.recv().await.unwrap();
2468 assert_eq!(forwarded.header.corr, ping.header.corr);
2469 assert_eq!(forwarded.body, ping.body);
2470 let reply = Frame::build(
2471 FrameType::Response,
2472 ping.header.flags,
2473 forwarded.header.channel,
2474 forwarded.header.epoch,
2475 forwarded.header.corr,
2476 b"healthy response".to_vec(),
2477 )
2478 .unwrap();
2479 router
2480 .route_for_connection(&healthy_ctx, reply)
2481 .await
2482 .unwrap();
2483 let pong = read_frame(&mut client).await.unwrap().unwrap();
2484 assert_eq!(pong.header.ty, FrameType::Response);
2485 assert_eq!(pong.header.channel, channel);
2486 assert_eq!(pong.header.epoch, epoch);
2487 assert_eq!(pong.header.corr, 4);
2488 assert_eq!(pong.body, b"healthy response");
2489 })
2490 .await
2491 .expect(
2492 "the same client's healthy route must answer within the probe deadline plus margin",
2493 );
2494 assert_eq!(
2495 blocked_rx.len(),
2496 1,
2497 "the blocked module must remain undrained"
2498 );
2499 assert_eq!(blocked_rx.try_recv().unwrap().header.corr, 99);
2500 assert!(
2501 blocked_rx.try_recv().is_err(),
2502 "a timed-out probe must not be enqueued later"
2503 );
2504 drop(client);
2505 client_server.await.unwrap().unwrap();
2506 }
2507
2508 #[tokio::test]
2509 async fn aborted_accept_does_not_end_listener() {
2510 let listener = Arc::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
2511 let addr = listener.local_addr().unwrap();
2512 let (auth, conn) = test_auth();
2513 let mut attempts = 0;
2514 let server = tokio::spawn(serve_listener_with_accept(
2515 Some(addr),
2516 echo_router(),
2517 auth,
2518 move || {
2519 attempts += 1;
2520 let result = if attempts == 1 {
2521 Some(io::Error::from(io::ErrorKind::ConnectionAborted))
2522 } else {
2523 None
2524 };
2525 let listener = Arc::clone(&listener);
2526 async move {
2527 match result {
2528 Some(err) => Err(err),
2529 None => listener.accept().await,
2530 }
2531 }
2532 },
2533 ));
2534 let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
2535 authenticate(&mut client, &conn).await;
2536 let ping = Frame::build(
2537 FrameType::Ping,
2538 Flags::new(false, Priority::Passive, false),
2539 0,
2540 0,
2541 77,
2542 Vec::new(),
2543 )
2544 .unwrap();
2545 crate::write_frame(&mut client, &ping).await.unwrap();
2546 let pong = timeout(TEST_DEADLINE, read_frame(&mut client))
2547 .await
2548 .unwrap()
2549 .unwrap()
2550 .unwrap();
2551 assert_eq!(pong.header.ty, FrameType::Pong);
2552 assert!(
2553 !server.is_finished(),
2554 "a temporary accept failure must not stop the listener"
2555 );
2556 server.abort();
2557 }
2558
2559 #[tokio::test]
2560 async fn exhausted_accept_backs_off_then_serves_connection() {
2561 let listener = Arc::new(TcpListener::bind("127.0.0.1:0").await.unwrap());
2562 let addr = listener.local_addr().unwrap();
2563 let (auth, conn) = test_auth();
2564 let mut attempts = 0;
2565 let server = tokio::spawn(serve_listener_with_accept(
2566 Some(addr),
2567 echo_router(),
2568 auth,
2569 move || {
2570 attempts += 1;
2571 #[cfg(unix)]
2574 let emfile = rustix::io::Errno::MFILE.raw_os_error();
2575 #[cfg(windows)]
2576 let emfile = 10024;
2577 let error = (attempts == 1).then(|| io::Error::from_raw_os_error(emfile));
2578 let listener = Arc::clone(&listener);
2579 async move {
2580 match error {
2581 Some(err) => Err(err),
2582 None => listener.accept().await,
2583 }
2584 }
2585 },
2586 ));
2587 let started = Instant::now();
2588 let mut client = tokio::net::TcpStream::connect(addr).await.unwrap();
2589 authenticate(&mut client, &conn).await;
2590 assert!(
2591 started.elapsed() >= Duration::from_millis(50),
2592 "fd exhaustion must back off before retrying"
2593 );
2594 assert!(!server.is_finished());
2595 server.abort();
2596 }
2597
2598 #[tokio::test]
2599 async fn fatal_accept_error_still_stops_listener() {
2600 let (auth, _) = test_auth();
2601 let err = serve_listener_with_accept(None, echo_router(), auth, || async {
2602 Err(io::Error::from(io::ErrorKind::PermissionDenied))
2603 })
2604 .await
2605 .unwrap_err();
2606 assert!(
2607 matches!(err, ServerError::Accept { source, .. } if source.kind() == io::ErrorKind::PermissionDenied)
2608 );
2609 }
2610
2611 #[tokio::test]
2612 async fn serve_listeners_with_no_listeners_returns_typed_error() {
2613 let (auth, _conn) = test_auth();
2614 let err = serve_listeners(Vec::new(), echo_router(), auth)
2615 .await
2616 .expect_err("empty listener set must fail loudly");
2617 assert!(matches!(err, ServerError::NoListeners));
2618 }
2619
2620 #[tokio::test]
2621 async fn malformed_header_returns_typed_error_no_panic() {
2622 let (mut client, server_stream) = duplex(128);
2623 let (auth, conn) = test_auth();
2624 let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2625 authenticate(&mut client, &conn).await;
2626 let mut header = [0u8; HEADER_LEN];
2627 header[4] = PROTOCOL_VERSION;
2628 header[5] = 250;
2629
2630 client.write_all(&header).await.unwrap();
2631 drop(client);
2632
2633 let err = server.await.unwrap().unwrap_err();
2634 assert!(matches!(
2635 err,
2636 ConnectionError::FrameIo(FrameIoError::DecodeHeader(DecodeError::UnknownFrameType {
2637 byte: 250
2638 }))
2639 ));
2640 }
2641
2642 #[tokio::test]
2643 async fn truncated_body_returns_typed_error_no_panic() {
2644 let (mut client, server_stream) = duplex(128);
2645 let (auth, conn) = test_auth();
2646 let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2647 authenticate(&mut client, &conn).await;
2648 let frame = request(7, 8, b"abcd");
2649
2650 client.write_all(&frame.header.encode()).await.unwrap();
2651 client.write_all(b"ab").await.unwrap();
2652 drop(client);
2653
2654 let err = server.await.unwrap().unwrap_err();
2655 assert!(matches!(
2656 err,
2657 ConnectionError::FrameIo(FrameIoError::UnexpectedEof {
2658 stage: ReadStage::Body,
2659 expected: 4,
2660 actual: 2
2661 })
2662 ));
2663 }
2664
2665 #[tokio::test]
2666 async fn unknown_channel_is_returned_as_error_frame_and_connection_continues() {
2667 let (mut client, server_stream) = duplex(1024);
2668 let (auth, conn) = test_auth();
2669 let server = tokio::spawn(handle_connection(server_stream, echo_router(), auth));
2670 authenticate(&mut client, &conn).await;
2671 let unknown = request(42, 10, b"lost");
2672 let known = request(7, 11, b"still-routes");
2673
2674 crate::write_frame(&mut client, &unknown).await.unwrap();
2675 crate::write_frame(&mut client, &known).await.unwrap();
2676
2677 let error = crate::read_frame(&mut client).await.unwrap().unwrap();
2678 assert_eq!(error.header.ty, FrameType::Error);
2679 assert_eq!(error.header.channel, 42);
2680 assert_eq!(error.header.corr, 10);
2681 let error_body: ErrorBody = serde_json::from_slice(&error.body).unwrap();
2682 assert_eq!(error_body.code, "unknown_channel");
2683
2684 let response = crate::read_frame(&mut client).await.unwrap().unwrap();
2685 assert_eq!(response.header.ty, FrameType::Response);
2686 assert_eq!(response.header.channel, 7);
2687 assert_eq!(response.header.corr, 11);
2688 assert_eq!(response.body, b"still-routes");
2689
2690 drop(client);
2691 server.await.unwrap().unwrap();
2692 }
2693}