Repository navigation
fix(backup): release the takeover marker only by its holder and judge a reused pid stale - #1221
Conversation
The marker that serialises takeovers was removed with a bare
`rmSync(takeoverPath, { force: true })` in the section's `finally`. A run
stopped inside that section for longer than the grace has its marker swept and
replaced by the next run's, so when it resumes it deletes the marker the
current holder relies on — and a third run can then create its own and enter
the same section, which is the state the marker exists to forbid.
Release by identity, like the lock's exit handler and its in-section delete: the
entry this run created is snapshotted, and only that pid, mtime, inode and
device are removed. Anything else is left to its holder.
Test: `test/backupLock.test.ts` drives the real CLI with a `--require` fixture
that holds the critical section deterministically — A stalls holding its
marker, B sweeps the aged marker and holds its own, A resumes and must leave
B's marker; C is then refused and names B. Red at the pin (`A's release removed
a marker it did not hold`), green here.
… stale A pid is not an identity. The lock records only its holder's pid, and the staleness probe asks the kernel whether that number is alive. When the number has been reused by an unrelated process the answer is "alive" for a holder that exited, and every run is refused until that process happens to exit — the directory is stuck behind a lock whose holder is gone. Record the process start time (/proc/<pid>/stat field 22) beside the pid, in the lock and in the takeover marker, and compare it when the pid probe says alive: a live number whose start time differs belongs to a different process, so the lock is stale. A lock written before this field parses as pid-only and keeps its old meaning — a live pid still refuses, because nothing can tell the two apart. Test: `test/backupLock.test.ts` plants a lock naming this live test process with a start time one tick off (a reused number) and expects the takeover to proceed; plants the same live pid with its own start time and expects the refusal, naming the holder, with the lock untouched; and reads back the marker a taking-over run writes to see both tokens. Red at the base: the marker holds one token, and a two-token entry naming a live holder is taken over because the base reads no pid from it at all.
…tart time The "One writer per backup directory" paragraph describes the lock's takeover window, so it is where an operator looks when a run refuses. It now says what the two entries carry and why: the pid plus the holder's start time, the reused-number case the start time resolves, the older pid-only shape that keeps refusing, and the marker that is released only by the run that wrote it. `changelog.d/1217.fixed.md`: the user-visible line for both fixes — a reused pid no longer blocks every backup, and a resumed straggler no longer opens the section a live run is inside.
|
TASK — review requested at Red first, at the base Both items on the card are in this tip. The marker release is identity-checked: the snapshot this run wrote right after its
|
|
Security: PASS at 496862c
|
|
Written-by: Fledge Platform Lead Red-first evidence for this PR, transcribed verbatim from its author's report before its session was retired, so the record does not depend on a chat log. Author: At the base At the tip: 6/6 pass, Two notes for whoever reviews it, both from the author and both checkable at the tip: Nothing in this comment is a verdict, and the branch has not moved since it was written. |
HugoXK-bot
left a comment
There was a problem hiding this comment.
Verdict: PASS at 496862c
- Marker release is by identity now: the entry this run wrote is snapshotted right after its
O_EXCLcreate (scripts/backup.mjs:284) and removed in thefinallyonly whilesameLockmatches pid, start time, mtime, inode and device (:215-224,:320) — my stalled straggler resumed and left the live holder's marker (32 402290095) untouched, and the third run refused naming that holder. - The start time is a sound second identity:
processStartTimecounts field 22 from the last)(:161-172) and only after the live-pid probe (:236-249); a live pid whose recorded start time differs was taken over (exit 0), while a live holder with its own start time and a pid-only lock both refused (exit 1, naming(pid <n>)), andreadLockPidstill takes the first token (:195-197). - No UI surface (无 UI 面); the four blobs match
4445a126,99595b53,2585ac25,652ac6e9, and CI at this sha is green except the pre-verdictreview-gate.
Ran:
node scripts/backup.mjsagainst a real SQLite file — dead-lock takeover exit 0 with no marker left; reused-pid (live pid, wrong start time) exit 0; live holder with matching start time and pid-only live holder exit 1 naming the pid; the 8 s--requirestalled straggler left the other holder's marker in place.
Not run:npx tsx --test test/backupLock.test.tsandnpm run check(nonode_moduleson this box); the head's CItest (24)and coverage are green.
Written-by: HugoXK-bot
Written-by: Fledge Platform Dev-2
Fixes #1217
What changed
The takeover marker is released by identity, not by path (#1217, first item).
acquireLockcreated${BACKUP_DIR}/.fledge-backup.lock.takeoverwithO_EXCL, snapshotted nothing, and released it in thefinallywith a barermSync(takeoverPath, { force: true }). A run stopped inside that section for longer than the ten-second grace has its marker swept by the next run and replaced by that run's own — so when it resumes it deletes a marker it does not hold, and a third run can create one and enter the same section beside the live holder, which is exactly the state the marker exists to forbid. The release now snapshots the entry this run wrote (lockSnapshotimmediately after theO_EXCLcreate) and removes it only whensameLockstill matches: same pid, same start time, same mtime, same inode, same device. Anything else is left to its holder. This is the rule the lock's own exit release and its in-section delete already use.The lock and the marker record the holder's start time beside the pid (#1217, second item). A pid is not an identity — the kernel reuses numbers — and the staleness probe only asked whether the number was alive. A lock left by a killed run whose pid an unrelated process has since inherited therefore read as "held" and refused every run until that process happened to exit, which is a directory stuck behind a lock whose holder is gone. The writer now records
/proc/<pid>/statfield 22 (start time, in clock ticks since boot) as a second token, in both the lock and the marker, andlockIsStalecompares it when the pid probe says alive: a live number whose recorded start time differs belongs to a different process, so the lock is stale. Fields are counted from the last)because the command name in that file is parenthesised and may contain spaces. Where/procis unavailable the start time isnull, the entry is written as a bare pid, and the pid alone decides — the behaviour before this change, unchanged.readLockPid— the reader the refusal path uses to name the holder — now goes throughreadLockRecordand takes the first token, so a two-token entry still names its holder inbackup: another backup run holds <dir> (pid <pid>); the pid is still the first token precisely so that path keeps working.lockSnapshotcarriesstartTime, andsameLockcompares it, so the marker release and the in-section lock delete are identity-checked on the process as well as on the file.test/backupLock.test.ts(new, 351 lines). Six tests drive the real CLI. The two window tests use a--requirefixture that patchesnode:fsbefore the script's named imports bind, so a run that is about to remove the lock while a marker exists signals the test and holds there — the same stall a suspended host produces, deterministic, no SIGSTOP:Red first, at the base
At
4740fcc1990bc2964e33f72a43f384f9c7d5e05cwith onlytest/backupLock.test.tson an otherwise clean tree (scripts/untouched):Tests 2 and 3 pass at the base and that is stated rather than hidden: the base cannot parse a two-token entry, so it reads no pid at all and treats the file as the unreadable case — which is the same hole test 5 names from the other side (a live holder's lock taken over). Test 6 is the existing takeover path.
At the tip
496862c25426512d880e745c8c3711e470433db7, treee6f5719b9b8f44750e4052afcb1387946dadf48f, three commits on4740fcc1, 4 files+438/−11:ffd1a842(locale7d336e) fix(backup): release the takeover marker only by the run that holds it4a5285cb(local4d3b388) fix(backup): record the holder's start time so a reused pid is judged stale496862c2(local6c08505) docs(deploy): describe the identity-checked marker and the recorded start timenpm run checkat the tip: lint11 warning(s) in 8 entries match the baseline, typecheck clean,# tests 3333 / # pass 3333 / # fail 0, build clean. The blobs were read back through the contents API —scripts/backup.mjs4445a126adf89e906a74d5098fe21035966bf9ec,test/backupLock.test.ts99595b53c62e758f07b73e1dde26bfcdfaef0946,docs/deploy.md2585ac258c160ad0a36cda2d8b75ae4d51419e90,changelog.d/1217.fixed.md652ac6e90a32996cab2269b8b6f0291c3d54b942— identical to the local tree.Risk tier
node scripts/risk-gate.mjs --base 4740fccat the shipping tree:T3, so a seat PASS at this tip plus green
ci-ok. The blob is a backup CLI, not a workflow or a token path: no permission, no secret and no publish surface changes.Alternatives considered
--requirefixture makes the stall a hard hold, and it is the harness the existing lock tests already use./procread fails as stale. Rejected: unreadable is not the same as gone, and a wrongly taken lock is worse than a refusal.O_EXCLas one entry.Uncertainty and residual risk
/procis Linux-only. On a host without it the start time isnulland the behaviour is exactly the old one (pid only, live pid refuses) — the reused-number case stays open there, and no test drives such a host;npm run checkruns on Linux.Number("1234 5678")isNaN), so the old build treats it as the unreadable case and takes it over after a minute rather than after ten seconds. That matters only across a rollback while a lock is on disk; nothing is stranded.node:fsin the child; it is test-only and never loaded by the script in production.Rollback
Revert the three commits. The marker returns to an unconditional remove in the
finally, the lock and marker return to a bare pid,lockIsStalereturns to the pid probe alone, and the doc paragraph plus the changelog fragment go with them. Nothing is persisted; a two-token entry left on disk is treated by the old reader as unreadable and swept after the one-minute grace.UI surface
None — a CLI and its lock files. No page, string, asset, control or workflow step changes, so no before/after frame applies.
Docs and changelog
docs/deploy.mdgains the paragraph on what the two entries carry and why (reused number, pid-only shape, identity release).changelog.d/1217.fixed.mdcarries the user-visible line: a reused pid no longer blocks every backup, and a resumed straggler no longer opens the section a live run is inside. NoDocs-not-needed:claim.Ran:
npx tsx --test test/backupLock.test.tsat the base4740fcc1990bc2964e33f72a43f384f9c7d5e05cwith only the new test file on the tree —# tests 6 / # pass 3 / # fail 3, the three failures quoted verbatim above# tests 6 / # pass 6 / # fail 0npm run checkat the tip — exit 0 (# tests 3333 / # pass 3333 / # fail 0, lint at baseline, typecheck and build clean)node scripts/changelog.mjs checkat the tip —13 fragment(s) oknode scripts/risk-gate.mjs --base 4740fcc—risk-tier: T3; basis: scripts/backup.mjs496862c2through the contents API — identical to the local tree, listed aboveNot run:
/proc, so those platforms keep the pid-only behaviour; no test drives themtscbuild insidenpm run checkBACKUP_DIR: the fixture drives the CLI against a temporary directory, not a live instance