Repository navigation
fix(loop): take over an empty state lock instead of refusing for ten minutes - #809
Merged
Merged
Conversation
…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.
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.
The failure
CI caught this in
four_workers_with_relative_disjoint_scopes_claim_without_force:acquire_active_state_lockpublishes the lock withcreate_newand writes the pidimmediately 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 overas 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_compatwith?, but every caller has already committed its ledger change bythen. 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_compatis now infallible and warns;cmd_leaseandcmd_backfillalready 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 twelveplaces and
let _ =in five.Verified against a real CLI
An empty lock planted (the CI state), then a live holder:
claimed by w1 ✔, exit 0⚠ could not refresh …+added ✔, exit 0The "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_succeedis 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_AFTERis removed rather than kept with a new value: its name asserted the behaviour that
was the bug.
future-cliis the crate's only dependent and still compiles.Verified:
future-loop414 lib plus 27/31/3/2/20/9/7/4/4/10 integration tests, andfuture-cli693, with fmt and clippy clean.