Skip to content

Commit a70b80f

Browse files
fix(client-runtime): a bearer replaced mid-attempt retries instead of sticking blocked
Review follow-up. The registry only woke a supervisor already blocked on authentication when its platform bearer changed. An attempt in flight had already read the old bearer; when it then failed authentication the supervisor blocked, and later polls stored the same new bearer so nothing woke it again. A changed bearer now also retries a supervisor that is connecting or backing off. A connected session keeps its socket. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 49cd484 commit a70b80f

2 files changed

Lines changed: 75 additions & 6 deletions

File tree

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
158158
) => Effect.Effect<void, Persistence.ConnectionPersistenceError>;
159159
readonly checkRoute?: (route: ConnectionRoute) => RouteCheck;
160160
readonly prepareRoute?: (target: ConnectionTarget) => ConnectionBlockedError | undefined;
161+
/** Runs before preparing a route; may suspend to hold an attempt in flight. */
162+
readonly beforePrepareRoute?: (
163+
target: ConnectionTarget,
164+
) => Effect.Effect<ConnectionBlockedError | undefined>;
161165
readonly initialDisabled?: ReadonlyArray<EnvironmentId>;
162166
},
163167
) {
@@ -385,6 +389,10 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
385389
target,
386390
};
387391
if (options?.prepareError) return yield* options.prepareError;
392+
const heldError = options?.beforePrepareRoute
393+
? yield* options.beforePrepareRoute(target)
394+
: undefined;
395+
if (heldError !== undefined) return yield* heldError;
388396
const routeError = options?.prepareRoute?.(target);
389397
if (routeError !== undefined) return yield* routeError;
390398
yield* Ref.update(connectedRoutes, (current) => [...current, connectionRouteId(target)]);
@@ -1314,6 +1322,64 @@ describe("EnvironmentRegistry", () => {
13141322
}),
13151323
);
13161324

1325+
it.effect("a bearer replaced while an attempt is in flight retries with the new one", () =>
1326+
Effect.gen(function* () {
1327+
const attemptStarted = yield* Deferred.make<void>();
1328+
const releaseFirstAttempt = yield* Deferred.make<void>();
1329+
const attempts = yield* Ref.make(0);
1330+
const harness = yield* makeHarness([], [], [], {
1331+
// The first attempt read the old bearer; it stalls, then fails
1332+
// authentication after the bearer has already been replaced.
1333+
beforePrepareRoute: (target) =>
1334+
target._tag !== "BearerConnectionTarget"
1335+
? Effect.succeed(undefined)
1336+
: Ref.updateAndGet(attempts, (count) => count + 1).pipe(
1337+
Effect.flatMap((count) =>
1338+
count === 1
1339+
? Deferred.succeed(attemptStarted, undefined).pipe(
1340+
Effect.andThen(Deferred.await(releaseFirstAttempt)),
1341+
Effect.as(
1342+
new ConnectionBlockedError({
1343+
reason: "authentication",
1344+
detail: "revoked",
1345+
}),
1346+
),
1347+
)
1348+
: Effect.succeed(undefined),
1349+
),
1350+
),
1351+
});
1352+
1353+
yield* Effect.gen(function* () {
1354+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1355+
const registration = (token: string) =>
1356+
new BearerConnectionRegistration({
1357+
target: BEARER_TARGET,
1358+
profile: BEARER_PROFILE,
1359+
credential: new BearerConnectionCredential({ token }),
1360+
});
1361+
yield* registry.reconcilePlatform([registration("old-bearer")]);
1362+
yield* Deferred.await(attemptStarted).pipe(Effect.timeout("1 second"));
1363+
1364+
// A live state subscription, as the UI holds, keeps the runtime in place
1365+
// so the stalled attempt's failure lands on the same supervisor.
1366+
yield* registry
1367+
.stateChanges(BEARER_TARGET.environmentId)
1368+
.pipe(Stream.runDrain, Effect.forkScoped({ startImmediately: true }));
1369+
yield* Effect.yieldNow;
1370+
yield* registry.reconcilePlatform([registration("new-bearer")]);
1371+
yield* Deferred.succeed(releaseFirstAttempt, undefined);
1372+
1373+
yield* awaitConnectionState(
1374+
registry,
1375+
BEARER_TARGET.environmentId,
1376+
(state) => state.phase === "connected",
1377+
);
1378+
expect(yield* Ref.get(attempts)).toBeGreaterThanOrEqual(2);
1379+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
1380+
}),
1381+
);
1382+
13171383
it.effect("starts a newly paired bearer environment without re-reading its profile", () =>
13181384
Effect.gen(function* () {
13191385
const harness = yield* makeHarness([]);

‎packages/client-runtime/src/connection/registry.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,17 +696,20 @@ export const make = Effect.gen(function* () {
696696
yield* installEntryLocked(entry, { retainEquivalentRuntime: true });
697697

698698
// The catalog entry carries no credential, so a fresh bearer keeps
699-
// the equivalent runtime. A supervisor that stopped on the old
700-
// bearer's authentication failure would otherwise wait for a manual
701-
// retry, so wake it with the new one.
699+
// the equivalent runtime. Anything not yet connected may be using the
700+
// old bearer: an attempt in flight would fail with it and then wait
701+
// blocked for a manual retry. Retry with the new one instead. A
702+
// connected session keeps its socket.
702703
if (bearerReplaced) {
703704
const scope = (yield* SubscriptionRef.get(serviceScopes)).get(target.environmentId);
704705
if (scope !== undefined) {
705706
const state = yield* SubscriptionRef.get(scope.supervisor.state);
706707
if (
707-
state.phase === "blocked" &&
708-
state.lastFailure?._tag === "ConnectionBlockedError" &&
709-
state.lastFailure.reason === "authentication"
708+
state.phase === "connecting" ||
709+
state.phase === "backoff" ||
710+
(state.phase === "blocked" &&
711+
state.lastFailure?._tag === "ConnectionBlockedError" &&
712+
state.lastFailure.reason === "authentication")
710713
) {
711714
yield* scope.supervisor.retryNow;
712715
}

0 commit comments

Comments
 (0)