@@ -259,6 +259,9 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
259259 readonly epoch : number ;
260260 } | null > ( null ) ;
261261 const persistence = yield * Queue . sliding < OrchestrationThreadDetailSnapshot > ( 1 ) ;
262+ // Cache removal must finish after any save that already passed its state
263+ // check. Keep storage I/O separate from the stream/history application lock.
264+ const persistenceLock = yield * Semaphore . make ( 1 ) ;
262265 // Latch set when the server reports the thread missing (subscribe fails
263266 // with OrchestrationThreadNotFoundError). Gates the outer foreground/probe
264267 // resubscribe path below so a deleted thread stops after its single
@@ -268,9 +271,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
268271 const persist = Effect . fn ( "EnvironmentThreadState.persist" ) ( function * (
269272 snapshot : OrchestrationThreadDetailSnapshot ,
270273 ) {
271- // A debounced write racing a deletion must not resurrect the cache after
272- // setDeleted removes it; the queue drain in setDeleted drops pending
273- // offers, this guard drops the offer already past the debounce.
274+ // Under persistenceLock, delayed writes skip deleted snapshots and
275+ // deletion waits for saves that have already passed this check.
274276 if ( ( yield * SubscriptionRef . get ( state ) ) . status === "deleted" ) return ;
275277 if ( resumeCache !== undefined && resumeCache . owner !== owner ) return ;
276278 if (
@@ -304,7 +306,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
304306 ) ,
305307 ) ,
306308 ) ;
307- } ) ;
309+ } , persistenceLock . withPermits ( 1 ) ) ;
308310
309311 yield * Stream . fromQueue ( persistence ) . pipe (
310312 Stream . debounce ( "500 millis" ) ,
@@ -408,17 +410,21 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
408410 // snapshot cannot re-save the thread after deletion. The queue is
409411 // sliding(1), so a single poll discards the whole backlog.
410412 yield * Queue . poll ( persistence ) ;
411- if ( resumeCache !== undefined && resumeCache . owner !== owner ) return ;
412- yield * cache . removeThread ( environmentId , threadId ) . pipe (
413- Effect . catch ( ( error ) =>
414- Effect . logWarning ( "Could not remove the cached thread." ) . pipe (
415- Effect . annotateLogs ( {
416- environmentId,
417- threadId,
418- error : error . message ,
419- } ) ,
420- ) ,
421- ) ,
413+ yield * persistenceLock . withPermits ( 1 ) (
414+ Effect . gen ( function * ( ) {
415+ if ( resumeCache !== undefined && resumeCache . owner !== owner ) return ;
416+ yield * cache . removeThread ( environmentId , threadId ) . pipe (
417+ Effect . catch ( ( error ) =>
418+ Effect . logWarning ( "Could not remove the cached thread." ) . pipe (
419+ Effect . annotateLogs ( {
420+ environmentId,
421+ threadId,
422+ error : error . message ,
423+ } ) ,
424+ ) ,
425+ ) ,
426+ ) ;
427+ } ) ,
422428 ) ;
423429 } ) ;
424430
0 commit comments