Repository navigation
fix: remove a lock takeover guard left behind once the lock is held - #335
Merged
Merged
Conversation
Merged
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.
Context
configfile changes files in the home folder, its deployment record and its copy of the dotfiles repository. Two configfile processes must never do that at once, so the commands that change things (
init,update,modules deploy,modules undeploy) take a lock first:~/.configfile/lock, a file holding the owner's pid (src/lock.ts,withLock).A process that dies leaves its lock behind. The next process takes it over: it removes the lock, then creates its own. Two processes doing that at once could each remove a lock the other has just created, so since #319 (released in 1.1.0) only one process at a time takes over, the one that managed to create
~/.configfile/lock.takeover(the "guard"). The guard holds its owner's pid too, and is removed at the end of the takeover.A guard can itself be left behind by a process that died.
removeStalethen moves it aside, checks that it still names the dead process, and puts it back if it does not (another process created a new guard meanwhile).Why
On CI, the test "never lets two processes hold the lock when the takeover guard was left behind" (
tests/lock.test.ts, 8 processes racing for a lock and a guard both left behind) failed once on PR #334 withexpected [ 'lock.takeover' ] to deeply equal []. The lock itself worked: no two processes held it at once. A guard was left behind at the end. It also fails locally about once in 30 runs.The cause, read from the code:
lock.takeover: nothing is there, so nothing is removed.That guard is harmless while its process number is unused: the next takeover removes a guard whose owner is dead. If another process reuses the number, the next takeover waits for it and then tells the user to delete the file.
Not confirmed with instrumentation: logging changes the timing, and the failure did not come back in 310 more runs. The new test covers the fix directly.
Change
src/lock.ts: a newremoveGuard. The process that holds the lock removeslock.takeover, whoever it names, right after taking the lock, next to the existing cleanup of drafts left behind.takeOver's comment now says a guard put back can outlive its owner.Not done: making
removeStalecheck whether the owner's takeover is over before putting a guard back. That needs the same check-then-act step one level down, and has the same race.Review order
src/lock.ts: theremoveGuardcall inwithLock, thenremoveGuardand thetakeOvercomment.tests/lock.test.ts: "removes a takeover guard left behind by a process still running, once it holds the lock".CHANGELOG.md.Tests
pnpm typecheckandpnpm lintare clean;pnpm vitest run: 811 tests pass.expected [ 'lock', 'lock.takeover' ] to deeply equal [ 'lock' ]) and passes with it.