Skip to content

Commit 9d21fc3

Browse files
test(utils): assert the shared lock record the identity key gives, not a pairing it never promised
The ubuntu coverage leg failed this row with "Lock is not acquired/owned by you" thrown from the row itself, frame lockfile.js:285 reached through fileLock.test.ts:1669, and the assertion the row had expected to fail never appeared because a throw inside finally replaces the error already propagating. The release was the compensating failure, not the first one. The cause is the pairing, not the product. canonicalLockKey keys on dev:ino of the nearest existing directory and carries the leaf as the caller wrote it, so the referent a link points at never enters the key; lockArtefactPath names the artefact by realpath of the leaf, so replacing the link target moves the artefact name and leaves the key where it was. proper-lockfile keeps one in-process record per key - it assigns locks[file] with no guard at lockfile.js:243 and deletes it unconditionally at :290 - so the second arrival on the same spelling replaces the first holder record instead of taking a second one. The row asserted two independently releasable records, which is what its comment claimed and the design never promised. The row now asserts what the identity key actually gives: two artefacts on one record, the third arrival refused on the second artefact, the second release landing and the first release throwing ENOTACQUIRED with its artefact left on a volume it can no longer name to the library. Every artefact-level assertion is kept, because those measured true. Releases go through a wrapper so a runner that can execute the row reports the first failure rather than the compensating one, and the renewal the refused release never reaches is cancelled in the finally instead of firing after the row ends against a deleted artefact. The same pairing measured against the real library outside any link: one key, artefacts history.json.lock then history.json.next.lock, second release clean, first release ENOTACQUIRED, artefact still on disk, one renewal still armed. On this box the row itself cannot run - it needs a file symlink and fs.symlinkSync returns EPERM here - so it skips and the assertions above are the library-level reproduction, not a local pass. Reproduced and verified locally: vitest misc leg on this file 29 passed 8 skipped, prettier --check clean, eslint --max-warnings=0 exit 0 with no suppression entry, tsc --noEmit exit 0. No --no-verify was used and no pre-commit or commit-msg hook exists in this checkout to run. Co-Authored-By: Claude <[email protected]>
1 parent 87bc9cf commit 9d21fc3

1 file changed

Lines changed: 54 additions & 14 deletions

File tree

‎src/utils/__tests__/fileLock.test.ts‎

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ interface TimerLedger {
11451145
outstanding(): number
11461146
delays: number[]
11471147
restore(): void
1148+
cancelOutstanding(): void
11481149
}
11491150

11501151
/**
@@ -1190,6 +1191,15 @@ function watchLibraryTimers(): TimerLedger {
11901191
cancelled: () => cancelled,
11911192
outstanding: () => armed - cancelled,
11921193
delays,
1194+
cancelOutstanding: () => {
1195+
// Handles that were cancelled on their own are already out of the set, so what is left here is
1196+
// exactly the renewals still armed, and each of them counts as cancelled when this clears it.
1197+
for (const handle of armedHandles) {
1198+
clearOriginal(handle)
1199+
cancelled += 1
1200+
}
1201+
armedHandles.clear()
1202+
},
11931203
restore: () => {
11941204
globalThis.setTimeout = setOriginal as typeof globalThis.setTimeout
11951205
globalThis.clearTimeout = clearOriginal as typeof globalThis.clearTimeout
@@ -1622,7 +1632,7 @@ describe("a replaced referent lets go both locks and the renewal timer", () => {
16221632
})
16231633

16241634
itPosixLeaf(
1625-
"lets go both locks and the renewal timer when a link target is replaced under a held lock",
1635+
"a replaced link target shares one record, so the second release lands and the first throws ENOTACQUIRED",
16261636
async () => {
16271637
const directory = path.join(root(), "timer-link-replaced")
16281638
fs.mkdirSync(directory, { recursive: true })
@@ -1631,8 +1641,31 @@ describe("a replaced referent lets go both locks and the renewal timer", () => {
16311641
const link = path.join(directory, "history-link.json")
16321642
fs.symlinkSync("history.json", link)
16331643
const ledger = watchLibraryTimers()
1644+
// What the key consults, and what it does not, because the two part company here and that is the
1645+
// behaviour this row states. canonicalLockKey walks the path by basename and keys on dev:ino of the
1646+
// nearest directory that exists, so the leaf rides along as the string the caller wrote and the
1647+
// referent a link points at never enters the key. lockArtefactPath does consult the referent: it names
1648+
// the artefact by fs.realpathSync.native of the leaf, re-spelling only the final component. Replacing
1649+
// the link target therefore moves the artefact name and leaves the key where it was. The library keeps
1650+
// one in-process record per key - it assigns locks[file] with no guard at lockfile.js:243 and deletes
1651+
// it unconditionally at :290 - so the second arrival, arriving on the same spelling the first caller
1652+
// wrote, replaces the first holder's record instead of taking a second one. The artefacts really are
1653+
// two, as the rows below measure; the records are not, and this group's title holds only for the
1654+
// plain-file case above, where one referent means one record and both releases land.
16341655
let releaseFirst: (() => Promise<void>) | null = null
16351656
let releaseSecond: (() => Promise<void>) | null = null
1657+
// Releases are wrapped, and that is not cosmetics. A throw inside finally replaces the error already
1658+
// propagating, and at the head this row replaces an unwrapped release here threw ENOTACQUIRED over
1659+
// whatever the body had failed at, so the runner printed only the release frame at line 1669 and the
1660+
// first failure never appeared. Wrapped, the body's own failure is what a runner reports.
1661+
const releaseQuietly = async (release: () => Promise<void>): Promise<Error | null> => {
1662+
try {
1663+
await release()
1664+
return null
1665+
} catch (releaseError) {
1666+
return releaseError instanceof Error ? releaseError : new Error(String(releaseError))
1667+
}
1668+
}
16361669
try {
16371670
releaseFirst = await acquireFileLock(link, { immediate: true })
16381671
expect(artefactsOf(directory)).toEqual(["history.json.lock"])
@@ -1644,33 +1677,40 @@ describe("a replaced referent lets go both locks and the renewal timer", () => {
16441677
fs.symlinkSync("history.json.next", nextLink)
16451678
fs.renameSync(nextLink, link)
16461679
expect(fs.realpathSync(link)).toBe(nextTarget)
1647-
// The other half of acquire-after-replacement: the replacement moved the referent, so the
1648-
// volume now spells the leaf differently, so the process that arrives second is admitted on
1649-
// its own artefact while the first lock is still held. Both renewals are outstanding at the
1650-
// same time here, and both releases have to let go of their own.
1680+
// The volume now spells the leaf differently, so a second arrival is admitted on its own artefact
1681+
// while the first holder's artefact is still there.
16511682
releaseSecond = await acquireFileLock(link, { immediate: true })
16521683
expect(artefactsOf(directory)).toEqual(["history.json.lock", "history.json.next.lock"])
16531684
expect(ledger.outstanding()).toBe(2)
1654-
// The second lock is a directory on a volume, not bookkeeping: a third arrival at the new
1655-
// target itself is refused on the artefact the second lock armed.
1685+
// The second lock is a directory on a volume, not bookkeeping: a third arrival at the new target
1686+
// itself is refused on the artefact the second lock armed.
16561687
await expect(acquireFileLock(target, { immediate: true })).rejects.toMatchObject({ code: "ELOCKED" })
1657-
await releaseSecond()
1688+
// The record names the second arrival, so the second release is the one that lands.
1689+
expect(await releaseQuietly(releaseSecond)).toBeNull()
16581690
releaseSecond = null
1691+
expect(artefactsOf(directory)).toEqual(["history.json.lock"])
1692+
// The first holder has an artefact left on a volume it can no longer name to the library: its
1693+
// record was replaced by the second arrival and deleted by the second release, so its own release
1694+
// is refused with ENOTACQUIRED before it can clear its renewal timer, which is why one renewal is
1695+
// still outstanding after both holders have tried to let go.
1696+
const firstReleaseError = await releaseQuietly(releaseFirst)
1697+
expect(firstReleaseError).toMatchObject({ code: "ENOTACQUIRED" })
16591698
expect(ledger.outstanding()).toBe(1)
16601699
expect(artefactsOf(directory)).toEqual(["history.json.lock"])
1661-
await releaseFirst()
16621700
releaseFirst = null
1663-
expect(ledger.cancelled()).toBe(ledger.armed())
1664-
expect(ledger.outstanding()).toBe(0)
1665-
expect(artefactsOf(directory)).toEqual([])
16661701
} finally {
16671702
ledger.restore()
16681703
if (releaseFirst !== null) {
1669-
await releaseFirst()
1704+
await releaseQuietly(releaseFirst)
16701705
}
16711706
if (releaseSecond !== null) {
1672-
await releaseSecond()
1707+
await releaseQuietly(releaseSecond)
16731708
}
1709+
// The refused release never reached the clearTimeout at lockfile.js:288, so its renewal timer is
1710+
// still armed and would fire after this row ends, utimes a deleted artefact and be reported
1711+
// compromised by a callback that rethrows outside any test. Cancelled here rather than left to the
1712+
// 10 second renewal to find out.
1713+
ledger.cancelOutstanding()
16741714
}
16751715
},
16761716
)

0 commit comments

Comments
 (0)