66use std:: sync:: Arc ;
77use std:: time:: Duration ;
88
9- use agent_tunnel_proto:: { ConnectResponse , ControlMessage , ControlRecvStream , ControlStream , SessionStream } ;
9+ use agent_tunnel_proto:: {
10+ ConnectResponse , ControlMessage , ControlRecvStream , ControlStream , SessionStream , current_time_millis,
11+ } ;
1012use anyhow:: { Context as _, bail} ;
1113use async_trait:: async_trait;
1214use devolutions_gateway_task:: { ShutdownSignal , Task } ;
1315use ipnetwork:: Ipv4Network ;
1416use sha2:: Digest as _;
1517
1618use crate :: config:: ConfHandle ;
17- use crate :: tunnel_helpers:: { Target , connect_to_target, current_time_millis , resolve_target} ;
19+ use crate :: tunnel_helpers:: { Target , connect_to_target, resolve_target} ;
1820
1921// ---------------------------------------------------------------------------
2022// Custom TLS verifier: chain + hostname validation + SPKI pinning
@@ -349,8 +351,8 @@ async fn run_single_connection(conf_handle: &ConfHandle, shutdown_signal: &mut S
349351
350352 // Split: recv half goes to a reader task, send half stays for periodic messages.
351353 let ( mut ctrl_send, ctrl_recv) = ctrl. into_split ( ) ;
352- let mut task_handles: Vec < tokio:: task:: JoinHandle < ( ) > > = Vec :: new ( ) ;
353- task_handles. push ( tokio :: spawn ( run_control_reader ( ctrl_recv) ) ) ;
354+ let mut task_handles = tokio:: task:: JoinSet :: new ( ) ;
355+ task_handles. spawn ( run_control_reader ( ctrl_recv) ) ;
354356
355357 // -- Main loop: accept incoming session streams + periodic tasks --
356358
@@ -380,6 +382,7 @@ async fn run_single_connection(conf_handle: &ConfHandle, shutdown_signal: &mut S
380382 }
381383
382384 _ = heartbeat_tick. tick( ) => {
385+ // TODO: track actual active_stream_count instead of hardcoded 0.
383386 let msg = ControlMessage :: heartbeat( current_time_millis( ) , 0 ) ;
384387 let _ = ctrl_send. send( & msg) . await
385388 . inspect( |_| trace!( "Sent Heartbeat" ) )
@@ -389,18 +392,15 @@ async fn run_single_connection(conf_handle: &ConfHandle, shutdown_signal: &mut S
389392 result = connection. accept_bi( ) => {
390393 let ( send, recv) = result. context( "accept incoming bidi stream" ) ?;
391394 let subnets = advertise_subnets. clone( ) ;
392- task_handles. push ( tokio :: spawn( run_session_proxy( subnets, send, recv) ) ) ;
395+ task_handles. spawn( run_session_proxy( subnets, send, recv) ) ;
393396 }
397+
398+ // Reap completed session tasks.
399+ Some ( _) = task_handles. join_next( ) => { }
394400 }
395401 }
396402
397- // Abort all spawned tasks on shutdown.
398- for handle in & task_handles {
399- handle. abort ( ) ;
400- }
401- for handle in task_handles {
402- let _ = handle. await ;
403- }
403+ task_handles. shutdown ( ) . await ;
404404
405405 Ok ( ( ) )
406406}
0 commit comments