Skip to content

fix(codex): survive lingering Windows codex login processes in add-account - #9430

Merged
brennanb2025 merged 1 commit into
brennanb2025/codex-ma-integration-validationfrom
brennanb2025/codex-ma-win-login-handle-fix
Jul 19, 2026
Merged

brennanb2025 merged 1 commit into
brennanb2025/codex-ma-integration-validationfrom
brennanb2025/codex-ma-win-login-handle-fix

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Root cause

A native-Windows validation run of the Codex multi-account add flow (codexAccounts.add({runtime:'host'})) proved that codex login (spawned by runCodexLogin) leaves a lingering process tree holding OS file handles on the per-account managed home — specifically <home>\log\codex-login.log. On Windows, held handles make directory removal fail, so doAddAccount's error-cleanup path (safeRemoveManagedHome → rmSync) died with ENOTEMPTY (errno 41, syscall rm) and left an orphaned codex-accounts\<id>\home.

Proof this is the only bug: driving the same add while a background killer terminated the lingering codex processes the instant auth.json appeared made the entire add succeed (account persisted, active, email resolved, per-account home + auth correct). Without the kill it failed deterministically (5–6 repros). Process-lifecycle/file-lock bug, not a logic flaw.

Fix (two prongs)

1. Release the handle before post-login file ops (runCodexLogin, primary)

  • On Windows (host runtime only), poll for auth.json in the managed home. Once it exists, give the login tree a short grace period (5s) to exit on its own; if it lingers, force-kill the whole tree with taskkill /pid <pid> /t /f (plain child.kill() only reaches the direct child — e.g. the cmd.exe shim for codex.cmd — and leaves descendants holding handles).
  • The forced non-zero exit after auth.json exists is treated as a successful login, so the kill never surfaces as a spurious failure.
  • The existing 120s login timeout now also kills the full tree on Windows instead of only the direct child, so a timed-out login can't strand handle-holding descendants either.
  • macOS/Linux: no behavioral change (the watcher is gated on win32; the timeout path still uses child.kill() there).

2. Windows-lock-resilient removal (safeRemoveManagedHome, defensive)

  • All managed-home rmSync calls now pass maxRetries: 8, retryDelay: 150 (mirroring the WINDOWS_RM_MAX_RETRIES policy in local-worktree-filesystem.ts and the chromium-cookie-snapshot.ts precedent), so a transient handle retries instead of throwing. This also fixes the secondary orphaned-home bug.
  • A removal that still fails after retries is warned and swallowed instead of thrown, so cleanup can no longer mask the original add/login error with an ENOTEMPTY stack.
  • Post-login config writes were already lock-resilient via writeFileAtomically → renameFileWithWindowsRetry; no change needed there.

Tooling fix

config/scripts/run-codex-real-account-validation.mjs placed its disposable root under os.tmpdir(), which on Windows is inside %USERPROFILE%, so its own real-home-protection guard always aborted. It now honors --temp-parent <dir> / ORCA_CODEX_VALIDATION_TEMP_PARENT for the disposable root, and fails fast with an actionable message (before creating anything) when the temp parent is inside the primary home. The guard that refuses the real home / real ~/.codex is unchanged and still enforced.

Tests

Added:

  • service.test.ts: force-kills a lingering Windows login tree once auth.json exists (fake win32 platform, asserts the taskkill /pid <pid> /t /f invocation and that the forced non-zero exit resolves the login as success).
  • service.test.ts: safeRemoveManagedHome passes maxRetries/retryDelay to rmSync and removes the home.
  • service.test.ts: a persistently locked home (ENOTEMPTY) no longer throws out of safeRemoveManagedHome (warns instead).
  • run-codex-real-account-validation.test.ts: temp-parent override is honored; a temp parent inside the primary home is refused with the override hint.

Ran (macOS):

  • npx vitest run --config config/vitest.config.ts src/main/codex-accounts/service.test.ts — 38/38 pass
  • npx vitest run --config config/vitest.config.ts config/scripts/run-codex-real-account-validation.test.ts — 4/4 pass
  • tsc --noEmit -p config/tsconfig.node.json — clean
  • oxlint on changed files + check:max-lines-ratchet — clean

Windows re-test needed (blocking before merge)

This was implemented on macOS; the bug is Windows-only and no end-to-end Windows verification has been done on this branch. On a native Windows machine (validation harness with --temp-parent outside the profile):

  • Repro run: codexAccounts.add({runtime:'host'}) now succeeds without any external process killer (previously deterministic ENOTEMPTY failure)
  • Two distinct accounts: add account A, then account B (A ≠ B); both persist with correct per-account homes, auth.json, and emails
  • Concurrent panes: panes running under different accounts get the right per-account CODEX_HOME while adds/switches happen
  • Orphaned-home cleanup: a deliberately failed add (e.g. cancel login) leaves no orphaned codex-accounts\<id>\home directory
  • Failure-path UX: when login genuinely fails, the surfaced error is the login error, not an ENOTEMPTY cleanup error
  • Validation harness itself starts on Windows with --temp-parent D:\some\dir (or ORCA_CODEX_VALIDATION_TEMP_PARENT) and the tripwire stays clean

Caveats

  • The tree kill only works while the direct login child is still alive. If codex's main process exits 0 but leaves a fully detached descendant holding the log handle, taskkill /t on the dead parent can't reach it; the rmSync retries (~1.2s of backoff) plus warn-instead-of-throw are the backstop for that shape. The proven repro (lingering tree killable at auth.json time) is fully covered.
  • Grace period is 5s after auth.json appears; if codex needs longer than that to finish post-auth writes on very slow machines, it gets force-killed — validation showed killing at auth.json time is safe (add succeeds end-to-end).
  • WSL-runtime logins are intentionally untouched (handles there are held by Linux-side processes that taskkill can't reach; the proven bug is host-runtime).

Do not merge until the Windows checklist above passes.

…count

On Windows, codex login can keep running (with descendants) after it has
written auth.json, holding OS handles on the per-account managed home
(log/codex-login.log). That made doAddAccount's post-login cleanup fail
with ENOTEMPTY (rmSync) and left an orphaned codex-accounts/<id>/home.

- runCodexLogin now watches for auth.json on Windows and force-kills the
  login process tree (taskkill /t) if it lingers past a short grace
  period; the forced exit is treated as a successful login. The 120s
  timeout path also kills the whole tree instead of only the direct
  child. macOS/Linux behavior is unchanged.
- safeRemoveManagedHome now removes homes with rmSync maxRetries /
  retryDelay (mirroring the local-worktree-filesystem Windows policy)
  and no longer lets a cleanup failure mask the original add error.
- run-codex-real-account-validation.mjs accepts --temp-parent /
  ORCA_CODEX_VALIDATION_TEMP_PARENT so the disposable root can live
  outside %USERPROFILE% on Windows, and fails with an actionable message
  before creating anything when the temp parent is inside the primary
  home. The real-home guard is unchanged.
@brennanb2025
brennanb2025 merged commit 84996bd into brennanb2025/codex-ma-integration-validation Jul 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant