@@ -329,12 +329,6 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
329329 disconnected : ReadonlyArray < PendingRequest > ,
330330 completeStream = false ,
331331 ) {
332- yield * Effect . forEach (
333- disconnected ,
334- ( { deferred, context } ) =>
335- Deferred . fail ( deferred , new PreviewAutomationClientDisconnectedError ( context ) ) ,
336- { discard : true } ,
337- ) ;
338332 if ( completeStream ) {
339333 // Discard this generation's commands and complete the RPC stream so a
340334 // responsive desktop can re-register after a timeout eviction.
@@ -344,18 +338,25 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
344338 // Replaced registrations must not reconnect and displace their successor.
345339 yield * Queue . shutdown ( queue ) ;
346340 }
341+ yield * Effect . forEach (
342+ disconnected ,
343+ ( { deferred, context } ) =>
344+ Deferred . fail ( deferred , new PreviewAutomationClientDisconnectedError ( context ) ) ,
345+ { discard : true } ,
346+ ) ;
347347 } ) ;
348348
349349 const disconnect = Effect . fn ( "PreviewAutomationBroker.disconnect" ) ( function * (
350350 clientId : string ,
351351 queue : ClientConnection [ "queue" ] ,
352352 completeStream = false ,
353353 ) {
354- const disconnected = yield * SynchronizedRef . modify ( state , ( current ) => {
354+ yield * SynchronizedRef . modifyEffect ( state , ( current ) => {
355355 const removed = removeConnectionFromState ( current , clientId , queue ) ;
356- return [ removed . disconnected , removed . state ] as const ;
356+ return closeConnection ( queue , removed . disconnected , completeStream ) . pipe (
357+ Effect . as ( [ undefined , removed . state ] as const ) ,
358+ ) ;
357359 } ) ;
358- yield * closeConnection ( queue , disconnected , completeStream ) ;
359360 } ) ;
360361
361362 const acquireConnection = Effect . fn ( "PreviewAutomationBroker.acquireConnection" ) ( function * (
@@ -564,18 +565,28 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
564565 return { ...next , pending } ;
565566 } ) ;
566567 const awaitResponse = Effect . fn ( "PreviewAutomationBroker.awaitResponse" ) ( function * ( ) {
567- const offered = yield * Queue . offer ( connection . queue , {
568- type : "request" ,
569- connectionId : connection . connectionId ,
570- request : {
571- requestId,
572- threadId : input . scope . threadId ,
573- tabId : requestContext . tabId ,
574- tabIdExplicit : input . tabId !== undefined ,
575- operation : input . operation ,
576- input : input . input ,
577- timeoutMs,
578- } ,
568+ const offered = yield * SynchronizedRef . modifyEffect ( state , ( current ) => {
569+ // A route can outlive its generation while another request evicts it.
570+ // Serialize the live-generation check and offer with queue closure.
571+ if (
572+ current . clients . get ( connection . clientId ) ?. queue !== connection . queue ||
573+ ! current . pending . has ( requestId )
574+ ) {
575+ return Effect . succeed ( [ false , current ] as const ) ;
576+ }
577+ return Queue . offer ( connection . queue , {
578+ type : "request" ,
579+ connectionId : connection . connectionId ,
580+ request : {
581+ requestId,
582+ threadId : input . scope . threadId ,
583+ tabId : requestContext . tabId ,
584+ tabIdExplicit : input . tabId !== undefined ,
585+ operation : input . operation ,
586+ input : input . input ,
587+ timeoutMs,
588+ } ,
589+ } ) . pipe ( Effect . map ( ( offered ) => [ offered , current ] as const ) ) ;
579590 } ) ;
580591 if ( ! offered ) {
581592 const completion = yield * Deferred . poll ( deferred ) ;
0 commit comments