@@ -38,6 +38,18 @@ const registerDisposerScoped = (disposer: (directory: string) => Promise<void>)
3838 ( off ) => Effect . sync ( off ) ,
3939 )
4040
41+ // Releases a gated disposer on scope teardown and yields long enough for an in-flight reload body
42+ // to settle before the layer's own `disposeAll` finalizer closes. Without the yield a base-arm
43+ // assertion failure leaves the stalled teardown wedging scope close instead of reporting the
44+ // assertion (the same wedge the fix removes on the fixed arm).
45+ const releaseGatedDisposer = ( release : ( ) => void ) =>
46+ Effect . addFinalizer ( ( ) =>
47+ Effect . gen ( function * ( ) {
48+ release ( )
49+ yield * Effect . sleep ( "500 millis" )
50+ } ) ,
51+ )
52+
4153describe ( "InstanceStore" , ( ) => {
4254 it . live ( "loads instance context" , ( ) =>
4355 Effect . gen ( function * ( ) {
@@ -764,6 +776,196 @@ describe("InstanceStore", () => {
764776 30_000 ,
765777 )
766778
779+ it . live (
780+ "a never-settling reusable disposer does not wedge reload, provide or load (NEW-V14-06)" ,
781+ ( ) =>
782+ Effect . gen ( function * ( ) {
783+ const dir = yield * tmpdirScoped ( { git : true } )
784+ const store = yield * InstanceStore . Service
785+ const events : string [ ] = [ ]
786+ let releaseDisposer : ( ) => void = ( ) => { }
787+ const gate = new Promise < void > ( ( resolve ) => {
788+ releaseDisposer = resolve
789+ } )
790+ // Unblock the disposer on scope teardown too, so an assertion failure cannot leave the
791+ // stalled teardown wedging the layer's own `disposeAll` finalizer.
792+ yield * releaseGatedDisposer ( ( ) => releaseDisposer ( ) )
793+ yield * registerDisposerScoped ( async ( ) => {
794+ events . push ( "dispose-start" )
795+ await gate
796+ events . push ( "dispose-end" )
797+ } )
798+ yield * setBootstrap (
799+ Effect . sync ( ( ) => {
800+ events . push ( "boot" )
801+ } ) ,
802+ )
803+
804+ const first = yield * store . load ( { directory : dir } )
805+
806+ // Plain load -> reload: the previous entry is reusable, so reload runs the stalled
807+ // directory-global disposer itself. Pre-fix that await is unbounded and wedges the
808+ // directory forever (base 7b7e70a6c1 reproduces reload, then provide and load too).
809+ const reloaded = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
810+ expect ( Option . isSome ( reloaded ) ) . toBe ( true )
811+ if ( Option . isSome ( reloaded ) ) expect ( reloaded . value ) . not . toBe ( first )
812+
813+ // Proof the bound actually fired: the replacement booted while the disposer was still
814+ // stalled, and the teardown was started rather than skipped.
815+ expect ( events ) . not . toContain ( "dispose-end" )
816+ expect ( events . filter ( ( event ) => event === "boot" ) ) . toHaveLength ( 2 )
817+
818+ // The replacement is live: a still-running disposer must not wedge leases or loads.
819+ const alive = yield * store
820+ . provide ( { directory : dir } , Effect . succeed ( "alive" as const ) )
821+ . pipe ( Effect . timeoutOption ( "4 seconds" ) )
822+ expect ( Option . isSome ( alive ) ) . toBe ( true )
823+
824+ const cached = yield * store . load ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "4 seconds" ) )
825+ expect ( Option . isSome ( cached ) ) . toBe ( true )
826+ if ( Option . isSome ( cached ) && Option . isSome ( reloaded ) ) expect ( cached . value ) . toBe ( reloaded . value )
827+
828+ // Once the stalled disposer finally settles, the directory recovers fully.
829+ releaseDisposer ( )
830+ yield * Effect . gen ( function * ( ) {
831+ while ( ! events . includes ( "dispose-end" ) ) yield * Effect . sleep ( "25 millis" )
832+ } ) . pipe ( Effect . timeoutOption ( "4 seconds" ) )
833+ expect ( events ) . toContain ( "dispose-end" )
834+ } ) ,
835+ 30_000 ,
836+ )
837+
838+ it . live (
839+ "a late-settling reusable disposer still serializes dispose-end before boot (NEW-V14-06)" ,
840+ ( ) =>
841+ Effect . gen ( function * ( ) {
842+ const dir = yield * tmpdirScoped ( { git : true } )
843+ const store = yield * InstanceStore . Service
844+ const events : string [ ] = [ ]
845+ yield * registerDisposerScoped ( async ( ) => {
846+ events . push ( "dispose-start" )
847+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) )
848+ events . push ( "dispose-end" )
849+ } )
850+ yield * setBootstrap (
851+ Effect . sync ( ( ) => {
852+ events . push ( "boot" )
853+ } ) ,
854+ )
855+
856+ const first = yield * store . load ( { directory : dir } )
857+ const reloaded = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
858+ expect ( Option . isSome ( reloaded ) ) . toBe ( true )
859+ if ( Option . isSome ( reloaded ) ) expect ( reloaded . value ) . not . toBe ( first )
860+
861+ // A disposer that settles within the bound keeps the reload-scoped ordering guarantee:
862+ // the replacement boots only after the old directory-global teardown finished.
863+ expect ( events ) . toEqual ( [ "boot" , "dispose-start" , "dispose-end" , "boot" ] )
864+ expect ( events . lastIndexOf ( "dispose-end" ) ) . toBeLessThan ( events . lastIndexOf ( "boot" ) )
865+
866+ const alive = yield * store
867+ . provide ( { directory : dir } , Effect . succeed ( "alive" as const ) )
868+ . pipe ( Effect . timeoutOption ( "4 seconds" ) )
869+ expect ( Option . isSome ( alive ) ) . toBe ( true )
870+ } ) ,
871+ 30_000 ,
872+ )
873+
874+ it . live (
875+ "repeated reloads stay bounded behind a never-settling reusable disposer (NEW-V14-06)" ,
876+ ( ) =>
877+ Effect . gen ( function * ( ) {
878+ const dir = yield * tmpdirScoped ( { git : true } )
879+ const store = yield * InstanceStore . Service
880+ const events : string [ ] = [ ]
881+ let releaseDisposer : ( ) => void = ( ) => { }
882+ const gate = new Promise < void > ( ( resolve ) => {
883+ releaseDisposer = resolve
884+ } )
885+ yield * releaseGatedDisposer ( ( ) => releaseDisposer ( ) )
886+ yield * registerDisposerScoped ( async ( ) => {
887+ events . push ( "dispose-start" )
888+ await gate
889+ events . push ( "dispose-end" )
890+ } )
891+ yield * setBootstrap ( Effect . sync ( ( ) => void events . push ( "boot" ) ) )
892+
893+ yield * store . load ( { directory : dir } )
894+
895+ // Every reload boots its own replacement, so the second one takes the reusable path again
896+ // and runs the still-stalled disposer; both waits must be bounded.
897+ const firstReload = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
898+ expect ( Option . isSome ( firstReload ) ) . toBe ( true )
899+ const secondReload = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
900+ expect ( Option . isSome ( secondReload ) ) . toBe ( true )
901+ if ( Option . isSome ( firstReload ) && Option . isSome ( secondReload ) ) {
902+ expect ( secondReload . value ) . not . toBe ( firstReload . value )
903+ }
904+
905+ const alive = yield * store
906+ . provide ( { directory : dir } , Effect . succeed ( "alive" as const ) )
907+ . pipe ( Effect . timeoutOption ( "4 seconds" ) )
908+ expect ( Option . isSome ( alive ) ) . toBe ( true )
909+
910+ releaseDisposer ( )
911+ yield * Effect . gen ( function * ( ) {
912+ while ( ! events . includes ( "dispose-end" ) ) yield * Effect . sleep ( "25 millis" )
913+ } ) . pipe ( Effect . timeoutOption ( "4 seconds" ) )
914+ expect ( events ) . toContain ( "dispose-end" )
915+ } ) ,
916+ 30_000 ,
917+ )
918+
919+ it . live (
920+ "an interrupted drain does not wedge a later reusable reload (NEW-V14-06)" ,
921+ ( ) =>
922+ Effect . gen ( function * ( ) {
923+ const dir = yield * tmpdirScoped ( { git : true } )
924+ const store = yield * InstanceStore . Service
925+ const events : string [ ] = [ ]
926+ let releaseDisposer : ( ) => void = ( ) => { }
927+ const gate = new Promise < void > ( ( resolve ) => {
928+ releaseDisposer = resolve
929+ } )
930+ yield * releaseGatedDisposer ( ( ) => releaseDisposer ( ) )
931+ yield * registerDisposerScoped ( async ( ) => {
932+ events . push ( "dispose-start" )
933+ await gate
934+ events . push ( "dispose-end" )
935+ } )
936+ yield * setBootstrap ( Effect . sync ( ( ) => void events . push ( "boot" ) ) )
937+
938+ const first = yield * store . load ( { directory : dir } )
939+ const disposing = yield * store . dispose ( first ) . pipe ( Effect . forkScoped )
940+ yield * Effect . sleep ( "150 millis" )
941+ yield * Fiber . interrupt ( disposing )
942+
943+ // Reload #1 follows the interrupted drain: it waits through the bounded tracker path.
944+ const firstReload = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
945+ expect ( Option . isSome ( firstReload ) ) . toBe ( true )
946+
947+ // Reload #2 is the ordinary reusable path and must stay bounded even though the same
948+ // never-settling disposer is still in flight.
949+ const secondReload = yield * store . reload ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "8 seconds" ) )
950+ expect ( Option . isSome ( secondReload ) ) . toBe ( true )
951+
952+ const alive = yield * store
953+ . provide ( { directory : dir } , Effect . succeed ( "alive" as const ) )
954+ . pipe ( Effect . timeoutOption ( "4 seconds" ) )
955+ expect ( Option . isSome ( alive ) ) . toBe ( true )
956+
957+ const cached = yield * store . load ( { directory : dir } ) . pipe ( Effect . timeoutOption ( "4 seconds" ) )
958+ expect ( Option . isSome ( cached ) ) . toBe ( true )
959+
960+ releaseDisposer ( )
961+ yield * Effect . gen ( function * ( ) {
962+ while ( ! events . includes ( "dispose-end" ) ) yield * Effect . sleep ( "25 millis" )
963+ } ) . pipe ( Effect . timeoutOption ( "4 seconds" ) )
964+ expect ( events ) . toContain ( "dispose-end" )
965+ } ) ,
966+ 45_000 ,
967+ )
968+
767969 it . live (
768970 "never uses a disposed context under concurrent provide/dispose (W5/F4a)" ,
769971 ( ) =>
0 commit comments