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 21, 11:00 UTC
fix(desktop): release cancelled Codex callback listeners during browser launch (#29734)
Co-authored-by: maria-rcks <maria@kuuro.net>
Romil committed
Nov 15, 2026 at 20:15 UTC
178570dcd3031f0e7e5c8720303a4af1f44d9ef2
4 files changed
+64
-31
apps/desktop/src/app/CodexAuthCallback.test.ts
+53
-26
@@ -37,18 +37,27 @@ function callback(authorizationUrl: string) {
37
}
38
39
describe("desktop Codex callback helper", () => {
40
- it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async () => {
40
+ it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async ({
41
+ onTestFinished,
42
+ }) => {
43
const authorizationUrl = request(await freePort());
44
const expected = callback(authorizationUrl);
43
- const received = await receiveCodexAuthCallback(authorizationUrl, async () => {
45
+ const opening = Promise.withResolvers<boolean>();
46
+ onTestFinished(() => opening.resolve(true));
47
+ const checkResponse = async () => {
48
const invalid = new URL(expected);
49
invalid.searchParams.set("state", "foreign");
50
expect((await fetch(invalid)).status).toBe(400);
51
const response = await fetch(expected);
52
expect(response.headers.get("cache-control")).toBe("no-store");
53
expect(await response.text()).not.toContain("test-code");
50
- return true;
54
+ };
55
+ let responseChecked!: Promise<void>;
56
+ const received = await receiveCodexAuthCallback(authorizationUrl, () => {
57
+ responseChecked = checkResponse();
58
+ return responseChecked.then(() => opening.promise);
59
});
60
+ await responseChecked;
61
expect(received).toBe(expected);
62
});
63
it("returns hosted web to the exact instance and environment without putting the code in its query", async () => {
@@ -61,34 +70,52 @@ describe("desktop Codex callback helper", () => {
70
instanceId: ProviderInstanceId.make("work"),
71
flowId: "flow-one",
72
};
73
+ const openBrowser = async () => {
74
+ const response = await fetch(expected, { redirect: "manual" });
75
+ expect(response.status).toBe(303);
76
+ const delivery = response.headers.get("location")!;
77
+ expect(new URL(delivery).searchParams.has("code")).toBe(false);
78
+ expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected);
79
+ expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl);
80
+ return true;
81
+ };
82
+ let opening!: Promise<boolean>;
83
await receiveCodexAuthCallback(
84
authorizationUrl,
66
- async () => {
67
- const response = await fetch(expected, { redirect: "manual" });
68
- expect(response.status).toBe(303);
69
- const delivery = response.headers.get("location")!;
70
- expect(new URL(delivery).searchParams.has("code")).toBe(false);
71
- expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected);
72
- expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl);
73
- return true;
74
- },
85
+ () => (opening = openBrowser()),
86
(url) => codexAuthDeliveryUrl(input, url),
87
);
88
+ await opening;
89
});
78
- it("cancels and releases its listener so exact-port reauthorization can run again", async () => {
79
- const authorizationUrl = request(await freePort());
80
- await expect(
81
- receiveCodexAuthCallback(authorizationUrl, async () => {
82
- cancelCodexAuthCallback(authorizationUrl);
83
- return true;
84
- }),
85
- ).rejects.toThrow("cancelled");
86
- expect(
87
- await receiveCodexAuthCallback(authorizationUrl, async () => {
88
- await fetch(callback(authorizationUrl));
89
- return true;
90
- }),
91
- ).toBe(callback(authorizationUrl));
90
+ it("cancels and releases its listener so exact-port reauthorization can run again", async ({
91
+ onTestFinished,
92
+ signal,
93
+ }) => {
94
+ for (const external of [false, true]) {
95
+ const authorizationUrl = request(await freePort());
96
+ const abort = new AbortController();
97
+ const opening = Promise.withResolvers<boolean>();
98
+ onTestFinished(() => opening.resolve(true));
99
+ await expect(
100
+ receiveCodexAuthCallback(
101
+ authorizationUrl,
102
+ async () => {
103
+ if (external) abort.abort();
104
+ else cancelCodexAuthCallback(authorizationUrl);
105
+ return opening.promise;
106
+ },
107
+ undefined,
108
+ abort.signal,
109
+ ),
110
+ ).rejects.toThrow("cancelled");
111
+ if (signal.aborted) return;
112
+ expect(
113
+ await receiveCodexAuthCallback(authorizationUrl, async () => {
114
+ await fetch(callback(authorizationUrl));
115
+ return true;
116
+ }),
117
+ ).toBe(callback(authorizationUrl));
118
+ }
119
});
120
it("allows two accounts to complete independently", async () => {
121
const a = request(await freePort(), "a".repeat(43));
apps/desktop/src/app/DesktopClerk.ts
+2
-1
@@ -178,11 +178,12 @@ export const make = Effect.gen(function* () {
178
Effect.gen(function* () {
179
yield* electronApp.whenReady;
180
yield* Effect.tryPromise({
181
- try: () =>
181
+ try: (signal) =>
182
receiveCodexAuthCallback(
183
request.authorizationUrl,
184
(url) => runPromise(shell.openExternal(url)),
185
(callbackUrl) => codexAuthDeliveryUrl(request, callbackUrl),
186
+ signal,
187
),
188
catch: () =>
189
new CodexAuthCallbackError({
apps/desktop/src/ipc/methods/providerAuth.ts
+7
-2
@@ -23,8 +23,13 @@ export const receiveProviderAuthCallback = DesktopIpc.makeIpcMethod({
23
const context = yield* Effect.context<ElectronShell.ElectronShell>();
24
const runPromise = Effect.runPromiseWith(context);
25
const callbackUrl = yield* Effect.tryPromise({
26
- try: () =>
27
- receiveCodexAuthCallback(authorizationUrl, (url) => runPromise(shell.openExternal(url))),
26
+ try: (signal) =>
27
+ receiveCodexAuthCallback(
28
+ authorizationUrl,
29
+ (url) => runPromise(shell.openExternal(url)),
30
+ undefined,
31
+ signal,
32
+ ),
33
catch: () =>
34
new CodexAuthCallbackError({
35
detail:
packages/shared/src/codexAuthCallback.ts
+2
-2
@@ -86,8 +86,8 @@ export async function receiveCodexAuthCallback(
86
server.listen(Number(new URL(request.redirectUri).port), "127.0.0.1", resolve);
87
});
88
if (abort.signal.aborted) throw new Error("Sign-in cancelled on this computer.");
89
- if (!(await openBrowser(request.authorizationUrl)))
90
- throw new Error("Could not open your sign-in browser.");
89
+ const opened = await Promise.race([openBrowser(request.authorizationUrl), callback.promise]);
90
+ if (!opened) throw new Error("Could not open your sign-in browser.");
91
return await callback.promise;
92
} finally {
93
clearTimeout(timer);