Repository navigation
fix(client-runtime): recover subscriptions that stop on a healthy connection #24400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
cc36b74
383a473
b162219
2b15e0c
86f3131
9446bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,7 @@ export const isRpcClientError = Schema.is(RpcClientError.RpcClientError); | |
| const isEnvironmentAuthorizationError = Schema.is(EnvironmentAuthorizationError); | ||
|
|
||
| const MAX_EXPECTED_FAILURE_RETRY_DELAY_MS = 30_000; | ||
| const STREAM_FAILURE_RETRY_DELAY = Duration.seconds(1); | ||
|
|
||
| export type EnvironmentRpcInput<TTag extends EnvironmentRpcTag> = Parameters<RpcMethod<TTag>>[0]; | ||
|
|
||
|
|
@@ -324,6 +325,19 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>( | |
| : Effect.void, | ||
| ), | ||
| Stream.catchCause((cause) => { | ||
| const retryAfter = (base: Duration.Input) => { | ||
| const retryDelay = Duration.millis( | ||
| Math.min( | ||
| Duration.toMillis(base) * 2 ** expectedFailureRetries, | ||
| MAX_EXPECTED_FAILURE_RETRY_DELAY_MS, | ||
| ), | ||
| ); | ||
| expectedFailureRetries += 1; | ||
| return Stream.fromEffect(Effect.sleep(retryDelay)).pipe( | ||
| Stream.drain, | ||
| Stream.concat(subscribeToSession()), | ||
| ); | ||
| }; | ||
| const hasOnlyExpectedFailures = | ||
| cause.reasons.length > 0 && | ||
| cause.reasons.every((reason) => reason._tag === "Fail"); | ||
|
|
@@ -332,17 +346,34 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>( | |
| cause.reasons.every( | ||
| (reason) => reason._tag === "Fail" && isRpcClientError(reason.error), | ||
| ); | ||
| if (isTransportFailure) { | ||
| // The RPC client dies with a SchemaError when one stream chunk fails to | ||
| // decode, ending only that request while the socket stays open. | ||
| const isChunkDecodeDefect = | ||
| cause.reasons.length > 0 && | ||
| cause.reasons.every( | ||
| (reason) => reason._tag === "Die" && Schema.isSchemaError(reason.defect), | ||
| ); | ||
| // Both end one stream while the session can stay healthy, so nothing else | ||
| // would replace it. A dead session is replaced by the supervisor, which | ||
| // interrupts this retry through switchMap. | ||
| if (isTransportFailure || isChunkDecodeDefect) { | ||
| return Stream.fromEffect( | ||
| Effect.logWarning( | ||
| "Durable RPC subscription lost its transport; waiting for the next session.", | ||
| "Durable RPC subscription stream ended; resubscribing.", | ||
| { | ||
| cause: Cause.pretty(cause), | ||
| method: tag, | ||
| environmentId: supervisor.target.environmentId, | ||
| }, | ||
| ), | ||
| ).pipe(Stream.drain); | ||
| ).pipe( | ||
| Stream.drain, | ||
| Stream.concat( | ||
| retryAfter( | ||
| options?.retryExpectedFailureAfter ?? STREAM_FAILURE_RETRY_DELAY, | ||
|
Comment on lines
+391
to
+392
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔎 Supported by static analysis
Keep backoff across repeated failures after the first value.
|
||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| if (hasOnlyExpectedFailures && options?.onExpectedFailure !== undefined) { | ||
| const handled = Stream.fromEffect(options.onExpectedFailure(cause)).pipe( | ||
|
|
@@ -358,19 +389,8 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>( | |
| ) { | ||
| return handled; | ||
| } | ||
| const retryDelay = Duration.millis( | ||
| Math.min( | ||
| Duration.toMillis(options.retryExpectedFailureAfter) * | ||
| 2 ** expectedFailureRetries, | ||
| MAX_EXPECTED_FAILURE_RETRY_DELAY_MS, | ||
| ), | ||
| ); | ||
| expectedFailureRetries += 1; | ||
| return handled.pipe( | ||
| Stream.concat( | ||
| Stream.fromEffect(Effect.sleep(retryDelay)).pipe(Stream.drain), | ||
| ), | ||
| Stream.concat(subscribeToSession()), | ||
| Stream.concat(retryAfter(options.retryExpectedFailureAfter)), | ||
| ); | ||
| } | ||
| return Stream.failCause(cause); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: pingdotgg/t3code
Length of output: 37330
Script executed:
Repository: pingdotgg/t3code
Length of output: 45671
Script executed:
Repository: pingdotgg/t3code
Length of output: 41987
Restrict same-session retry to durable subscriptions.
subscribeDynamicMappedapplies this retry branch to everyEnvironmentSubscriptionRpcTag.threadFindProgressiveconsumes the finitesearchThreadStreamand completes only after receiving a result wherecomplete !== false. A persistent transport or decode failure instead enters the retry loop, so the finite search can remain pending indefinitely instead of failing.Make same-session retry opt-in for durable subscriptions, and exclude
searchThreadStream.