@@ -1418,17 +1418,25 @@ export const layerWithOptions = (
14181418 } ) ;
14191419 } ) ;
14201420
1421- const reclaimRefusedIdleRelease = ( entry : LiveSessionEntry , threadId : ThreadId ) =>
1421+ const reclaimRefusedThreadRelease = (
1422+ entry : LiveSessionEntry ,
1423+ threadId : ThreadId ,
1424+ refusal ?: ProviderAdapter . ProviderAdapterThreadUnloadRefusedError ,
1425+ ) =>
14221426 Effect . gen ( function * ( ) {
14231427 const pending = entry . pendingThreadReleases . get ( threadId ) ;
14241428 const idle = entry . idleThreadUnloads . get ( threadId ) ;
14251429 const loaded = entry . loadedProviderThreadByThread . get ( threadId ) ;
14261430 const original = pending ?. providerThreads [ 0 ] ;
14271431 if (
1428- pending ?. idleUnload ?. release !== "refused" ||
1432+ pending === undefined ||
14291433 pending . providerThreads . length !== 1 ||
1430- idle ?. generation !== pending . idleUnload . generation ||
1431- ! entry . attachedThreadIds . has ( threadId ) ||
1434+ ( pending . idleUnload === undefined
1435+ ? refusal ?. driver !== entry . runtime . driver ||
1436+ refusal . providerThreadId !== original ?. id
1437+ : pending . idleUnload . release !== "refused" ||
1438+ ! entry . attachedThreadIds . has ( threadId ) ) ||
1439+ idle ?. generation !== pending . idleUnload ?. generation ||
14321440 loaded === undefined ||
14331441 original ?. nativeThreadRef === null ||
14341442 original === undefined ||
@@ -1439,7 +1447,7 @@ export const layerWithOptions = (
14391447 original . providerSessionId !== entry . runtime . providerSessionId
14401448 )
14411449 return false ;
1442- const generation = pending . idleUnload . generation ;
1450+ const generation = pending . idleUnload ? .generation ;
14431451 const projection = yield * projectionStore . getThreadRecords ( threadId , [ "providerThreads" ] ) ;
14441452 const binding = projection . providerThreads . find (
14451453 ( thread ) => thread . id === projection . thread . activeProviderThreadId ,
@@ -1458,7 +1466,7 @@ export const layerWithOptions = (
14581466 const latestIdle = latest ?. idleThreadUnloads . get ( threadId ) ;
14591467 if (
14601468 latest ?. runtime !== entry . runtime ||
1461- ! latest . attachedThreadIds . has ( threadId ) ||
1469+ latest . attachedThreadIds . has ( threadId ) !== entry . attachedThreadIds . has ( threadId ) ||
14621470 latest . pendingThreadReleases . get ( threadId ) !== pending ||
14631471 latest . loadedProviderThreadByThread . get ( threadId ) !== loaded ||
14641472 latestIdle ?. generation !== generation
@@ -1470,7 +1478,7 @@ export const layerWithOptions = (
14701478 idleThreadUnloads . delete ( threadId ) ;
14711479 const updated = new Map ( current ) ;
14721480 updated . set ( key , { ...latest , pendingThreadReleases, idleThreadUnloads } ) ;
1473- return [ Option . some ( latestIdle . fiber ) , updated ] as const ;
1481+ return [ Option . some ( latestIdle ? .fiber ?? null ) , updated ] as const ;
14741482 } ) ;
14751483 if ( Option . isNone ( reclaimed ) ) return false ;
14761484 yield * cancelIdleFiber ( reclaimed . value ) ;
@@ -1912,9 +1920,20 @@ export const layerWithOptions = (
19121920 detail : "Provider runtime changed before thread attachment; retry the operation." ,
19131921 } ) ;
19141922 }
1915- yield * reclaimRefusedIdleRelease ( entry , threadId ) . pipe (
1923+ yield * reclaimRefusedThreadRelease ( entry , threadId ) . pipe (
19161924 Effect . flatMap ( ( reclaimed ) =>
1917- reclaimed ? Effect . void : confirmPendingThreadRelease ( entry , threadId ) ,
1925+ reclaimed
1926+ ? Effect . void
1927+ : confirmPendingThreadRelease ( entry , threadId ) . pipe (
1928+ Effect . catchTags ( {
1929+ ProviderAdapterThreadUnloadRefusedError : ( refusal ) =>
1930+ reclaimRefusedThreadRelease ( entry , threadId , refusal ) . pipe (
1931+ Effect . flatMap ( ( reclaimed ) =>
1932+ reclaimed ? Effect . void : Effect . fail ( refusal ) ,
1933+ ) ,
1934+ ) ,
1935+ } ) ,
1936+ ) ,
19181937 ) ,
19191938 Effect . mapError (
19201939 ( cause ) =>
0 commit comments