@@ -156,7 +156,8 @@ test("a close after EOSE stamps liveUntil at the last frame and the replay resum
156156 window . clearTimeout ( client . reconnectTimeout ) ;
157157 client . reconnectTimeout = null ;
158158
159- now = 2_100_000 ;
159+ // Reconnect late enough that liveUntil, not the silent-drop window, binds.
160+ now = 5_000_000 ;
160161 client . wsId = 8 ;
161162 writes . length = 0 ;
162163 await client . replayLiveSubscriptions ( ) ;
@@ -166,6 +167,42 @@ test("a close after EOSE stamps liveUntil at the last frame and the replay resum
166167 ) ;
167168} ) ;
168169
170+ test ( "a quiet subscription whose fan-out was dropped still replays the drop on a prompt reconnect" , async ( ) => {
171+ // The relay can drop a fan-out frame under backpressure and keep the socket
172+ // open, so pings after the drop do not prove the subscription missed nothing.
173+ now = 10_000_000 ;
174+ const { client, ids } = await liveClient ( [ quiet ] ) ;
175+ client . stallWatchdog . start ( ) ;
176+ const droppedAt = 10_100 ; // stored, but its fan-out frame never arrived
177+ for ( now = 10_030_000 ; now <= 12_000_000 ; now += 30_000 ) {
178+ await client . handleWsMessage (
179+ { type : "Ping" , data : [ ] } ,
180+ client . connectionGeneration ,
181+ ) ;
182+ }
183+ now -= 30_000 ;
184+ await close ( client , 1006 ) ;
185+ const subscription = client . subscriptions . get ( ids [ 0 ] ) ;
186+ assert . equal ( subscription . liveUntil , Math . floor ( now / 1_000 ) ) ;
187+ assert . ok (
188+ subscription . liveUntil - 960 > droppedAt ,
189+ "precondition: the lookback alone would skip the drop" ,
190+ ) ;
191+ window . clearTimeout ( client . reconnectTimeout ) ;
192+ client . reconnectTimeout = null ;
193+
194+ now += 2_000 ;
195+ client . wsId = 8 ;
196+ writes . length = 0 ;
197+ await client . replayLiveSubscriptions ( ) ;
198+ const { since } = writes [ 0 ] . frame [ 2 ] ;
199+ assert . equal ( since , Math . floor ( now / 1_000 ) - 3_600 ) ;
200+ assert . ok (
201+ droppedAt >= since ,
202+ `drop at ${ droppedAt } must satisfy since ${ since } ` ,
203+ ) ;
204+ } ) ;
205+
169206test ( "a subscription that never reached EOSE keeps replaying its original filter" , async ( ) => {
170207 now = 2_000_000 ;
171208 const second = { ...quiet , "#h" : [ "channel-1" ] } ;
@@ -177,6 +214,7 @@ test("a subscription that never reached EOSE keeps replaying its original filter
177214 window . clearTimeout ( client . reconnectTimeout ) ;
178215 client . reconnectTimeout = null ;
179216
217+ now = 5_000_000 ;
180218 client . wsId = 8 ;
181219 writes . length = 0 ;
182220 await client . replayLiveSubscriptions ( ) ;
@@ -284,6 +322,49 @@ test("a peer Ping is liveness; the close after it is not", async () => {
284322 assert . equal ( client . subscriptions . get ( ids [ 0 ] ) . liveUntil , 2_060 ) ;
285323} ) ;
286324
325+ test ( "a frame queued through a sleep does not count as liveness" , async ( ) => {
326+ now = 2_000_000 ;
327+ const { client, ids } = await liveClient ( [ quiet ] ) ;
328+ client . stallWatchdog . start ( ) ;
329+ now = 2_030_000 ;
330+ await deliver ( client , [ "NOTICE" , "hello" ] ) ; // last frame before the sleep
331+ // A ping and a NOTICE were queued just before the sleep; the renderer
332+ // processes them on wake, ahead of the overdue idle tick and the close.
333+ now = 2_030_000 + 3_600_000 ;
334+ await client . handleWsMessage (
335+ { type : "Ping" , data : [ ] } ,
336+ client . connectionGeneration ,
337+ ) ;
338+ now += 1_000 ;
339+ await deliver ( client , [ "NOTICE" , "queued" ] ) ;
340+ await close ( client , 1006 ) ;
341+ assert . equal (
342+ client . subscriptions . get ( ids [ 0 ] ) . liveUntil ,
343+ 2_030 ,
344+ "stamped at the last frame before the gap, not at wake" ,
345+ ) ;
346+ } ) ;
347+
348+ test ( "a new connection's liveness credit does not inherit the previous socket's" , async ( ) => {
349+ now = 2_000_000 ;
350+ const { client, ids } = await liveClient ( [ quiet ] ) ;
351+ client . relayUrl = "ws://relay.test" ;
352+ await close ( client , 1006 ) ;
353+ // Reconnect well past the gap limit: the AUTH OK starts fresh credit
354+ // instead of reading as a frame after a gap on the old socket's clock.
355+ timers . get ( client . reconnectTimeout ) . at += 300_000 ;
356+ await redial ( client ) ;
357+ const authAt = now ;
358+ now += 30_000 ;
359+ await deliver ( client , [ "EOSE" , ids [ 0 ] ] ) ;
360+ now += 1_000 ;
361+ await close ( client , 1006 ) ;
362+ assert . equal (
363+ client . subscriptions . get ( ids [ 0 ] ) . liveUntil ,
364+ Math . floor ( ( authAt + 30_000 ) / 1_000 ) ,
365+ ) ;
366+ } ) ;
367+
287368for ( const code of [ 1012 , 1001 ] ) {
288369 test ( `close ${ code } spreads the first reconnect over [0, 10s] and keeps the backoff escalated` , async ( ) => {
289370 const { client } = await liveClient ( [ quiet ] ) ;
@@ -369,18 +450,19 @@ test("a close delivered late during replay stamps the last frame, not the wake",
369450 await close ( client , 1006 ) ;
370451 await redial ( client ) ;
371452 // The quiet channel reaches EOSE while the paged repair is still pending.
372- now = 2_100_000 ;
453+ now += 30_000 ;
454+ const eoseAt = Math . floor ( now / 1_000 ) ;
373455 await deliver ( client , [ "EOSE" , ids [ 0 ] ] ) ;
374456 await deliver ( client , [ "EOSE" , ids [ 1 ] ] ) ;
375457 await flush ( ) ;
376458 assert . equal ( repairRequests . length , 1 , "precondition: repair in flight" ) ;
377- assert . equal ( intervals . size , 0 , "precondition: watchdog not started yet " ) ;
459+ assert . equal ( intervals . size , 1 , "the watchdog runs during replay " ) ;
378460
379461 // An hour of sleep, then the close is delivered.
380- now = 2_100_000 + 3_600_000 ;
462+ now += 3_600_000 ;
381463 await close ( client , 1006 ) ;
382464 const subscription = client . subscriptions . get ( ids [ 0 ] ) ;
383- assert . equal ( subscription . liveUntil , 2_100 , "stamped at the EOSE frame" ) ;
465+ assert . equal ( subscription . liveUntil , eoseAt , "stamped at the EOSE frame" ) ;
384466
385467 // The in-flight HTTP page settles; the stale attempt then stands down.
386468 for ( const { resolve } of repairRequests . splice ( 0 ) ) resolve ( [ ] ) ;
@@ -389,7 +471,33 @@ test("a close delivered late during replay stamps the last frame, not the wake",
389471 writes . length = 0 ;
390472 await redial ( client ) ;
391473 const quietReq = writes . find ( ( { frame } ) => frame [ 1 ] === ids [ 0 ] ) ;
392- assert . equal ( quietReq . frame [ 2 ] . since , 2_100 - 960 ) ;
474+ assert . equal ( quietReq . frame [ 2 ] . since , eoseAt - 960 ) ;
475+ } ) ;
476+
477+ test ( "a half-open socket that never acknowledges replay is reset by the stall watchdog" , async ( ) => {
478+ now = 2_000_000 ;
479+ const { client, ids } = await liveClient ( [ paged ] ) ;
480+ client . relayUrl = "ws://relay.test" ;
481+ await close ( client , 1006 ) ;
482+ await redial ( client ) ;
483+ assert . deepEqual ( writes . at ( - 1 ) . frame , [
484+ "REQ" ,
485+ ids [ 0 ] ,
486+ { ...paged , limit : 0 } ,
487+ ] ) ;
488+ assert . notEqual ( client . connectPromise , null , "precondition: parked on EOSE" ) ;
489+ assert . equal ( client . connectionStateEmitter . get ( ) , "connected" ) ;
490+ assert . equal ( intervals . size , 1 , "the watchdog is armed while parked" ) ;
491+
492+ // No EOSE, no close: the idle tick is what notices.
493+ now += 70_000 ;
494+ const [ tick ] = intervals . values ( ) ;
495+ tick ( ) ;
496+ await flush ( ) ;
497+ assert . equal ( client . connectionStateEmitter . get ( ) , "stalled" ) ;
498+ assert . equal ( client . connectPromise , null , "connect() released by the reset" ) ;
499+ assert . equal ( intervals . size , 0 ) ;
500+ assert . equal ( repairRequests . length , 0 , "no blind repair" ) ;
393501} ) ;
394502
395503test ( "disposing a subscription parked on its paged live EOSE releases connect()" , async ( ) => {
0 commit comments