Skip to content

fix: environment-dependent coordination root lets concurrent unlocks bypass serialization #64

Closed

Description

@abruption

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 TMPDIR under /var/folders/..., while cron, some SSH sessions, and sudo -u shells do not set TMPDIR and 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):

  1. Hold acquireUnlockLease(codexHome, threadId) in a process with the default TMPDIR.
  2. Run codex-unlock unlock <thread> --codex-home <fixture> --json with TMPDIR removed 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-unlock 0.4.0 at 6227e6c
  • macOS 27.0 arm64, Node.js 22.14.0

Source evidence

  • Lease root depends on XDG_RUNTIME_DIR / tmpdir():
    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:
    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:
    | 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_DIR values and asserts at most one SIGTERM.

Scope and limitations

Checked

Activity

  1. added this to the v0.4.1 milestone on Sep 28, 2026
  2. added
    bug
    on Sep 28, 2026
  3. added a commit that references this issue on Sep 29, 2026
    1bd2f39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions