Repository navigation
fix(codex): survive lingering Windows codex login processes in add-account - #9430
Merged
brennanb2025 merged 1 commit intoJul 19, 2026
Conversation
…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.
8 tasks
brennanb2025
merged commit Jul 19, 2026
84996bd
into
brennanb2025/codex-ma-integration-validation
2 checks passed
1 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
A native-Windows validation run of the Codex multi-account add flow (
codexAccounts.add({runtime:'host'})) proved thatcodex login(spawned byrunCodexLogin) 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, sodoAddAccount's error-cleanup path (safeRemoveManagedHome→rmSync) died with ENOTEMPTY (errno 41, syscall rm) and left an orphanedcodex-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.jsonappeared 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)auth.jsonin 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 withtaskkill /pid <pid> /t /f(plainchild.kill()only reaches the direct child — e.g. thecmd.exeshim forcodex.cmd— and leaves descendants holding handles).auth.jsonexists is treated as a successful login, so the kill never surfaces as a spurious failure.win32; the timeout path still useschild.kill()there).2. Windows-lock-resilient removal (
safeRemoveManagedHome, defensive)rmSynccalls now passmaxRetries: 8, retryDelay: 150(mirroring theWINDOWS_RM_MAX_RETRIESpolicy inlocal-worktree-filesystem.tsand thechromium-cookie-snapshot.tsprecedent), so a transient handle retries instead of throwing. This also fixes the secondary orphaned-home bug.writeFileAtomically→renameFileWithWindowsRetry; no change needed there.Tooling fix
config/scripts/run-codex-real-account-validation.mjsplaced its disposable root underos.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_PARENTfor 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~/.codexis unchanged and still enforced.Tests
Added:
service.test.ts: force-kills a lingering Windows login tree onceauth.jsonexists (fakewin32platform, asserts thetaskkill /pid <pid> /t /finvocation and that the forced non-zero exit resolves the login as success).service.test.ts:safeRemoveManagedHomepassesmaxRetries/retryDelaytormSyncand removes the home.service.test.ts: a persistently locked home (ENOTEMPTY) no longer throws out ofsafeRemoveManagedHome(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 passnpx vitest run --config config/vitest.config.ts config/scripts/run-codex-real-account-validation.test.ts— 4/4 passtsc --noEmit -p config/tsconfig.node.json— cleanoxlinton changed files +check:max-lines-ratchet— cleanWindows 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-parentoutside the profile):codexAccounts.add({runtime:'host'})now succeeds without any external process killer (previously deterministic ENOTEMPTY failure)CODEX_HOMEwhile adds/switches happencodex-accounts\<id>\homedirectory--temp-parent D:\some\dir(orORCA_CODEX_VALIDATION_TEMP_PARENT) and the tripwire stays cleanCaveats
taskkill /ton 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 atauth.jsontime) is fully covered.auth.jsonappears; if codex needs longer than that to finish post-auth writes on very slow machines, it gets force-killed — validation showed killing atauth.jsontime is safe (add succeeds end-to-end).taskkillcan't reach; the proven bug is host-runtime).Do not merge until the Windows checklist above passes.