Skip to content

Commit 30b2bc8

Browse files
author
lane-u5a-2359
committed
test: give back the leaf lock when a coexist row fails before its release
The three rows in the coexistence block acquire a lock in this process and release it inline, so an assertion that lands between the two leaves the lock held with its renewal timer keeping the artefact fresh. That is what the finding on row 4555925401 says about the row that reaps its child in a finally and never releases the leaf, and the same hole sits in the row beside it whose two releases follow its last assertion and in the posix row whose two releases follow its own. Guard the failure path for the block, in the shape the merge block guard took in the previous commit: record what each row is holding, clear the record when the row lets go, and in the afterEach release only what is still recorded, clearing before awaiting so a release that lands late is never attempted twice and the row that failed still reports its own assertion. The success paths did not move, and the diff is 81 added lines with no removed line. Pin it with two rows that need no alias and no symlink, so they are measured wherever the block runs rather than only where short names are generated. The first forces the failure the finding names, an assertion that lands while the leaf lock is held and before any release line in the block, and records that the artefact is still there at that instant, which is the hole and not the fix. The second runs after the guard and asserts what the guard left, namely no artefact under this process and the same path acquirable again by an immediate call. Measured by taking the guard away: without it the file printed 1 failed, 32 passed and 8 skipped of 41 with the message that an artefact list containing the leaf lock directory did not equal an empty list; with it the file printed 33 passed and 8 skipped of 41 and the misc lane printed 2127 passed and 21 skipped of 2148 over 119 files where the commit before this one printed 2125 passed and 21 skipped of 2144. The prettier check, the type check and the lint pass came back clean on both sides. No production file changed, so the priced unit delta against the unit base is unchanged, and the mutation leg for these commits is unmeasured locally.
1 parent 36ad681 commit 30b2bc8

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,42 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
13131313
let lockModule = ""
13141314
let generatesShortNames = false
13151315
let traceIndex = 0
1316+
let heldAliasLeaf: (() => Promise<void>) | null = null
1317+
let heldAliasSibling: (() => Promise<void>) | null = null
1318+
let heldLinkLock: (() => Promise<void>) | null = null
1319+
let heldLinkSibling: (() => Promise<void>) | null = null
1320+
let forcedDirectory: string | null = null
1321+
let forcedTarget: string | null = null
1322+
1323+
// The same hole the guard at line 1518 covers for the merge rows is here in three rows, and the
1324+
// finding on row 4555925401 names the clearest of them: this block acquires a leaf lock, releases it
1325+
// inline, and a row that fails before that line leaves the lock held by this process while its
1326+
// renewal timer keeps the artefact fresh. Row by row guards would mean three of them, so the guard
1327+
// goes on the block in the same shape, releasing only what has not been released and clearing the
1328+
// record before awaiting, so a release that lands late cannot be released twice and the row that
1329+
// failed still reports its own assertion.
1330+
afterEach(async () => {
1331+
if (heldAliasLeaf !== null) {
1332+
const give = heldAliasLeaf
1333+
heldAliasLeaf = null
1334+
await give()
1335+
}
1336+
if (heldAliasSibling !== null) {
1337+
const give = heldAliasSibling
1338+
heldAliasSibling = null
1339+
await give()
1340+
}
1341+
if (heldLinkLock !== null) {
1342+
const give = heldLinkLock
1343+
heldLinkLock = null
1344+
await give()
1345+
}
1346+
if (heldLinkSibling !== null) {
1347+
const give = heldLinkSibling
1348+
heldLinkSibling = null
1349+
await give()
1350+
}
1351+
})
13161352
beforeAll(async () => {
13171353
actualLock = (await vi.importActual<typeof import("proper-lockfile")>("proper-lockfile")).lock
13181354
lockModule = createRequire(import.meta.url).resolve("proper-lockfile")
@@ -1363,8 +1399,10 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
13631399
const aliasLeaf = aliasOf(target)
13641400
expect(aliasLeaf).not.toBe(target)
13651401
const releaseLeaf = await acquireFileLock(target, { immediate: true })
1402+
heldAliasLeaf = releaseLeaf
13661403
// The coexistence half: a sibling with its own name and its own inode has to get in.
13671404
const releaseSibling = await acquireFileLock(sibling, { immediate: true })
1405+
heldAliasSibling = releaseSibling
13681406
const bothPlaced = [
13691407
"state-sidecar-leaf-for-coheld-lock-testing.json.lock",
13701408
"task-history-leaf-for-coheld-lock-testing.json.lock",
@@ -1404,7 +1442,9 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
14041442
expect(await siblingProbe.waitForExit()).toBe(1)
14051443
expect(artefactsOf(directory)).toEqual(bothPlaced)
14061444
await releaseLeaf()
1445+
heldAliasLeaf = null
14071446
await releaseSibling()
1447+
heldAliasSibling = null
14081448
expect(artefactsOf(directory)).toEqual([])
14091449
},
14101450
)
@@ -1414,6 +1454,7 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
14141454
const sibling = path.join(directory, "state-sidecar-leaf-for-sibling-admission.json")
14151455
fs.writeFileSync(sibling, '{"pre":1}', "utf8")
14161456
const releaseLeaf = await acquireFileLock(target, { immediate: true })
1457+
heldAliasLeaf = releaseLeaf
14171458
const holder = startLockHolder(sibling, "hold", lockModule, directory, nextTraceName(), false, {
14181459
holdMs: volumeAliasTimeoutMs,
14191460
waitForAssertions: true,
@@ -1439,6 +1480,7 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
14391480
expect(traceLines.some((line) => line.startsWith("ASSERTIONS-DONE "))).toBe(true)
14401481
expect(traceLines.some((line) => line.startsWith("HOLD-EXPIRED "))).toBe(false)
14411482
await releaseLeaf()
1483+
heldAliasLeaf = null
14421484
expect(artefactsOf(directory)).toEqual([])
14431485
} finally {
14441486
// Always this process to end it: an assertion failure above must not leave the child
@@ -1461,7 +1503,9 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
14611503
fs.writeFileSync(sibling, '{"pre":1}', "utf8")
14621504
expect(fs.statSync(link).ino).toBe(fs.statSync(target).ino)
14631505
const releaseLink = await acquireFileLock(link, { immediate: true })
1506+
heldLinkLock = releaseLink
14641507
const releaseSibling = await acquireFileLock(sibling, { immediate: true })
1508+
heldLinkSibling = releaseSibling
14651509
expect(artefactsOf(directory)).toEqual(["history.json.lock", "state-sidecar.json.lock"])
14661510
expect(canonicalLockKey(link)).not.toBe(canonicalLockKey(sibling))
14671511
const probe = startLockHolder(target, "try", lockModule, directory, nextTraceName())
@@ -1472,10 +1516,47 @@ describe("an unrelated lock and a held leaf coexist while the alias contends", (
14721516
expect(await probe.waitForExit()).toBe(1)
14731517
expect(artefactsOf(directory)).toEqual(["history.json.lock", "state-sidecar.json.lock"])
14741518
await releaseLink()
1519+
heldLinkLock = null
14751520
await releaseSibling()
1521+
heldLinkSibling = null
14761522
expect(artefactsOf(directory)).toEqual([])
14771523
},
14781524
)
1525+
1526+
it("leaves the leaf lock held when a row fails before its release", { timeout: 30000 }, async () => {
1527+
const { directory, target } = scenario("coheld-forced-failure", "task-history-leaf-for-forced-failure.json")
1528+
forcedDirectory = directory
1529+
forcedTarget = target
1530+
heldAliasLeaf = await acquireFileLock(target, { immediate: true })
1531+
let forced = ""
1532+
try {
1533+
// The failure the finding names, forced on purpose so that the letting go is what is
1534+
// tested: an assertion that lands while this process still holds the leaf lock, before
1535+
// any release line in this block would have run.
1536+
expect(artefactsOf(directory)).toEqual([])
1537+
} catch (thrown) {
1538+
forced = String(thrown).slice(0, 40)
1539+
}
1540+
expect(forced).not.toBe("")
1541+
// What that failure leaves at the instant it lands, before any block guard can run: the
1542+
// artefact still sitting under this process. The row after this one is the assertion about
1543+
// the guard, because a hook cannot run inside the row that failed.
1544+
expect(artefactsOf(directory)).toEqual(["task-history-leaf-for-forced-failure.json.lock"])
1545+
})
1546+
1547+
it("gives back the leaf lock that a failed row left held", { timeout: 30000 }, async () => {
1548+
if (forcedDirectory === null || forcedTarget === null) {
1549+
throw new Error("the forced failure row recorded no objects to check")
1550+
}
1551+
// Running here, after the guard of this block, is the point: what is asserted is what the
1552+
// guard left, namely no artefact under this process and the same path acquirable again by
1553+
// an immediate call that refuses rather than waits.
1554+
expect(artefactsOf(forcedDirectory)).toEqual([])
1555+
heldAliasLeaf = await acquireFileLock(forcedTarget, { immediate: true })
1556+
await heldAliasLeaf()
1557+
heldAliasLeaf = null
1558+
expect(artefactsOf(forcedDirectory)).toEqual([])
1559+
})
14791560
})
14801561

14811562
/**

0 commit comments

Comments
 (0)