11//! Drives the simulated population through the scenario's ramp.
22
33use std:: collections:: HashMap ;
4+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
45use std:: sync:: Arc ;
56use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
67
@@ -64,6 +65,8 @@ struct Shared {
6465 stop : watch:: Receiver < bool > ,
6566 /// Seeded thread roots per channel id, resolved lazily by whichever client needs them first.
6667 roots : Mutex < HashMap < String , Arc < Vec < String > > > > ,
68+ in_flight_connects : AtomicUsize ,
69+ established_clients : AtomicUsize ,
6770}
6871
6972/// Runs every ramp step and returns the JSON report; `Err` when a step aborted.
@@ -77,6 +80,8 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
7780 stats : stats. clone ( ) ,
7881 stop,
7982 roots : Mutex :: new ( HashMap :: new ( ) ) ,
83+ in_flight_connects : AtomicUsize :: new ( 0 ) ,
84+ established_clients : AtomicUsize :: new ( 0 ) ,
8085 } ) ;
8186 let scenario = & shared. scenario ;
8287 let pop = & scenario. population ;
@@ -128,12 +133,16 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
128133 let lost = tasks. iter ( ) . filter ( |t| t. is_finished ( ) ) . count ( ) ;
129134 let reason = if lost > 0 {
130135 Some ( format ! ( "{lost} clients exited unexpectedly" ) )
136+ } else if let Some ( reason) = abort_reason (
137+ & summaries,
138+ scenario. abort . max_error_rate ,
139+ scenario. abort . max_p99_ms ,
140+ ) {
141+ Some ( reason)
142+ } else if !tasks. is_empty ( ) && shared. established_clients . load ( Ordering :: SeqCst ) == 0 {
143+ Some ( "zero clients connected" . into ( ) )
131144 } else {
132- abort_reason (
133- & summaries,
134- scenario. abort . max_error_rate ,
135- scenario. abort . max_p99_ms ,
136- )
145+ None
137146 } ;
138147 let report = json ! ( {
139148 "step" : n,
@@ -168,7 +177,7 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
168177 "clients_panicked" : panicked,
169178 "clients_cancelled" : cancelled,
170179 } ) ;
171- if aborted || panicked > 0 {
180+ if aborted || panicked > 0 || cancelled > 0 {
172181 Err ( report)
173182 } else {
174183 Ok ( report)
@@ -378,13 +387,22 @@ async fn connect(
378387) -> Option < NostrWsConnection > {
379388 while !stopped ( shared) {
380389 let t0 = Instant :: now ( ) ;
390+ shared. in_flight_connects . fetch_add ( 1 , Ordering :: SeqCst ) ;
381391 let attempt = tokio:: select! {
382- attempt = shared. net. connect( community, keys, auth) => attempt,
383- _ = stopping( shared) => return None ,
392+ attempt = shared. net. connect( community, keys, auth) => {
393+ shared. in_flight_connects. fetch_sub( 1 , Ordering :: SeqCst ) ;
394+ attempt
395+ }
396+ _ = stopping( shared) => {
397+ shared. in_flight_connects. fetch_sub( 1 , Ordering :: SeqCst ) ;
398+ shared. stats. err( Op :: WsConnect , "connection attempt abandoned on stop" ) ;
399+ return None ;
400+ }
384401 } ;
385402 match attempt {
386403 Ok ( conn) => {
387404 shared. stats . ok ( Op :: WsConnect , t0. elapsed ( ) ) ;
405+ shared. established_clients . fetch_add ( 1 , Ordering :: SeqCst ) ;
388406 return Some ( conn) ;
389407 }
390408 Err ( e) => shared. stats . err ( Op :: WsConnect , & e. to_string ( ) ) ,
@@ -587,7 +605,9 @@ async fn human(shared: Arc<Shared>, global: usize) {
587605 if rng. unit ( ) < profile. thread_reply_fraction {
588606 let roots = roots ( & shared, community, & keys, channel) . await ;
589607 if !roots. is_empty ( ) {
590- tags. push ( tag ( [ "e" , & roots[ rng. below ( roots. len ( ) ) ] , "" , "reply" ] ) ) ;
608+ let root = & roots[ rng. below ( roots. len ( ) ) ] ;
609+ tags. push ( tag ( [ "e" , root, "" , "root" ] ) ) ;
610+ tags. push ( tag ( [ "e" , root, "" , "reply" ] ) ) ;
591611 }
592612 }
593613 let event = EventBuilder :: new (
@@ -1074,4 +1094,57 @@ hold_secs = 1
10741094 assert_eq ! ( report[ "clients_cancelled" ] , 0 ) ;
10751095 held. abort ( ) ;
10761096 }
1097+
1098+ #[ tokio:: test]
1099+ async fn stalled_connects_with_long_timeout_aborts_run ( ) {
1100+ // A peer that accepts TCP but never answers the WebSocket upgrade.
1101+ // When connect_timeout_secs > hold_secs, the step must still abort because 0 clients connected.
1102+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
1103+ let addr = listener. local_addr ( ) . unwrap ( ) ;
1104+ let held = tokio:: spawn ( async move {
1105+ let mut open = Vec :: new ( ) ;
1106+ while let Ok ( ( socket, _) ) = listener. accept ( ) . await {
1107+ open. push ( socket) ;
1108+ }
1109+ } ) ;
1110+ let scenario = Scenario :: parse ( & format ! (
1111+ r#"
1112+ run_id = "t"
1113+ [target]
1114+ ws_url = "ws://{addr}"
1115+ http_url = "http://{addr}"
1116+ connect_timeout_secs = 5.0
1117+ [shape]
1118+ communities = 1
1119+ channels_per_community = 1
1120+ [population]
1121+ humans = 2
1122+ agents = 0
1123+ channels_per_member = 1
1124+ connect_rate_per_sec = 100.0
1125+ [[ramp]]
1126+ users = 1.0
1127+ hold_secs = 1
1128+ "#
1129+ ) )
1130+ . unwrap ( ) ;
1131+ let net = Net :: new ( & scenario) . await . unwrap ( ) ;
1132+ let started = Instant :: now ( ) ;
1133+ let report = tokio:: time:: timeout (
1134+ Duration :: from_secs ( 10 ) ,
1135+ run ( scenario, net, Shard { index : 0 , count : 1 } ) ,
1136+ )
1137+ . await
1138+ . expect ( "run returns when step aborts on 0 connected clients" )
1139+ . expect_err ( "a step whose clients fail to connect must abort" ) ;
1140+ assert ! ( started. elapsed( ) < Duration :: from_secs( 5 ) ) ;
1141+ let steps = report[ "steps" ] . as_array ( ) . unwrap ( ) ;
1142+ assert_eq ! ( steps. len( ) , 1 ) ;
1143+ assert_eq ! ( steps[ 0 ] [ "aborted" ] , true ) ;
1144+ assert_eq ! (
1145+ steps[ 0 ] [ "abort_reason" ] . as_str( ) . unwrap( ) ,
1146+ "zero clients connected"
1147+ ) ;
1148+ held. abort ( ) ;
1149+ }
10771150}
0 commit comments