diff --git a/src/main/rate-limits/codex-fetcher.test.ts b/src/main/rate-limits/codex-fetcher.test.ts index 77da7f8d3..b3f3591c0 100644 --- a/src/main/rate-limits/codex-fetcher.test.ts +++ b/src/main/rate-limits/codex-fetcher.test.ts @@ -127,6 +127,28 @@ describe('fetchCodexRateLimits', () => { expect(ptySpawnMock).not.toHaveBeenCalled() }) + it('removes RPC listeners when the app-server timeout settles', async () => { + const rpcChild = makeRpcChild() + childSpawnMock.mockReturnValue(rpcChild) + + const resultPromise = fetchCodexRateLimits({ allowPtyFallback: false }) + await vi.advanceTimersByTimeAsync(10_000) + + await expect(resultPromise).resolves.toMatchObject({ + provider: 'codex', + session: null, + weekly: null, + status: 'error', + error: 'RPC timeout' + }) + expect(rpcChild.kill).toHaveBeenCalledTimes(1) + expect(rpcChild.stdout.listenerCount('data')).toBe(0) + expect(rpcChild.stderr.listenerCount('data')).toBe(0) + expect(rpcChild.listenerCount('error')).toBe(0) + expect(rpcChild.listenerCount('close')).toBe(0) + expect(ptySpawnMock).not.toHaveBeenCalled() + }) + it('normalizes Codex RPC remaining-minute windows to fixed display durations', async () => { const rpcChild = makeRpcChild() childSpawnMock.mockReturnValue(rpcChild) diff --git a/src/main/rate-limits/codex-fetcher.ts b/src/main/rate-limits/codex-fetcher.ts index d46da8c40..f7c89f2de 100644 --- a/src/main/rate-limits/codex-fetcher.ts +++ b/src/main/rate-limits/codex-fetcher.ts @@ -160,19 +160,43 @@ async function fetchViaRpc(options?: FetchCodexRateLimitsOptions): Promise { - if (!resolved) { - resolved = true + let timeout: ReturnType | null = null + + function cleanupListeners(): void { + if (timeout) { + clearTimeout(timeout) + timeout = null + } + child.stdout.off('data', onStdoutData) + child.stderr.off('data', onStderrData) + child.off('error', onError) + child.off('close', onClose) + } + + function settle(result: ProviderRateLimits, options?: { kill?: boolean }): void { + if (resolved) { + return + } + resolved = true + cleanupListeners() + if (options?.kill) { child.kill() - resolve({ + } + resolve(result) + } + + timeout = setTimeout(() => { + settle( + { provider: 'codex', session: null, weekly: null, updatedAt: Date.now(), error: 'RPC timeout', status: 'error' - }) - } + }, + { kill: true } + ) }, rpcTimeoutMs) function sendRpc(method: string, params?: unknown): number { @@ -196,7 +220,7 @@ async function fetchViaRpc(options?: FetchCodexRateLimitsOptions): Promise { + function onStdoutData(chunk: Buffer): void { buffer += chunk.toString() // JSON-RPC messages are newline-delimited @@ -228,19 +252,19 @@ async function fetchViaRpc(options?: FetchCodexRateLimitsOptions): Promise { + function onStderrData(chunk: Buffer): void { stderr += chunk.toString() // Why: this background poll only needs recent failure context for hints. if (stderr.length > MAX_DIAGNOSTIC_OUTPUT_LENGTH) { stderr = stderr.slice(-MAX_DIAGNOSTIC_OUTPUT_LENGTH) } - }) + } - child.on('error', (err) => { - if (!resolved) { - resolved = true - clearTimeout(timeout) - const isEnoent = (err as NodeJS.ErrnoException).code === 'ENOENT' - const isBareCommand = codexCommand === 'codex' - resolve({ - provider: 'codex', - session: null, - weekly: null, - updatedAt: Date.now(), - error: isEnoent - ? isBareCommand - ? 'Codex CLI not found' - : 'Codex CLI found but could not run — Node.js may not be in your PATH' - : withMacTailscaleDnsHint(err.message, stderr), - status: isEnoent && isBareCommand ? 'unavailable' : 'error' - }) - } - }) + function onError(err: Error): void { + const isEnoent = (err as NodeJS.ErrnoException).code === 'ENOENT' + const isBareCommand = codexCommand === 'codex' + settle({ + provider: 'codex', + session: null, + weekly: null, + updatedAt: Date.now(), + error: isEnoent + ? isBareCommand + ? 'Codex CLI not found' + : 'Codex CLI found but could not run — Node.js may not be in your PATH' + : withMacTailscaleDnsHint(err.message, stderr), + status: isEnoent && isBareCommand ? 'unavailable' : 'error' + }) + } - child.on('close', () => { - if (!resolved) { - resolved = true - clearTimeout(timeout) - resolve({ - provider: 'codex', - session: null, - weekly: null, - updatedAt: Date.now(), - error: withMacTailscaleDnsHint('RPC process exited unexpectedly', stderr), - status: 'error' - }) - } - }) + function onClose(): void { + settle({ + provider: 'codex', + session: null, + weekly: null, + updatedAt: Date.now(), + error: withMacTailscaleDnsHint('RPC process exited unexpectedly', stderr), + status: 'error' + }) + } + + child.stdout.on('data', onStdoutData) + child.stderr.on('data', onStderrData) + child.on('error', onError) + child.on('close', onClose) }) }