Skip to content

Commit ebf9c37

Browse files
t3dotggclaude
andcommitted
fix(client): ask the relay again after three cached ticket timeouts in a row
A relay endpoint that accepts the ticket request and never answers kept the cached token until it expired, up to an hour. Count cached-token ticket timeouts per environment and token. The third timeout in a row drops the token and mints a new one through the relay. A success resets the count. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 857c263 commit ebf9c37

2 files changed

Lines changed: 73 additions & 7 deletions

File tree

‎packages/client-runtime/src/authorization/layer.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,57 @@ describe("RemoteEnvironmentAuthorization", () => {
514514
}),
515515
);
516516

517+
it.effect("asks the relay again after three cached ticket timeouts in a row", () =>
518+
Effect.gen(function* () {
519+
const harness = yield* makeHarness({
520+
initialToken: persistedToken(),
521+
responses: [
522+
STALLED,
523+
STALLED,
524+
websocketTicket("cached-ticket"),
525+
STALLED,
526+
STALLED,
527+
STALLED,
528+
Response.json(DESCRIPTOR),
529+
accessToken("replacement-access-token"),
530+
websocketTicket("replacement-ticket"),
531+
],
532+
});
533+
534+
const [timeouts, replaced] = yield* Effect.gen(function* () {
535+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
536+
const authorize = () => remote.authorizeDpop({ expectedEnvironmentId: ENVIRONMENT_ID });
537+
// Runs an attempt whose cached ticket request stalls until its 10 s budget runs out.
538+
const stalled = <A, E>(attempt: Effect.Effect<A, E>) =>
539+
Effect.gen(function* () {
540+
const pending = yield* Effect.forkChild(attempt);
541+
yield* Queue.take(harness.stalls);
542+
yield* TestClock.adjust("10 seconds");
543+
return yield* Fiber.join(pending);
544+
});
545+
const timeouts = [
546+
yield* stalled(Effect.flip(authorize())),
547+
yield* stalled(Effect.flip(authorize())),
548+
];
549+
// A success resets the count, so two more timeouts keep the cached token.
550+
yield* authorize();
551+
timeouts.push(
552+
yield* stalled(Effect.flip(authorize())),
553+
yield* stalled(Effect.flip(authorize())),
554+
);
555+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(0);
556+
return [timeouts, yield* stalled(authorize())] as const;
557+
}).pipe(Effect.provide(harness.layer));
558+
559+
for (const failure of timeouts) {
560+
expect(failure).toMatchObject({ _tag: "ConnectionTransientError", reason: "timeout" });
561+
}
562+
expect(replaced.socketUrl).toContain("wsTicket=replacement-ticket");
563+
expect(replaced.httpAuthorization).toMatchObject({ accessToken: "replacement-access-token" });
564+
expect(yield* Ref.get(harness.bootstrapCalls)).toBe(1);
565+
}),
566+
);
567+
517568
it.effect("keeps a newly refreshed token when its websocket ticket request times out", () =>
518569
Effect.gen(function* () {
519570
const harness = yield* makeHarness({

‎packages/client-runtime/src/authorization/service.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
8181

8282
const BEARER_DESCRIPTOR_CACHE_TTL_MS = 10_000;
8383
const DPOP_AUTHORIZATION_TIMEOUT_MS = 30_000;
84+
/** Cached-token ticket timeouts in a row before the client asks the relay again. */
85+
const CACHED_TICKET_TIMEOUT_LIMIT = 3;
8486

8587
function mapDpopSocketError(error: RemoteEnvironmentAuthError | ConnectionAttemptError) {
8688
return error._tag === "ConnectionTransientError" || error._tag === "ConnectionBlockedError"
@@ -111,6 +113,10 @@ export const make = Effect.gen(function* () {
111113
EnvironmentId,
112114
{ readonly accessToken: string; readonly identity: ClientCapabilities.CloudSessionIdentity }
113115
>();
116+
const cachedTicketTimeouts = new Map<
117+
EnvironmentId,
118+
{ readonly accessToken: string; readonly count: number }
119+
>();
114120
const pendingTokens = new Map<
115121
EnvironmentId,
116122
{
@@ -465,18 +471,27 @@ export const make = Effect.gen(function* () {
465471
if (selected.fromCache) {
466472
const cachedSocket = yield* createDpopSocketUrl(selected.token).pipe(Effect.result);
467473
if (Result.isSuccess(cachedSocket)) {
474+
cachedTicketTimeouts.delete(input.expectedEnvironmentId);
468475
yield* assertSession(selected.identity);
469476
return { ...httpAuthorization(selected.token), socketUrl: cachedSocket.success };
470477
}
471-
// A timeout means a slow server, not a bad token. Keep the token so the next attempt
472-
// reuses it instead of minting a new credential. The cost: an endpoint that accepts the
473-
// request and never answers is only replaced when the token expires.
474-
if (
475-
cachedSocket.failure._tag === "ConnectionBlockedError" ||
476-
cachedSocket.failure._tag === "RemoteEnvironmentAuthTimeoutError"
477-
) {
478+
if (cachedSocket.failure._tag === "ConnectionBlockedError") {
478479
return yield* mapDpopSocketError(cachedSocket.failure);
479480
}
481+
// A timeout means a slow server, not a bad token. Keep the token so the next attempt
482+
// reuses it instead of minting a new credential. After several timeouts in a row the
483+
// endpoint may be gone, so drop the token and ask the relay again. The mint runs in the
484+
// service scope, so it still lands if the setup deadline ends this attempt first.
485+
if (cachedSocket.failure._tag === "RemoteEnvironmentAuthTimeoutError") {
486+
const accessToken = selected.token.accessToken;
487+
const previous = cachedTicketTimeouts.get(input.expectedEnvironmentId);
488+
const count = previous?.accessToken === accessToken ? previous.count + 1 : 1;
489+
if (count < CACHED_TICKET_TIMEOUT_LIMIT) {
490+
cachedTicketTimeouts.set(input.expectedEnvironmentId, { accessToken, count });
491+
return yield* mapDpopSocketError(cachedSocket.failure);
492+
}
493+
cachedTicketTimeouts.delete(input.expectedEnvironmentId);
494+
}
480495
selected = yield* getDpopToken({
481496
...input,
482497
rejectedAccessToken: selected.token.accessToken,

0 commit comments

Comments
 (0)