@@ -66,11 +66,38 @@ const NETWORK_STREAM_WAIT_WARNING: Duration = Duration::from_secs(5);
6666/// after a takeover retires its connection.
6767const ADOPTED_ATTACHMENT_RELEASE_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
6868
69- fn begin_recovery_window (
70- deadline : & mut Option < tokio:: time:: Instant > ,
71- failure_time : tokio:: time:: Instant ,
72- ) -> tokio:: time:: Instant {
73- * deadline. get_or_insert ( failure_time + CONNECT_RETRY_TIMEOUT )
69+ /// Recovery budget for one outage of a long-lived wait stream.
70+ ///
71+ /// A wait can outlive many connections. The budget opens at the first
72+ /// transport failure on a connection generation and covers only that outage:
73+ /// a failure on a newer generation means an earlier recovery replaced the
74+ /// connection, so it starts a fresh window instead of inheriting one that may
75+ /// have expired long ago. Repeated failures on the same generation (for
76+ /// example, a live connection whose streams keep failing) share one window.
77+ #[ derive( Debug , Default ) ]
78+ struct WaitRecoveryWindow {
79+ window : Option < ( Option < u64 > , tokio:: time:: Instant ) > ,
80+ }
81+
82+ impl WaitRecoveryWindow {
83+ /// Returns the deadline for recovering from a failure observed on
84+ /// `generation` at `failure_time`.
85+ fn deadline (
86+ & mut self ,
87+ generation : Option < u64 > ,
88+ failure_time : tokio:: time:: Instant ,
89+ ) -> tokio:: time:: Instant {
90+ if self
91+ . window
92+ . is_some_and ( |( window_generation, _) | window_generation != generation)
93+ {
94+ self . window = None ;
95+ }
96+ let ( _, deadline) = * self
97+ . window
98+ . get_or_insert ( ( generation, failure_time + CONNECT_RETRY_TIMEOUT ) ) ;
99+ deadline
100+ }
74101}
75102
76103/// Internal raw transport establishment; shared code still authenticates
@@ -1953,14 +1980,13 @@ impl BoundaryClient {
19531980 request : Request ,
19541981 ) -> Result < ( BoundaryDuplexStream , Response ) , BackendError > {
19551982 let envelope = Self :: prepare_request ( request) ?;
1956- let mut recovery_deadline = None ;
1983+ let mut recovery = WaitRecoveryWindow :: default ( ) ;
19571984 loop {
19581985 let generation = self . connection_generation ( ) . await ;
19591986 match self . open_exchange_envelope ( & envelope) . await {
19601987 Ok ( response) => return Ok ( response) ,
19611988 Err ( BackendError :: Unavailable ( message) ) if is_transport_unavailable ( & message) => {
1962- let deadline =
1963- begin_recovery_window ( & mut recovery_deadline, tokio:: time:: Instant :: now ( ) ) ;
1989+ let deadline = recovery. deadline ( generation, tokio:: time:: Instant :: now ( ) ) ;
19641990 if tokio:: time:: Instant :: now ( ) >= deadline {
19651991 return Err ( BackendError :: Unavailable ( message) ) ;
19661992 }
@@ -3731,6 +3757,10 @@ mod tests {
37313757 kind : crate :: boundary_protocol:: BoundaryErrorKind :: Denied ,
37323758 message : "a mediation session is already active" . to_string ( ) ,
37333759 } ,
3760+ Request :: Wait { process_id } if hold_test_wait ( & process_id) => {
3761+ // Keep the wait in flight until its transport fails.
3762+ std:: future:: pending ( ) . await
3763+ }
37343764 Request :: Wait { .. } => Response :: Exited {
37353765 status : ExitStatusWire :: Exited ( 23 ) ,
37363766 } ,
@@ -4007,6 +4037,88 @@ mod tests {
40074037 assert_eq ! ( requests. load( Ordering :: Acquire ) , 5 ) ;
40084038 }
40094039
4040+ #[ tokio:: test]
4041+ async fn wait_survives_a_second_transport_loss_after_recovery ( ) {
4042+ let certificate = test_certificate ( ) ;
4043+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
4044+ let address = listener. local_addr ( ) . unwrap ( ) ;
4045+ let accepted = Arc :: new ( std:: sync:: atomic:: AtomicUsize :: new ( 0 ) ) ;
4046+ let server_accepted = accepted. clone ( ) ;
4047+ let server_config = certificate. server_config . clone ( ) ;
4048+ let server = tokio:: spawn ( async move {
4049+ for _ in 0 ..3 {
4050+ let ( stream, _) = listener. accept ( ) . await . unwrap ( ) ;
4051+ let stream = tokio_rustls:: TlsAcceptor :: from ( server_config. clone ( ) )
4052+ . accept ( stream)
4053+ . await
4054+ . unwrap ( ) ;
4055+ server_accepted. fetch_add ( 1 , Ordering :: AcqRel ) ;
4056+ tokio:: spawn ( serve_test_grpc ( Box :: new ( stream) , "a" . repeat ( 32 ) ) ) ;
4057+ }
4058+ } ) ;
4059+ let client = Arc :: new ( BoundaryClient :: new (
4060+ tls_runtime_descriptor ( address, certificate. client_tls ) ,
4061+ test_bearer ( & "a" . repeat ( 32 ) ) ,
4062+ ) ) ;
4063+ let attach = Request :: Attach {
4064+ supervisor_instance_id : client. supervisor_instance_id ,
4065+ policy : Box :: new ( SandboxPolicyWire :: from ( sandbox ( ) . policy ) ) ,
4066+ supervisor_takeover : None ,
4067+ supervisor_recovery_timeout_secs : None ,
4068+ } ;
4069+ assert ! ( matches!(
4070+ client. call_idempotent( attach) . await . unwrap( ) ,
4071+ Response :: Attached { .. }
4072+ ) ) ;
4073+ assert ! ( matches!(
4074+ client. call_idempotent( Request :: Confirm ) . await . unwrap( ) ,
4075+ Response :: Confirmed { .. }
4076+ ) ) ;
4077+ let process = BoundaryProcess {
4078+ exec : false ,
4079+ adopted : false ,
4080+ client : client. clone ( ) ,
4081+ process_id : HELD_WAIT_PROCESS_ID . to_string ( ) ,
4082+ } ;
4083+ let waiting = tokio:: spawn ( async move { process. wait ( ) . await } ) ;
4084+ held_waits_reach ( 1 ) . await ;
4085+
4086+ // First outage: the wait reattaches on a replacement connection.
4087+ abort_current_transport ( & client) . await ;
4088+ held_waits_reach ( 2 ) . await ;
4089+ assert_eq ! ( accepted. load( Ordering :: Acquire ) , 2 ) ;
4090+ assert ! (
4091+ !waiting. is_finished( ) ,
4092+ "the first transport loss is recovered"
4093+ ) ;
4094+
4095+ // The workload keeps running well past the first outage's recovery
4096+ // window. Step virtual time so real I/O, such as HTTP/2 keepalive
4097+ // pings, still completes on the live replacement connection.
4098+ for _ in 0 ..=CONNECT_RETRY_TIMEOUT . as_secs ( ) {
4099+ tokio:: time:: pause ( ) ;
4100+ tokio:: time:: advance ( Duration :: from_secs ( 1 ) ) . await ;
4101+ tokio:: time:: resume ( ) ;
4102+ tokio:: time:: sleep ( Duration :: from_millis ( 5 ) ) . await ;
4103+ }
4104+ assert ! (
4105+ !waiting. is_finished( ) ,
4106+ "the replacement connection stays live"
4107+ ) ;
4108+
4109+ // Second outage on the replacement connection: the boundary is still
4110+ // reachable, so the wait must reattach again rather than report loss.
4111+ abort_current_transport ( & client) . await ;
4112+ let status = tokio:: time:: timeout ( Duration :: from_secs ( 10 ) , waiting)
4113+ . await
4114+ . expect ( "wait finishes once the boundary answers" )
4115+ . expect ( "wait task" )
4116+ . expect ( "a second transport loss must be recovered, not reported as boundary loss" ) ;
4117+ assert_eq ! ( status, BoundaryExitStatus :: Exited ( 23 ) ) ;
4118+ assert_eq ! ( accepted. load( Ordering :: Acquire ) , 3 ) ;
4119+ server. await . unwrap ( ) ;
4120+ }
4121+
40104122 #[ tokio:: test]
40114123 async fn stopping_client_reports_a_lost_boundary_without_recovery ( ) {
40124124 let certificate = test_certificate ( ) ;
@@ -4077,14 +4189,46 @@ mod tests {
40774189 fn wait_recovery_window_begins_at_transport_failure ( ) {
40784190 let wait_started = tokio:: time:: Instant :: now ( ) ;
40794191 let failure_time = wait_started + CONNECT_RETRY_TIMEOUT + Duration :: from_secs ( 5 ) ;
4080- let mut deadline = None ;
4192+ let mut recovery = WaitRecoveryWindow :: default ( ) ;
40814193
40824194 assert_eq ! (
4083- begin_recovery_window ( & mut deadline, failure_time) ,
4195+ recovery . deadline( Some ( 1 ) , failure_time) ,
40844196 failure_time + CONNECT_RETRY_TIMEOUT
40854197 ) ;
40864198 }
40874199
4200+ #[ test]
4201+ fn wait_recovery_window_is_shared_by_failures_on_one_connection ( ) {
4202+ let first_failure = tokio:: time:: Instant :: now ( ) ;
4203+ let mut recovery = WaitRecoveryWindow :: default ( ) ;
4204+ let deadline = recovery. deadline ( Some ( 1 ) , first_failure) ;
4205+
4206+ // A live connection whose streams keep failing must not retry forever.
4207+ assert_eq ! (
4208+ recovery. deadline( Some ( 1 ) , first_failure + Duration :: from_secs( 20 ) ) ,
4209+ deadline
4210+ ) ;
4211+ let late_failure = first_failure + CONNECT_RETRY_TIMEOUT + Duration :: from_secs ( 1 ) ;
4212+ assert ! ( late_failure >= recovery. deadline( Some ( 1 ) , late_failure) ) ;
4213+ }
4214+
4215+ #[ test]
4216+ fn wait_recovery_window_resets_after_connection_is_replaced ( ) {
4217+ // A wait loses its connection, recovers onto a new generation, and
4218+ // runs for far longer than the retry window before losing that
4219+ // replacement too. The second outage needs its own budget; inheriting
4220+ // the first window turned it into "boundary lost during wait" without
4221+ // any reattach attempt.
4222+ let first_failure = tokio:: time:: Instant :: now ( ) ;
4223+ let mut recovery = WaitRecoveryWindow :: default ( ) ;
4224+ recovery. deadline ( Some ( 1 ) , first_failure) ;
4225+
4226+ let second_failure = first_failure + Duration :: from_mins ( 43 ) ;
4227+ let deadline = recovery. deadline ( Some ( 2 ) , second_failure) ;
4228+ assert_eq ! ( deadline, second_failure + CONNECT_RETRY_TIMEOUT ) ;
4229+ assert ! ( second_failure < deadline) ;
4230+ }
4231+
40884232 #[ tokio:: test]
40894233 async fn mediation_stream_failure_retries_without_deadlocking_the_cache ( ) {
40904234 let certificate = test_certificate ( ) ;
@@ -4451,6 +4595,39 @@ mod tests {
44514595 . expect ( "test bearer slot" )
44524596 }
44534597
4598+ /// Waits for this process are held open by `TestGrpcBoundary` for the
4599+ /// first `HELD_WAITS` arrivals, then answered with `Exited(23)`.
4600+ const HELD_WAIT_PROCESS_ID : & str = "test-generation:held-wait:0" ;
4601+ const HELD_WAITS : usize = 2 ;
4602+ static HELD_WAIT_ARRIVALS : std:: sync:: atomic:: AtomicUsize =
4603+ std:: sync:: atomic:: AtomicUsize :: new ( 0 ) ;
4604+
4605+ fn hold_test_wait ( process_id : & str ) -> bool {
4606+ process_id == HELD_WAIT_PROCESS_ID
4607+ && HELD_WAIT_ARRIVALS . fetch_add ( 1 , Ordering :: AcqRel ) < HELD_WAITS
4608+ }
4609+
4610+ async fn held_waits_reach ( count : usize ) {
4611+ tokio:: time:: timeout ( Duration :: from_secs ( 5 ) , async {
4612+ while HELD_WAIT_ARRIVALS . load ( Ordering :: Acquire ) < count {
4613+ tokio:: time:: sleep ( Duration :: from_millis ( 5 ) ) . await ;
4614+ }
4615+ } )
4616+ . await
4617+ . expect ( "held wait reaches the test boundary" ) ;
4618+ }
4619+
4620+ async fn abort_current_transport ( client : & BoundaryClient ) {
4621+ client
4622+ . grpc_channel
4623+ . lock ( )
4624+ . await
4625+ . as_ref ( )
4626+ . expect ( "cached channel" )
4627+ . transport
4628+ . abort ( ) ;
4629+ }
4630+
44544631 async fn spawn_tls_boundary (
44554632 certificate : Arc < rustls:: ServerConfig > ,
44564633 expected_token : String ,
0 commit comments