@@ -32,7 +32,12 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
3232import * as Persistence from "../platform/persistence.ts" ;
3333import { runCachePersistence } from "./cachePersistence.ts" ;
3434import * as ManagedRelay from "../relay/managedRelay.ts" ;
35- import { isAuthorizationFailure , request , subscribeDynamic } from "../rpc/client.ts" ;
35+ import {
36+ isAuthorizationFailure ,
37+ request ,
38+ subscribeDynamic ,
39+ subscriptionRetryDelay ,
40+ } from "../rpc/client.ts" ;
3641import { parseThreadKey , threadKey } from "./entities.ts" ;
3742import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts" ;
3843import { THREAD_SNAPSHOT_IDLE_TTL_MS } from "./threadRetention.ts" ;
@@ -55,6 +60,8 @@ import {
5560 type EnvironmentThreadStatus ,
5661} from "./threadState.ts" ;
5762
63+ const THREAD_SUBSCRIPTION_RETRY_DELAY = "250 millis" ;
64+
5865function statusWithoutLiveData (
5966 data : Option . Option < OrchestrationV2ThreadProjection > ,
6067) : EnvironmentThreadStatus {
@@ -967,8 +974,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
967974 ( yield * Ref . get ( socketOnlyOpen ) ) && Option . isNone ( ( yield * SubscriptionRef . get ( state ) ) . data )
968975 ) ;
969976 } ) ;
970- // A defect parks the subscription, so a socket-only open resubscribes once .
971- const httpRetries = yield * Queue . unbounded < void > ( ) ;
977+ // A stream defect parks the subscription until something resubscribes it .
978+ const resubscribes = yield * Queue . unbounded < void > ( ) ;
972979 const markSynchronizing = Effect . gen ( function * ( ) {
973980 if ( yield * Ref . get ( resumingLive ) ) return ;
974981 yield * SubscriptionRef . update ( state , ( current ) =>
@@ -979,15 +986,27 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
979986 } ) ;
980987
981988 yield * markSynchronizing ;
982- yield * Effect . forkScoped (
989+ // A 404 can arrive before the thread exists, so a miss asks HTTP again while mounted.
990+ let httpMisses = 0 ;
991+ let defectRetries = 0 ;
992+ const backOffAfterDefect = Effect . suspend ( ( ) => {
993+ const retryDelay = subscriptionRetryDelay ( THREAD_SUBSCRIPTION_RETRY_DELAY , defectRetries ) ;
994+ defectRetries += 1 ;
995+ return setStreamError ( "Could not synchronize the thread." ) . pipe (
996+ Effect . andThen ( Effect . sleep ( retryDelay ) ) ,
997+ ) ;
998+ } ) ;
999+ const followThread = ( ) : Effect . Effect <
1000+ void ,
1001+ never ,
1002+ EnvironmentSupervisor . EnvironmentSupervisor
1003+ > =>
9831004 subscribeDynamic (
9841005 ORCHESTRATION_V2_WS_METHODS . subscribeThread ,
9851006 Effect . fn ( "EnvironmentThreadState.makeSubscribeInput" ) ( function * ( session ) {
9861007 let current = yield * SubscriptionRef . get ( state ) ;
987- // A prior definitive miss (or delete event) already cleared this thread.
988- // Park the subscription attempt without opening the socket so we do not
989- // retry forever against a known-missing id.
990- if ( current . status === "deleted" ) {
1008+ // A delete event already cleared this thread. Park without opening the socket.
1009+ if ( current . status === "deleted" && httpMisses === 0 ) {
9911010 return yield * Effect . never ;
9921011 }
9931012
@@ -1024,6 +1043,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10241043 yield * snapshotLoader . load ( prepared , threadId ) ;
10251044 switch ( httpResult . _tag ) {
10261045 case "present" : {
1046+ httpMisses = 0 ;
10271047 if ( canLoadHistory && httpResult . history !== undefined ) {
10281048 yield * Ref . set ( acceptsBoundedSocketSnapshots , true ) ;
10291049 }
@@ -1052,9 +1072,13 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10521072 break ;
10531073 }
10541074 case "missing" : {
1055- yield * setDeleted ( ) ;
1075+ if ( current . status !== "deleted" ) yield * setDeleted ( ) ;
10561076 // A 404 also means "not created yet". Only a delete event may outlive this mount.
10571077 if ( resumeCache ?. owner === owner ) resumeCache . snapshot = undefined ;
1078+ yield * Effect . sleep (
1079+ subscriptionRetryDelay ( THREAD_SUBSCRIPTION_RETRY_DELAY , httpMisses ++ ) ,
1080+ ) ;
1081+ yield * Queue . offer ( resubscribes , undefined ) ;
10581082 return yield * Effect . never ;
10591083 }
10601084 case "unavailable" : {
@@ -1085,22 +1109,49 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10851109 {
10861110 // A socket-only open that fails retries over HTTP, which tells a
10871111 // missing thread apart from a failure, before showing an error.
1088- onDefect : Effect . fn ( "EnvironmentThreadState.onDefect" ) ( function * ( ) {
1112+ onDefect : Effect . fn ( "EnvironmentThreadState.onDefect" ) ( function * ( cause ) {
10891113 if ( yield * failedSocketOnlyOpen ) {
10901114 yield * Ref . set ( socketOnlyOpen , false ) ;
1091- return yield * Queue . offer ( httpRetries , undefined ) ;
1115+ return yield * Queue . offer ( resubscribes , undefined ) ;
1116+ }
1117+ if ( ! Cause . hasDies ( cause ) ) {
1118+ return yield * setStreamError ( "Could not synchronize the thread." ) ;
10921119 }
1093- yield * setStreamError ( "Could not synchronize the thread." ) ;
1120+ yield * backOffAfterDefect ;
1121+ yield * Queue . offer ( resubscribes , undefined ) ;
10941122 } ) ,
10951123 onExpectedFailure : Effect . fn ( "EnvironmentThreadState.onExpectedFailure" ) ( function * ( cause ) {
10961124 const retryOverHttp = ! isAuthorizationFailure ( cause ) && ( yield * failedSocketOnlyOpen ) ;
10971125 if ( ! retryOverHttp ) yield * setStreamError ( formatThreadError ( cause ) ) ;
10981126 } ) ,
1099- retryExpectedFailureAfter : "250 millis" ,
1100- resubscribe : Stream . merge ( foregroundResubscriptions , Stream . fromQueue ( httpRetries ) ) ,
1127+ retryExpectedFailureAfter : THREAD_SUBSCRIPTION_RETRY_DELAY ,
1128+ resubscribe : Stream . merge ( foregroundResubscriptions , Stream . fromQueue ( resubscribes ) ) ,
11011129 } ,
1102- ) . pipe ( Stream . runForEachArray ( applyItems ) ) ,
1103- ) ;
1130+ ) . pipe (
1131+ Stream . runForEachArray ( ( items ) =>
1132+ applyItems ( items ) . pipe (
1133+ Effect . andThen (
1134+ Effect . sync ( ( ) => {
1135+ defectRetries = 0 ;
1136+ } ) ,
1137+ ) ,
1138+ ) ,
1139+ ) ,
1140+ // A defect while applying items ends the stream; resubscribe for a fresh snapshot.
1141+ Effect . catchCause ( ( cause ) =>
1142+ Cause . hasInterrupts ( cause )
1143+ ? Effect . interrupt
1144+ : Effect . logWarning ( "Thread subscription failed; resubscribing." , {
1145+ cause : Cause . pretty ( cause ) ,
1146+ environmentId,
1147+ threadId,
1148+ } ) . pipe (
1149+ Effect . andThen ( backOffAfterDefect ) ,
1150+ Effect . andThen ( Effect . suspend ( followThread ) ) ,
1151+ ) ,
1152+ ) ,
1153+ ) ;
1154+ yield * Effect . forkScoped ( followThread ( ) ) ;
11041155
11051156 return state ;
11061157} ) ;
0 commit comments