Skip to content

Commit 9c24106

Browse files
fix(task-persistence): report the read failure when missing-file recovery rejects
readFileWithMissingRetry awaited recoverMissingJsonFile inside its retry catch but outside any handler, so a rejection from the recovery escaped the function instead of becoming the value that branch interprets. Every caller classifies this read by error code: apiMessages.ts maps ENOENT to "no file yet" and anything else to io_error, taskMessages.ts maps it to the "not_found" kind, TaskHistoryStore.ts swallows it into null. A rejecting recovery therefore re-classifies the read: an EBUSY inside recovery becomes an I/O error on a file that is merely absent. The fix keeps the missing-file retry the rows already assert and counts a rejected recovery as "nothing recovered", so the caller always receives the ENOENT read failure it knows how to classify. jsonFileRecovery traces the recovery failure with its own code before throwing, so the diagnostic is not lost, and no new abstraction is introduced. safeWriteJson.ts:343 is a changed executable line in this pull's own diff: the hunk switches its argument from absoluteFilePath to resolvedTargetPath, and no row distinguished the two spellings because every existing recovery row keeps the alias and the referent in one directory. The added row plants the orphaned backup beside the referent with the alias directory empty, which only the resolved-target spelling can see, and asserts that the merge callback read the restored history, that the trace reported the restore, that the published file holds the merged content, and that the backup was consumed. The link is simulated by mocking fs.realpath, the same way the staging and lock-key rows in that file do, because real symlinks are unavailable here. Controls, holding the previous head d6fa711: - new retry row against the previous head with the source untouched: 1 failed | 3 passed, the failure being the escaping EBUSY rejection; with the fix: 4 passed. - the same row against each mutant the instrument plans for lines 54-55 of the fix: a recovery rejection counted as recovered (.catch(() => true)) makes the reader resolve with bytes that appeared during the recovery and fails this row alone; reverting line 49 to its pre-fix shape fails this row alone. - new recovery row with 343 reverted to its pre-pull argument: the only failure in utils/__tests__/safeWriteJson.test.ts (1 failed | 53 passed | 4 skipped), so the row reaches 343 and asserts exactly the change this pull made there. - repo-wide prettier --check .: clean. tsc --noEmit in src: clean. eslint on the three touched files with --max-warnings=0 --prune-suppressions: no problems, and src/eslint-suppressions.json is byte-identical to its committed blob (a prune run on a partial file set rewrites that file, so it was reverted). Pricing, printed by scripts/stryker-diff.mjs (blob c332971815aecfaf) against this pull's own base 5319fe9: the instrument reports "extension has 770 changed executable lines (limit 500)" at the previous head and 772 here, so this commit adds two chargeable executable lines to a package already 270 lines over the cap, and 2357 stays a split question - this fix does not pretend otherwise. On its own unit delta the instrument lists only core/task-persistence/readFileWithMissingRetry.ts:54-55 (2 chargeable lines) and its dry run instrumented that file with 5 mutants (limit 400): the two added rows are test-only and the instrument's own path classifier excludes __tests__ paths, so the rows are cap-neutral by construction rather than by headroom. Review thread answered: pull-request comment 4505288199, on src/utils/safeWriteJson.ts line 343. The await above has no review thread to answer: the thread listing read from this repository tonight lists 50 threads, every one of them resolved, and none on src/core/task-persistence/readFileWithMissingRetry.ts. No changeset and no changelog entry.
1 parent d6fa711 commit 9c24106

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎src/core/task-persistence/__tests__/readFileWithMissingRetry.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs/promises"
22
import * as os from "os"
33
import * as path from "path"
44

5+
import * as recovery from "../../../utils/jsonFileRecovery"
56
import { readFileWithMissingRetry } from "../readFileWithMissingRetry"
67

78
describe("readFileWithMissingRetry", () => {
@@ -26,4 +27,43 @@ describe("readFileWithMissingRetry", () => {
2627
it("propagates non-ENOENT errors", async () => {
2728
await expect(readFileWithMissingRetry(dir)).rejects.toMatchObject({ code: "EISDIR" })
2829
})
30+
31+
// The callers of this reader classify the failure they receive by error code:
32+
// apiMessages.ts maps ENOENT to "no file yet" and anything else to io_error,
33+
// taskMessages.ts maps it to the "not_found" kind, and TaskHistoryStore.ts swallows
34+
// it into null. A recovery that rejects must therefore not reach the caller as the
35+
// read's error - the reader's own ENOENT is the failure those classifiers know.
36+
it("reports the missing file, not the recovery failure, when recovery itself rejects", async () => {
37+
const missing = path.join(dir, "api_mock_latest.json")
38+
const recoveryError = Object.assign(new Error("recovery scan failed"), { code: "EBUSY" })
39+
// The recovery puts the file back and then fails - it loses the lock too late to
40+
// report success. A rejection still cannot be read as "recovered": the reader cannot
41+
// tell whether the recovery finished, and every caller classifies this read by error
42+
// code. If the rejection counted as a recovery, the reader would return the planted
43+
// bytes and the row below would pass on a third read.
44+
const recoverySpy = vi.spyOn(recovery, "recoverMissingJsonFile").mockImplementation(async () => {
45+
await fs.writeFile(missing, JSON.stringify({ recoveredBySomeoneElse: true }))
46+
throw recoveryError
47+
})
48+
try {
49+
let failure: unknown
50+
const value = await readFileWithMissingRetry(missing).catch((reason: unknown) => {
51+
failure = reason
52+
return undefined
53+
})
54+
// The rejection carries EBUSY, which every caller above would report as an I/O
55+
// error on a file that is simply not there yet; the reader's own failure is the
56+
// ENOENT it already saw, and a resolved read of the planted bytes would be a third
57+
// outcome neither the caller nor this row expects.
58+
expect(failure).toBeDefined()
59+
expect(value).toBeUndefined()
60+
expect(failure).not.toBe(recoveryError)
61+
expect(failure).toMatchObject({ code: "ENOENT" })
62+
// The recovery was attempted exactly once, so the missing-file retry the rows
63+
// above assert is unchanged.
64+
expect(recoverySpy).toHaveBeenCalledTimes(1)
65+
} finally {
66+
recoverySpy.mockRestore()
67+
}
68+
})
2969
})

‎src/core/task-persistence/readFileWithMissingRetry.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export async function readFileWithMissingRetry(filePath: string): Promise<string
4646
return await fs.readFile(filePath, "utf8")
4747
} catch (retryError) {
4848
if (getErrorCode(retryError) !== "ENOENT") throw retryError
49-
if (!(await recoverMissingJsonFile(filePath))) throw retryError
49+
// A recovery that rejects failed for its own reasons - a readdir error, a lock it could
50+
// not take - and jsonFileRecovery traces that failure with its code before throwing.
51+
// It is not the reader's failure: the caller can only classify the read it asked for,
52+
// so an unreachable recovery counts as "nothing recovered" and the ENOENT read above
53+
// is what the caller receives.
54+
const recovered = await recoverMissingJsonFile(filePath).catch(() => false)
55+
if (!recovered) throw retryError
5056
return fs.readFile(filePath, "utf8")
5157
}
5258
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,4 +1315,50 @@ describe("safeWriteJson", () => {
13151315
await undoEscapedPublish(escaped, Buffer.from("{}"))
13161316
await undoEscapedPublish(path.join(outside, "absent.json"), undefined)
13171317
})
1318+
1319+
// Line 343 is the recovery call, and this pull changed its argument from the caller's
1320+
// spelling to the resolved publish target. The two spellings only differ when the
1321+
// orphaned replacement files sit beside the referent rather than beside the alias, which
1322+
// is the crash state below. Real symlinks are unavailable in this lane, so the link is
1323+
// simulated by mocking fs.realpath the same way the staging and lock-key rows do.
1324+
test("recovers an orphaned backup found beside the resolved target, not the caller alias", async () => {
1325+
const referentDir = path.join(tempDir, "recover-referent")
1326+
const aliasDir = path.join(tempDir, "recover-alias")
1327+
await fs.mkdir(referentDir, { recursive: true })
1328+
await fs.mkdir(aliasDir, { recursive: true })
1329+
const callerPath = path.join(aliasDir, "history.json")
1330+
const referentPath = path.join(referentDir, "history.json")
1331+
// Crash state of the two-step replacement seen through the link: the committed bytes
1332+
// live in an orphaned backup next to the referent, the referent itself is absent, and
1333+
// the alias directory holds no replacement files at all.
1334+
const backupPath = path.join(referentDir, ".history.json.bak_1_last.tmp")
1335+
const committed = { history: ["committed before the interruption"] }
1336+
await fs.writeFile(backupPath, JSON.stringify(committed))
1337+
// Only the file resolves through the link; both directories are already canonical, so
1338+
// the scan directory is the one place the two spellings disagree.
1339+
const realpathSpy = vi
1340+
.spyOn(fs, "realpath")
1341+
.mockImplementation(async (target) => (target === callerPath ? referentPath : String(target)))
1342+
const merge = vi.fn((existing: unknown, incoming: unknown) => ({
1343+
...(existing as Record<string, unknown>),
1344+
...(incoming as Record<string, unknown>),
1345+
}))
1346+
const trace = vi.fn()
1347+
try {
1348+
await safeWriteJson(callerPath, { added: true }, { merge, trace })
1349+
// Recovery scanned the resolved target, so the merge callback read the committed
1350+
// history instead of reporting an absent file.
1351+
expect(merge).toHaveBeenCalledExactlyOnceWith(committed, { added: true })
1352+
// The restore is reported through the same trace channel the writer already uses.
1353+
expect(trace).toHaveBeenCalledWith({ stage: "recover", detail: backupPath })
1354+
expect(await readFileContent(referentPath)).toEqual({
1355+
history: ["committed before the interruption"],
1356+
added: true,
1357+
})
1358+
// The restored backup was consumed: the leftover does not outlive the write.
1359+
expect(fsSyncActual.existsSync(backupPath)).toBe(false)
1360+
} finally {
1361+
realpathSpy.mockRestore()
1362+
}
1363+
})
13181364
})

0 commit comments

Comments
 (0)