@@ -318,6 +318,34 @@ describe("shared Matrix client generations", () => {
318318 ] ) ;
319319 } ) ;
320320
321+ it ( "retries admission when retirement starts after state resolution" , async ( ) => {
322+ const retiringClient = createMockClient ( "retiring" ) ;
323+ const replacementClient = createMockClient ( "replacement" ) ;
324+ createMatrixClientMock
325+ . mockResolvedValueOnce ( retiringClient )
326+ . mockResolvedValueOnce ( replacementClient ) ;
327+ const auth = authFor ( "main" ) ;
328+ const monitor = await acquireSharedMatrixClient ( {
329+ auth,
330+ role : "monitor" ,
331+ startClient : false ,
332+ } ) ;
333+ monitor . registerMonitorRetirement ( createMonitorRetirement ( [ ] ) ) ;
334+
335+ const racingAcquire = acquireSharedMatrixClient ( { auth, startClient : false } ) ;
336+ let retirement : Promise < void > | undefined ;
337+ queueMicrotask ( ( ) => {
338+ retirement = monitor . release ( { mode : "discard" } ) ;
339+ } ) ;
340+
341+ const racingLease = await racingAcquire ;
342+ await racingLease . release ( { mode : "discard" } ) ;
343+ await retirement ;
344+
345+ expect ( racingLease . client ) . toBe ( replacementClient ) ;
346+ expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 2 ) ;
347+ } ) ;
348+
321349 it ( "signals cooperative transient work and persists after it drains" , async ( ) => {
322350 const callOrder : string [ ] = [ ] ;
323351 const client = createMockClient ( "main" , callOrder ) ;
@@ -360,18 +388,24 @@ describe("shared Matrix client generations", () => {
360388 expect ( client . stopWithoutPersist ) . not . toHaveBeenCalled ( ) ;
361389 } ) ;
362390
363- it ( "bounds non-cooperative transient drain and makes late release harmless " , async ( ) => {
391+ it ( "bounds non-cooperative transient drain and replaces after every late release" , async ( ) => {
364392 vi . useFakeTimers ( ) ;
365393 const callOrder : string [ ] = [ ] ;
366394 const client = createMockClient ( "main" , callOrder ) ;
367- createMatrixClientMock . mockResolvedValue ( client ) ;
395+ const replacementClient = createMockClient ( "replacement" ) ;
396+ createMatrixClientMock . mockResolvedValueOnce ( client ) . mockResolvedValueOnce ( replacementClient ) ;
368397 const auth = authFor ( "main" ) ;
369398 const monitor = await acquireSharedMatrixClient ( {
370399 auth,
371400 role : "monitor" ,
372401 startClient : false ,
373402 } ) ;
374- const transient = await acquireSharedMatrixClient ( {
403+ const firstTransient = await acquireSharedMatrixClient ( {
404+ auth,
405+ role : "transient" ,
406+ startClient : false ,
407+ } ) ;
408+ const finalTransient = await acquireSharedMatrixClient ( {
375409 auth,
376410 role : "transient" ,
377411 startClient : false ,
@@ -390,20 +424,109 @@ describe("shared Matrix client generations", () => {
390424 await expect ( retirementError ) . resolves . toMatchObject ( {
391425 message : "Matrix transient leases did not drain within 5000ms" ,
392426 } ) ;
393- expect ( transient . abortSignal . aborted ) . toBe ( true ) ;
427+ expect ( firstTransient . abortSignal . aborted ) . toBe ( true ) ;
428+ expect ( finalTransient . abortSignal . aborted ) . toBe ( true ) ;
394429 expect ( client . stopWithoutPersist ) . toHaveBeenCalledTimes ( 1 ) ;
395430 expect ( client . stopAndPersist ) . not . toHaveBeenCalled ( ) ;
396431 await expect ( acquireSharedMatrixClient ( { auth } ) ) . rejects . toMatchObject ( {
397432 message : "Matrix transient leases did not drain within 5000ms" ,
398433 } ) ;
399434 expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 1 ) ;
400435
401- const firstLateRelease = transient . release ( { mode : "persist" } ) ;
402- const secondLateRelease = transient . release ( { mode : "persist" } ) ;
403- expect ( secondLateRelease ) . toBe ( firstLateRelease ) ;
436+ const firstLateRelease = firstTransient . release ( { mode : "persist" } ) ;
437+ const duplicateLateRelease = firstTransient . release ( { mode : "persist" } ) ;
438+ expect ( duplicateLateRelease ) . toBe ( firstLateRelease ) ;
404439 await firstLateRelease ;
405440 expect ( client . stopWithoutPersist ) . toHaveBeenCalledTimes ( 1 ) ;
406441 expect ( client . stopAndPersist ) . not . toHaveBeenCalled ( ) ;
442+ await expect ( acquireSharedMatrixClient ( { auth } ) ) . rejects . toMatchObject ( {
443+ message : "Matrix transient leases did not drain within 5000ms" ,
444+ } ) ;
445+ expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 1 ) ;
446+
447+ await finalTransient . release ( { mode : "persist" } ) ;
448+
449+ const [ firstReplacement , secondReplacement ] = await Promise . all ( [
450+ acquireSharedMatrixClient ( { auth, startClient : false } ) ,
451+ acquireSharedMatrixClient ( { auth, startClient : false } ) ,
452+ ] ) ;
453+ expect ( firstReplacement . client ) . toBe ( replacementClient ) ;
454+ expect ( secondReplacement . client ) . toBe ( replacementClient ) ;
455+ expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 2 ) ;
456+ await firstReplacement . release ( { mode : "discard" } ) ;
457+ await secondReplacement . release ( { mode : "discard" } ) ;
458+ } ) ;
459+
460+ it ( "keeps monitor cleanup poison after every late transient releases" , async ( ) => {
461+ vi . useFakeTimers ( ) ;
462+ const cause = new Error ( "monitor cleanup failed" ) ;
463+ const client = createMockClient ( "main" ) ;
464+ createMatrixClientMock . mockResolvedValue ( client ) ;
465+ const auth = authFor ( "main" ) ;
466+ const monitor = await acquireSharedMatrixClient ( {
467+ auth,
468+ role : "monitor" ,
469+ startClient : false ,
470+ } ) ;
471+ const transient = await acquireSharedMatrixClient ( {
472+ auth,
473+ role : "transient" ,
474+ startClient : false ,
475+ } ) ;
476+ const monitorRetirement = createMonitorRetirement ( [ ] ) ;
477+ monitorRetirement . cleanup . mockRejectedValue ( cause ) ;
478+ monitor . registerMonitorRetirement ( monitorRetirement ) ;
479+
480+ const retirementError = monitor . release ( { mode : "persist" } ) . then (
481+ ( ) => null ,
482+ ( error : unknown ) => error ,
483+ ) ;
484+ await vi . advanceTimersByTimeAsync ( 5_000 ) ;
485+ await expect ( retirementError ) . resolves . toBe ( cause ) ;
486+ await transient . release ( ) ;
487+
488+ await expect ( acquireSharedMatrixClient ( { auth } ) ) . rejects . toBe ( cause ) ;
489+ expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 1 ) ;
490+ } ) ;
491+
492+ it ( "keeps poison when the poisoned decryption drain fails before a late release" , async ( ) => {
493+ vi . useFakeTimers ( ) ;
494+ const cause = new Error ( "poisoned decryption drain failed" ) ;
495+ const client = createMockClient ( "main" ) ;
496+ client . drainPendingDecryptions . mockImplementation ( async ( reason : string ) => {
497+ if ( reason === "matrix poisoned client shutdown" ) {
498+ throw cause ;
499+ }
500+ } ) ;
501+ createMatrixClientMock . mockResolvedValue ( client ) ;
502+ const auth = authFor ( "main" ) ;
503+ const monitor = await acquireSharedMatrixClient ( {
504+ auth,
505+ role : "monitor" ,
506+ startClient : false ,
507+ } ) ;
508+ const transient = await acquireSharedMatrixClient ( {
509+ auth,
510+ role : "transient" ,
511+ startClient : false ,
512+ } ) ;
513+ monitor . registerMonitorRetirement ( createMonitorRetirement ( [ ] ) ) ;
514+
515+ const retirementError = monitor . release ( { mode : "persist" } ) . then (
516+ ( ) => null ,
517+ ( error : unknown ) => error ,
518+ ) ;
519+ await vi . advanceTimersByTimeAsync ( 5_000 ) ;
520+ await expect ( retirementError ) . resolves . toMatchObject ( {
521+ message : "Matrix transient leases did not drain within 5000ms" ,
522+ } ) ;
523+ await transient . release ( ) ;
524+
525+ await expect ( acquireSharedMatrixClient ( { auth } ) ) . rejects . toMatchObject ( {
526+ message : "Matrix transient leases did not drain within 5000ms" ,
527+ } ) ;
528+ expect ( client . stopWithoutPersist ) . toHaveBeenCalledTimes ( 1 ) ;
529+ expect ( createMatrixClientMock ) . toHaveBeenCalledTimes ( 1 ) ;
407530 } ) ;
408531
409532 it ( "quiesces and cleans up the monitor before waiting for an existing transient lease" , async ( ) => {
0 commit comments