Skip to content

fix(loop): take over an empty state lock instead of refusing for ten minutes - #809

Merged
huichen merged 1 commit into
mainfrom
fix/loop-lock-atomic
Sep 24, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/loop-lock-atomic

Conversation

@huichen

@huichen huichen commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

The failure

CI caught this in four_workers_with_relative_disjoint_scopes_claim_without_force:

ACTIVE_GOAL_STATE.md.lock exists without a pid and is not stale (mtime ...);
refusing takeover until it ages past 600s

acquire_active_state_lock publishes the lock with create_new and writes the pid
immediately after. Between those two syscalls the file exists and is empty — and an
empty lock was refused until it aged past ten minutes. Four workers claiming at
once meant one of them regularly landed in that microsecond window. Measured at
~2% per race with no artificial delay, and the loss was not a retry but a hard
error that lasted ten minutes while the lock stayed empty.

It reproduced on the first CI run I watched, which is why I looked rather than
reran: the job had 11 consecutive green runs before mine, and my diff touches zero
files under orchestration/.

Three things were wrong

1. A ten-minute wait where milliseconds decide. An empty lock means one of
exactly two things: another process is between create and pid-write (microseconds),
or one died there. Waiting milliseconds separates them; waiting ten minutes does
not. An empty lock is now polled for EMPTY_LOCK_WAIT (250 ms) and then taken over
as abandoned. Garbage content is classified the same way, so a permissions oddity
cannot wedge the projection either.

Taking over a live-but-slow writer is harmless by construction: the file this lock
guards is a regenerable projection of the ledger, so the worst case is writing
it twice with the same content. The ledger's own advisory lock is the real
concurrency guard — the code comment said as much already.

2. A committed change was reported as failed. Twelve call sites propagated
sync_compat with ?, but every caller has already committed its ledger change by
then. So a lock failure reported a completed operation as failed — and the natural
retry answered "another agent holds a live lease" for a todo this very process had
claimed. sync_compat is now infallible and warns; cmd_lease and cmd_backfill
already ignored the result, so this makes that the rule instead of a per-call
choice.

3. The inconsistency that let it hide. The same projection was ? in twelve
places and let _ = in five.

Verified against a real CLI

An empty lock planted (the CI state), then a live holder:

state before after
fresh empty lock exit 1, todo claimed anyway claimed by w1 ✔, exit 0
live holder exit 1, add committed anyway ⚠ could not refresh … + added ✔, exit 0

The "claimed anyway" column is the worse half of the old behaviour: the ledger had
the claim, the command said it failed, and the message users saw pointed somewhere
else entirely.

concurrent_acquires_all_succeed is the regression test — four threads racing,
150 rounds. It fails 5/5 on the old behaviour and passes 5/5 on the new, in
2.5 s. The other three new tests pin the classification (abandoned empty, fresh
empty with a zero grace period, garbage content).

Scope

Two files, orchestration/loop/src/{compat,console}.rs. EMPTY_LOCK_STALE_AFTER
is removed rather than kept with a new value: its name asserted the behaviour that
was the bug. future-cli is the crate's only dependent and still compiles.

Verified: future-loop 414 lib plus 27/31/3/2/20/9/7/4/4/10 integration tests, and
future-cli 693, with fmt and clippy clean.

…minutes

CI caught this in `four_workers_with_relative_disjoint_scopes_claim_without_force`:

    ACTIVE_GOAL_STATE.md.lock exists without a pid and is not stale (mtime ...);
    refusing takeover until it ages past 600s

`acquire_active_state_lock` publishes the lock with `create_new` and writes the
pid immediately after. Between those two syscalls the file exists and is empty,
and an empty lock was refused until it aged past ten minutes. Four workers
claiming at once meant one of them regularly landed in that microsecond window:
measured at ~2% per race with no artificial delay, and the loss was not a retry
but a hard error lasting ten minutes while the lock stayed empty.

Three things were wrong, and this fixes all three.

**A short wait, not a ten-minute one.** The empty state means one of exactly two
things: another process is between create and pid-write (microseconds), or one
died there. Waiting milliseconds separates them; waiting ten minutes does not.
An empty lock is now polled for `EMPTY_LOCK_WAIT` (250 ms) and then taken over as
abandoned. Taking over a live-but-slow writer is harmless by construction — the
file this lock guards is a regenerable projection of the ledger, so the worst
case is writing it twice with the same content, and the ledger's own advisory
lock is the real concurrency guard. Garbage content is classified the same way,
so a permissions oddity cannot wedge the projection either.

**A committed change is no longer reported as failed.** Twelve call sites
propagated `sync_compat` with `?`, but every caller has already committed its
ledger change by then — the projection is derived. A lock failure therefore
reported a completed operation as failed, and the natural retry answered
"another agent holds a live lease" for a todo this very process had claimed.
`sync_compat` is now infallible and warns; `cmd_lease` and `cmd_backfill` already
ignored the result, so this makes that the rule rather than a per-call choice.
The command still prints its success line and exits 0.

**The inconsistency that let it hide.** The same projection was `?` in twelve
places and `let _ =` in five. One rule with a warning beats two conventions.

Verified end to end against a real CLI, with an empty lock planted (the CI
state) and then with a live holder:

| state | before | after |
|---|---|---|
| fresh empty lock | exit 1, todo claimed anyway | `claimed by w1 ✔`, exit 0 |
| live holder | exit 1, add committed anyway | `⚠ could not refresh …` + `added ✔`, exit 0 |

`concurrent_acquires_all_succeed` is the regression test: four threads racing to
acquire, 150 rounds. It fails 5/5 on the old behaviour and passes 5/5 on the new,
in 2.5 s.

Machine-derived, not guessed: the empty-lock window was reproduced at ~2% of
500 races with zero added delay, and the field behaviour was reproduced by
planting an empty lock and running `todo claim` / `todo add` against an isolated
`FUTURE_LOOP_ROOT`.

Verified: future-loop 414 lib + 27/31/3/2/20/9/7/4/4/10 integration tests, and
future-cli 693 (its only dependent), fmt and clippy clean.
@huichen
huichen enabled auto-merge (squash) September 24, 2026 01:33
@huichen
huichen merged commit cb26a45 into main Sep 24, 2026
12 checks passed
@huichen
huichen deleted the fix/loop-lock-atomic branch September 24, 2026 01:37
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