The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 13:00 UTC
fix(server): stalled OAuth token requests no longer block credential calls (#19321)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 14, 2026 at 01:38 UTC
f372c0061f7678da94853110da06e821a3af7c83
2 files changed
+76
-2
apps/server/src/cloud/CliTokenManager.test.ts
+68
@@ -1,5 +1,6 @@
1
import * as NodeServices from "@effect/platform-node/NodeServices";
2
import { assert, it } from "@effect/vitest";
3
+import * as Cause from "effect/Cause";
4
import * as ConfigProvider from "effect/ConfigProvider";
5
import * as Deferred from "effect/Deferred";
6
import * as Duration from "effect/Duration";
@@ -15,6 +16,8 @@ import * as TestClock from "effect/testing/TestClock";
16
import * as HttpClient from "effect/http/HttpClient";
17
import * as HttpClientResponse from "effect/http/HttpClientResponse";
18
19
+import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
20
+import * as ExternalLauncher from "../process/externalLauncher.ts";
21
import * as CliTokenManager from "./CliTokenManager.ts";
22
23
// pk_test_<base64 of "clerk.example.test$">
@@ -164,6 +167,57 @@ const layerDeviceFlow = (server: DeviceFlowServer) =>
167
const tokenRequests = (requests: ReadonlyArray<RecordedTokenRequest>) =>
168
requests.filter((request) => request.url.endsWith("/oauth/token"));
169
170
+// The endpoint sends 200 headers, then the JSON body never finishes.
171
+const layerStalledBody = Layer.succeed(
172
+ HttpClient.HttpClient,
173
+ HttpClient.make((request) =>
174
+ Effect.succeed(
175
+ HttpClientResponse.fromWeb(
176
+ request,
177
+ new Response(
178
+ new ReadableStream({
179
+ start: (controller) => controller.enqueue(new TextEncoder().encode("{")),
180
+ }),
181
+ { status: 200, headers: { "content-type": "application/json" } },
182
+ ),
183
+ ),
184
+ ),
185
+ ),
186
+);
187
+
188
+const expiredStoredToken = new TextEncoder().encode(
189
+ JSON.stringify({ accessToken: "expired", refreshToken: "refresh-token-1", expiresAtEpochMs: 0 }),
190
+);
191
+
192
+it.effect("a stalled token refresh fails and releases the credential lock", () =>
193
+ Effect.gen(function* () {
194
+ const manager = yield* CliTokenManager.CloudCliTokenManager;
195
+ const refresh = yield* manager.getExisting.pipe(Effect.flip, Effect.forkChild);
196
+ const hasCredential = yield* manager.hasCredential.pipe(Effect.forkChild);
197
+
198
+ yield* TestClock.adjust(Duration.seconds(10));
199
+
200
+ assert.instanceOf(yield* Fiber.join(refresh), CliTokenManager.CloudCliCredentialRefreshError);
201
+ assert.isTrue(yield* Fiber.join(hasCredential));
202
+ }).pipe(
203
+ Effect.provide(
204
+ CliTokenManager.layer.pipe(
205
+ Layer.provide(
206
+ Layer.mergeAll(
207
+ layerStalledBody,
208
+ Layer.mock(ServerSecretStore.ServerSecretStore)({
209
+ get: () => Effect.succeedSome(expiredStoredToken),
210
+ }),
211
+ Layer.mock(ExternalLauncher.ExternalLauncher)({}),
212
+ NodeServices.layer,
213
+ ),
214
+ ),
215
+ ),
216
+ ),
217
+ provideTestEnv,
218
+ ),
219
+);
220
+
221
it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) => {
222
it.effect("requests a device code, shows it, and polls until Clerk grants the token", () =>
223
Effect.gen(function* () {
@@ -322,4 +376,18 @@ it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) =>
376
assert.isTrue(isAuthorizationError(result));
377
}),
378
);
379
+
380
+ it.effect("fails instead of hanging when the device authorization response stalls", () =>
381
+ Effect.gen(function* () {
382
+ const fiber = yield* CliTokenManager.deviceAuthorizationLogin(() => Effect.void).pipe(
383
+ Effect.provide(layerStalledBody),
384
+ provideTestEnv,
385
+ Effect.flip,
386
+ Effect.forkChild,
387
+ );
388
+ yield* TestClock.adjust(Duration.seconds(10));
389
+
390
+ assert.isTrue(Cause.isTimeoutError(yield* Fiber.join(fiber)));
391
+ }),
392
+ );
393
});
apps/server/src/cloud/CliTokenManager.ts
+8
-2
@@ -44,6 +44,10 @@ const DEVICE_CODE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
44
const DEVICE_AUTHORIZATION_DEFAULT_INTERVAL = Duration.seconds(5);
45
// RFC 8628 §3.5: a slow_down response means "add 5 seconds to the interval".
46
const DEVICE_AUTHORIZATION_SLOW_DOWN_INCREMENT = Duration.seconds(5);
47
+// Bounds one token or device-authorization request, including reading the
48
+// body. A stalled refresh holds the manager's semaphore and blocks every
49
+// other credential call.
50
+const OAUTH_REQUEST_TIMEOUT = Duration.seconds(10);
51
const boldTerminalText = (value: string): string => `\u001b[1m${value}\u001b[22m`;
52
53
function formatLoopbackAuthorizationPrompt(authorizationUrl: string): string {
@@ -290,11 +294,12 @@ const exchangeToken = Effect.fn("cloud.cli_token.exchange")(function* (
294
params: Record<string, string>,
295
) {
296
const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
293
- const response = yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(
297
+ return yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(
298
HttpClientRequest.bodyUrlParams(params),
299
httpClient.execute,
300
+ Effect.flatMap((response) => readTokenResponse(response, params)),
301
+ Effect.timeout(OAUTH_REQUEST_TIMEOUT),
302
);
297
- return yield* readTokenResponse(response, params);
303
});
304
305
const makePkceRequest = Effect.gen(function* () {
@@ -393,6 +398,7 @@ export const deviceAuthorizationLogin = Effect.fn("cloud.cli_token.device_author
398
}),
399
httpClient.execute,
400
Effect.flatMap(HttpClientResponse.schemaBodyJson(DeviceAuthorizationResponse)),
401
+ Effect.timeout(OAUTH_REQUEST_TIMEOUT),
402
);
403
// Clerk's advertised lifetime and interval are authoritative.
404
const expiresIn = Duration.seconds(authorization.expires_in);