Skip to content

fix: read-only lock probe can make a concurrent Codex writer acquisition fail #55

Closed

Description

@abruption

What happened

The OS lock probe used by list and inspect acquires the thread writer lock exclusively (flock LOCK_EX|LOCK_NB) and releases it immediately when the lock is free. During that short window, a Codex process trying to take the same lock with a non-blocking try_lock fails with WouldBlock.

Upstream Codex acquires thread-writer-locks/.coordination.lock before try_lock() on the per-thread lock and treats WouldBlock as "thread … already has an active writer" (or skips publication in try_acquire_for_publication). codex-unlock never takes .coordination.lock, so its probe is not serialized with Codex's acquisition.

inspectThread probes twice per thread and list probes every lock file.

Reproduction: running dist/lock.js probeLock in a loop against an acquirer that mimics Codex (open, then flock exnb) for 3 seconds produced 4,436 WouldBlock failures out of 155,390 acquisition attempts.

What you expected

Read-only diagnostics (list, inspect) never cause a Codex writer acquisition to fail. The README and docs/cli-reference.md describe these commands as read-only without mentioning this interference.

Environment

  • codex-unlock 0.4.0 at 6227e6c
  • macOS 27.0 arm64, Node.js 22.14.0

Source evidence

  • Probe acquires and releases the exclusive lock:

    codex-unlock/src/lock.ts

    Lines 86 to 123 in 6227e6c

    try {
    const noFollow = constants.O_NOFOLLOW ?? 0;
    fd = openSync(path, constants.O_RDWR | noFollow);
    const opened = publicSnapshot(fstatSync(fd));
    if (
    opened.device !== before.snapshot.device ||
    opened.inode !== before.snapshot.inode
    ) {
    return {
    status: "unknown",
    method: "flock_exclusive_nonblocking",
    error: "lock file changed while it was opened",
    };
    }
    try {
    flockSync(fd, "exnb");
    } catch (error) {
    if (isContentionError(error)) {
    return { status: "held", method: "flock_exclusive_nonblocking" };
    }
    return {
    status: "unknown",
    method: "flock_exclusive_nonblocking",
    error: errorText(error),
    };
    }
    try {
    flockSync(fd, "un");
    } catch (error) {
    return {
    status: "unknown",
    method: "flock_exclusive_nonblocking",
    error: `probe acquired the lock but unlock failed: ${errorText(error)}`,
    };
    }
    return { status: "free", method: "flock_exclusive_nonblocking" };
  • Two probes per inspected thread:
    Promise.resolve(probeLock(lockPath)),
    and
    Promise.resolve(probeLock(lockPath)),
  • list inspects every thread:
    const sessions = await Promise.all(threadIds.map((id) => inspectThread(id, options)));

Acceptance criteria

  • Probing is performed while holding <CODEX_HOME>/thread-writer-locks/.coordination.lock (taken non-blocking; contention classified as unknown, file not created if absent), so Codex's blocking coordination acquire waits instead of failing; or the probe uses a mechanism that cannot make a concurrent try_lock fail.
  • A regression test runs a probe loop concurrently with an acquirer that follows the Codex coordination protocol and observes zero WouldBlock failures.
  • If the interference cannot be removed, it is documented as a limitation of list/inspect.

Scope and limitations

  • Reliability issue only; the window per probe is microseconds and requires Codex to resume or publish a thread at the same moment.
  • Codex behavior was taken from the upstream rollout writer-lock source; the reproduction used a mimicking acquirer, not a real Codex process.

Checked

  • No existing issue covers probe interference with Codex acquisition.

Activity

  1. added
    bug
    on Sep 28, 2026
  2. added this to the v0.4.1 milestone on Sep 28, 2026
  3. abruption commented on Sep 28, 2026

    @abruption
    OwnerAuthor

    Accepted design: coordinated probe with fail-closed guard absence

    Decision recorded on 2026-09-29. This resolves the remaining design choice for #55; implementation and validation are still pending.

    When a thread lock exists but its native .coordination.lock is absent, the result is unknown. Never fall back to an uncoordinated thread-lock probe and never create the missing coordination file. ENOENT does not establish a safe legacy protocol: deletion, incomplete copies, and concurrent coordinator creation are also possible.

    Probe contract

    • Use the existing .coordination.lock beside the thread lock in the canonical thread-writer-locks directory. Open without creation/truncation, with no-follow and nonblocking flags. Validate current-user ownership, regular-file type, link count of one, and path/descriptor device and inode identity before and after probing. Do not require mode 0600 or reject group-write permission alone; do not repair ownership or permissions.
    • Acquire the native guard with nonblocking exclusive flock. Retry contention outside the critical section, using a monotonic deadline and a total acquisition budget of approximately 100 ms, capped by any remaining caller deadline. Exhaustion, unsafe paths, permission errors, or identity changes return unknown.
    • Hold the guard for one synchronous thread-lock probe only. No await, external command, transcript scan, stability delay, signal, or exit wait occurs while it is held. Release the thread descriptor and native guard through finally; release/close failures must not produce a successful observation.
    • Preserve lock order: codex-unlock operation lease → native coordination guard → thread probe. Never acquire the operation lease while holding the native guard.
    • Apply the same contract to inspection, the final pre-signal check, and post-signal polling. A busy guard during owner shutdown is temporarily unavailable evidence: allow another observation within the existing termination budget rather than immediately declaring termination failure.
    • Preserve verified absence semantics when the home and lock-directory observations establish that the thread lock is absent. The stricter unknown policy concerns an existing thread lock without a usable coordinator.
    • Keep method: "flock_exclusive_nonblocking" and JSON Schema v1. Any additional guard evidence must be optional and backward compatible. Native lock and coordination files must not be created, deleted, truncated, or rewritten by the probe.

    Guarantees and limitations

    For writers that follow the verified Codex coordination protocol, the probe must not cause a thread try_lock to return WouldBlock. The native guard can briefly delay acquisition/publication/cleanup. If the diagnostic process is stopped or paused while holding it, that delay can extend until execution resumes or the process exits; the acquisition retry budget is not a guarantee about time spent paused while holding a lock.

    Document diagnostics as non-mutating to file contents, ownership, permissions, and modification times, while acknowledging temporary OS locks. Do not promise access-time invariance. Uncoordinated/custom writers are outside the zero-interference guarantee. Update CLI help and applicable README, security, CLI-reference, and safety-contract text consistently.

    Passive OS queries such as Linux /proc/locks or a verified Darwin query remain follow-up work. Missing or incomplete query evidence must not be interpreted as a free lock.

    Implementation and acceptance gates

    Pinned protocol evidence

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