Skip to content

Commit f9825c3

Browse files
committed
fix(client-runtime): space token handoffs at least the refresh skew apart
A renewed token that is already inside the refresh skew was replaced at once, and so was every token after it. A server issuing short tokens would start a loop of renewals and overlapping sockets. After a handoff, the next one now waits at least the skew; a token too short for that ends its session at expiry and reconnects with backoff.
1 parent 4b92848 commit f9825c3

3 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ operation. The server closes a socket when the session it authenticated with
4141
expires, so the supervisor opens a replacement on a renewed DPoP token shortly
4242
before that and moves the session to it without leaving the connected state or
4343
changing the transport generation. The previous socket stays open until its
44-
token expires, for requests already running on it.
44+
token expires, for requests already running on it. Handoffs on one connection
45+
are at least the refresh skew apart, so a session whose token lasts less than
46+
that ends at expiry and reconnects instead.
4547

4648
Session listings must retain unrevoked connected sessions after credential expiry
4749
so an open connection does not disappear from connection management. This does

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,67 @@ describe("EnvironmentSupervisor", () => {
16191619
}).pipe(Effect.provide(TestClock.layer())),
16201620
);
16211621

1622+
it.effect("does not hand off again when renewed tokens expire within the refresh skew", () =>
1623+
Effect.gen(function* () {
1624+
const harness = yield* makeHarness({
1625+
prepare: (attempt) =>
1626+
Clock.currentTimeMillis.pipe(
1627+
Effect.map((now) => ({
1628+
...PREPARED_CONNECTION,
1629+
target: RELAY_TARGET,
1630+
httpAuthorization: {
1631+
_tag: "Dpop" as const,
1632+
accessToken: `access-token-${attempt}`,
1633+
expiresAtEpochMs: now + 30_000,
1634+
},
1635+
})),
1636+
),
1637+
});
1638+
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1639+
initiallyDesired: true,
1640+
}).pipe(Effect.provide(harness.dependencies));
1641+
1642+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1643+
yield* TestClock.adjust("29 seconds");
1644+
// The first token is already inside the skew, so it is replaced once. The
1645+
// renewed token is too short for another handoff, so its session ends at
1646+
// expiry and reconnects instead of looping.
1647+
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
1648+
}).pipe(Effect.provide(TestClock.layer())),
1649+
);
1650+
1651+
it.effect("waits at least the refresh skew between handoffs", () =>
1652+
Effect.gen(function* () {
1653+
const harness = yield* makeHarness({
1654+
prepare: (attempt) =>
1655+
Clock.currentTimeMillis.pipe(
1656+
Effect.map((now) => ({
1657+
...PREPARED_CONNECTION,
1658+
target: RELAY_TARGET,
1659+
httpAuthorization: {
1660+
_tag: "Dpop" as const,
1661+
accessToken: `access-token-${attempt}`,
1662+
expiresAtEpochMs: now + 90_000,
1663+
},
1664+
})),
1665+
),
1666+
});
1667+
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1668+
initiallyDesired: true,
1669+
}).pipe(Effect.provide(harness.dependencies));
1670+
1671+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1672+
yield* TestClock.adjust("5 minutes");
1673+
// Handoffs at 30 s, a skew before the first token expires, then every
1674+
// 60 s: 90, 150, 210 and 270 s.
1675+
expect(yield* Ref.get(harness.sessionCount)).toBe(6);
1676+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1677+
phase: "connected",
1678+
generation: 1,
1679+
});
1680+
}).pipe(Effect.provide(TestClock.layer())),
1681+
);
1682+
16221683
it.effect("keeps the session while a failed replacement retries before expiry", () =>
16231684
Effect.gen(function* () {
16241685
const harness = yield* makeHarness({

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,22 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
640640
// The server closes a socket when the DPoP access token it opened with
641641
// expires, so a replacement opens on a renewed token first. Starting inside
642642
// the refresh skew makes the authorization service mint a new token instead
643-
// of returning the one that is about to expire.
643+
// of returning the one that is about to expire. After a handoff, the next one
644+
// waits at least the skew, so tokens issued for less than that can't start
645+
// a loop; their sessions end at expiry and reconnect with backoff instead.
644646
const replaceBeforeExpiry = Effect.fnUntraced(function* (
645647
lease: ConnectionDriver.EnvironmentConnectionLease,
648+
minimumDelayMs: number,
646649
) {
647650
const authorization = lease.prepared.httpAuthorization;
648651
if (authorization?._tag !== "Dpop") return yield* Effect.never;
649652
const untilExpiry = Clock.currentTimeMillis.pipe(
650653
Effect.map((now) => Math.max(0, authorization.expiresAtEpochMs - now)),
651654
);
652-
yield* Effect.sleep(Math.max(0, (yield* untilExpiry) - DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS));
655+
const lifetimeMs = yield* untilExpiry;
656+
const delayMs = Math.max(minimumDelayMs, lifetimeMs - DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS);
657+
if (delayMs >= lifetimeMs) return yield* Effect.never;
658+
yield* Effect.sleep(delayMs);
653659
const attemptScope = yield* Scope.Scope;
654660
const routeId = connectionRouteId(lease.prepared.target);
655661
for (let failureCount = 0; ; failureCount += 1) {
@@ -827,7 +833,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
827833
retryAt: null,
828834
});
829835

830-
const awaitConnectedEvent = (current: typeof active) => {
836+
const awaitConnectedEvent = (current: typeof active, minimumReplacementDelayMs: number) => {
831837
const traced = (error: ConnectionAttemptError): TracedAttemptFailure => ({
832838
error,
833839
attemptSpan: current.attemptSpan,
@@ -838,12 +844,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
838844
Effect.map((resetRetry) => ({ _tag: "Ended" as const, resetRetry })),
839845
Effect.mapError(traced),
840846
),
841-
replaceBeforeExpiry(current.lease).pipe(
847+
replaceBeforeExpiry(current.lease, minimumReplacementDelayMs).pipe(
842848
Effect.map((replacement) => ({ _tag: "Replaced" as const, replacement })),
843849
),
844850
]).pipe(exitUnlessInterrupted);
845851
};
846-
let connectedExit = yield* awaitConnectedEvent(active);
852+
let connectedExit = yield* awaitConnectedEvent(active, 0);
847853
while (Exit.isSuccess(connectedExit) && connectedExit.value._tag === "Replaced") {
848854
// A disconnect signal lost to the race must not keep the replacement.
849855
if (!(yield* Ref.get(intent)).desired) break;
@@ -863,7 +869,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
863869
);
864870
active = { attemptSpan: active.attemptSpan, lease: replacement.lease };
865871
activeScope = replacement.scope;
866-
connectedExit = yield* awaitConnectedEvent(active);
872+
connectedExit = yield* awaitConnectedEvent(active, DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS);
867873
}
868874
const connectedForMs = (yield* Clock.currentTimeMillis) - connectedAt;
869875
if (Exit.isSuccess(connectedExit)) {

0 commit comments

Comments
 (0)