11//! Recover transport affinity, never the response chain, after an interrupted turn.
22use super :: * ;
33
4+ // This is background recovery, not the latency of acknowledging Escape. A
5+ // subsecond deadline discards healthy sockets before ordinary generations can
6+ // finish, losing cache affinity on the next turn. Bound the wait for a successor
7+ // without assuming the provider will finish within a single network round trip.
8+ const CANCEL_DRAIN_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
9+
410#[ derive( Clone , Copy , PartialEq , Eq ) ]
511pub ( super ) enum RecoveryPhase {
612 Idle ,
@@ -52,7 +58,7 @@ impl Drop for RecoverableAttempt {
5258 return ;
5359 } ;
5460 let completed = self . phase == RecoveryPhase :: Completed ;
55- let deadline = tokio:: time:: Instant :: now ( ) + Duration :: from_millis ( 500 ) ;
61+ let deadline = tokio:: time:: Instant :: now ( ) + CANCEL_DRAIN_TIMEOUT ;
5662 runtime. spawn ( async move {
5763 // Absolute deadline plus frame/byte budgets: activity cannot keep a
5864 // cancelled generation alive indefinitely. No response events are
@@ -77,7 +83,10 @@ impl Drop for RecoverableAttempt {
7783 vec ! [
7884 ( "transport_retained" , recovered. to_string( ) ) ,
7985 ( "response_chain_retained" , "false" . to_string( ) ) ,
80- ( "drain_timeout_ms" , "500" . to_string( ) ) ,
86+ (
87+ "drain_timeout_ms" ,
88+ CANCEL_DRAIN_TIMEOUT . as_millis( ) . to_string( ) ,
89+ ) ,
8190 ] ,
8291 ) ;
8392 } ) ;
@@ -277,6 +286,36 @@ mod tests {
277286 server. await . unwrap ( ) ;
278287 }
279288
289+ #[ tokio:: test]
290+ async fn cancellation_retains_affinity_when_completion_takes_over_half_a_second ( ) {
291+ let _env_lock = jcode_base:: storage:: lock_test_env ( ) ;
292+ let ( state, mut peer) = pair ( ) . await ;
293+ let connected_at = state. connected_at ;
294+ let server = tokio:: spawn ( async move {
295+ tokio:: time:: sleep ( Duration :: from_millis ( 650 ) ) . await ;
296+ peer. send ( WsMessage :: Text (
297+ serde_json:: json!( { "type" : "response.completed" } ) . to_string ( ) ,
298+ ) )
299+ . await
300+ . unwrap ( ) ;
301+ peer
302+ } ) ;
303+ let ( attempt, slot) = attempt ( state, RecoveryPhase :: InFlight ) ;
304+ drop ( attempt) ;
305+ let guard = tokio:: time:: timeout ( Duration :: from_secs ( 2 ) , slot. lock ( ) )
306+ . await
307+ . unwrap ( ) ;
308+ let state = guard
309+ . as_ref ( )
310+ . expect ( "delayed completion retains warm socket" ) ;
311+ assert_eq ! ( state. connected_at, connected_at) ;
312+ assert_eq ! ( state. cache_key. as_deref( ) , Some ( "fixture-cache" ) ) ;
313+ assert ! ( state. last_response_id. is_empty( ) ) ;
314+ assert ! ( state. last_response_output. is_empty( ) ) ;
315+ assert ! ( state. last_input_item_hashes. is_empty( ) ) ;
316+ server. await . unwrap ( ) ;
317+ }
318+
280319 #[ tokio:: test]
281320 async fn cancellation_after_terminal_consumed_does_not_drain_next_response ( ) {
282321 let _env_lock = jcode_base:: storage:: lock_test_env ( ) ;
0 commit comments