Repository navigation
Description
What happened
The unlock operation lease is created under a root chosen from the caller's environment: $XDG_RUNTIME_DIR/codex-unlock when XDG_RUNTIME_DIR is set, otherwise os.tmpdir()/codex-unlock-<uid>. os.tmpdir() follows TMPDIR.
Two unlock invocations for the same Codex home and thread therefore use different lease directories when their environments differ, do not see each other's lease, and can both pass revalidation and send SIGTERM to the same owner. Common environment splits:
- macOS: an interactive terminal has a per-user
TMPDIRunder/var/folders/..., while cron, some SSH sessions, andsudo -ushells do not setTMPDIRand fall back to/tmp. - Linux: one session has
XDG_RUNTIME_DIR(systemd user session) and another does not.
Reproduction on macOS with the repository test fixture (test/helpers/owner-fixture.mjs, a same-user dummy owner holding a real advisory lock and a transcript ending in task_complete):
- Hold
acquireUnlockLease(codexHome, threadId)in a process with the defaultTMPDIR. - Run
codex-unlock unlock <thread> --codex-home <fixture> --jsonwithTMPDIRremoved from the environment.
lease (terminal env): acquired
second unlock with TMPDIR unset: exit 0, outcome "unlocked", signalSent "SIGTERM", reasons []
owner signal: SIGTERM
With the same environment the second attempt is correctly refused with concurrent_unlock_in_progress.
What you expected
One lease per canonical Codex home and thread regardless of the caller's TMPDIR/XDG_RUNTIME_DIR, so concurrent attempts from differently configured shells are serialized as documented in the safety race matrix ("Two CLI unlocks inspect the same safe owner → exactly one SIGTERM").
Environment
codex-unlock0.4.0 at6227e6c- macOS 27.0 arm64, Node.js 22.14.0
Source evidence
- Lease root depends on
XDG_RUNTIME_DIR/tmpdir():codex-unlock/src/coordination.ts
Lines 36 to 45 in 6227e6c
function runtimeRoot(uid: number, override?: string): string { const configured = override ?? process.env.XDG_RUNTIME_DIR; if (configured !== undefined) { if (!isAbsolute(configured) || !privateDirectory(configured, uid)) { throw new Error("runtime_directory_is_not_private"); } return join(configured, "codex-unlock"); } return join(tmpdir(), `codex-unlock-${uid}`); } - Lease path is derived from that root:
codex-unlock/src/coordination.ts
Lines 65 to 81 in 6227e6c
root = runtimeRoot(uid, runtimeDirectory); mkdirSync(root, { mode: 0o700, recursive: true }); if (!privateDirectory(root, uid)) { return { status: "unknown", reason: "coordination_directory_is_not_private" }; } chmodSync(root, 0o700); canonicalHome = realpathSync(codexHome); } catch { return { status: "unknown", reason: "coordination_directory_unavailable" }; } const key = createHash("sha256") .update(canonicalHome) .update("\0") .update(threadId) .digest("hex"); const path = join(root, `${key}.lock`); - Documented invariant:
codex-unlock/docs/safety-race-matrix.md
Line 14 in 6227e6c
| Two CLI unlocks inspect the same safe owner | One private advisory operation lease per canonical home/thread | Exactly one SIGTERM; competitor refused | `concurrent CLI unlock attempts send at most one SIGTERM` |
Acceptance criteria
- The lease location does not depend on environment variables that differ between shells of the same user (for example, a fixed per-user location derived from the account rather than
TMPDIR, or a lease inside a same-user directory tied to the Codex home), while keeping the existing private-directory checks. - A regression test runs two unlock attempts with different
TMPDIR/XDG_RUNTIME_DIRvalues and asserts at most oneSIGTERM.
Scope and limitations
- Requires two concurrent same-user unlock attempts for the same thread from differently configured environments; the result is a duplicate
SIGTERMto an owner that already passed every safety check, not a signal to an unrelated process. - Related but distinct: security: case-variant --codex-home paths bypass unlock serialization and send duplicate SIGTERM #53 (case-variant
--codex-homespellings) and fix: predictable shared tmp coordination fallback allows unlock denial on Linux #60 (another user pre-creating the shared/tmpfallback on Linux). The same/tmpfallback is also reached on macOS whenTMPDIRis unset.
Checked
- security: serialize concurrent unlock attempts #34 / fix: serialize concurrent unlock attempts #35 introduced the lease; security: case-variant --codex-home paths bypass unlock serialization and send duplicate SIGTERM #53 and fix: predictable shared tmp coordination fallback allows unlock denial on Linux #60 cover other ways to reach a different or unusable lease, not environment-dependent roots.
Activity
fix: key unlock leases by native lock identity