Skip to content

Commit 66a7e40

Browse files
HugoXKBirfy
andauthored
test(timeline): pin the refused directory fsync (#3304)
Fixes #3301 Owner: Dev Written-by: Dev Reviewer: Reviewer Docs-not-needed: tests only; no product behaviour and no documented write order changes. ## What this is The fix is already in `main`: `#3300` (merged as `1d06764b`) made `flushDirectory` tolerate a refused directory `fsync` — EPERM on Windows, EINVAL on a filesystem without directory flushes — so a segment that was synced and renamed is not rewritten on every flush. This branch is rebased onto `main` (`664f48fd`); the old tip's `src/store.js` change and `changelog.d/3301.fixed.md` are gone. The diff is one file: `plugins/timeline/test/store.test.js`. `main` has no `sync` option to inject a refusal, so the cases pin the `fsyncSync` binding `store.js` imports with `node:module`'s `syncBuiltinESMExports()` — the seam the repo already uses in `ds-bot/test/store.test.js`. Only directory handles are refused: the file sync stays real, so the cases cannot paper over a failing file sync. ## What the three cases pin 1. **EPERM still saves** — `flush()` is true, `stats().failing` is false, the segment is not rewritten by the next flush, and the records read back after a reopen. 2. **EINVAL tolerated** — the same answer for a filesystem without directory flushes. 3. **Rotation under refusal** — the cap still prunes to `trace-000002.json`/`trace-000003.json` and the surviving records are `[3, 4, 5, 6]`. Red first, same tree with only `noDirectoryFlush` emptied (`new Set([])`; src restored afterwards): `node --test test/store.test.js` → 16 pass / 3 fail, exactly the three cases above. ## Ran at this tip: ``` $ cd plugins/timeline && TZ=UTC npm test # tests 175 / pass 175 / fail 0 (main before this change: 172/172) $ TZ=UTC node --import ./test/resolve.mjs --test test/store.test.js # tests 19 / pass 19 / fail 0 $ node scripts/lint-plugins.mjs timeline # 0 errors, 9 pre-existing warnings $ node .github/scripts/changelog.mjs check # 4 fragment(s) well formed ``` Not run: real Windows (the refusal is Windows-only; the cases inject it), arch-docs (`core/scripts/check-arch-docs.mjs` shells out to `git`, absent here; the commit carries `Docs-not-needed:`), full `npm run verify`, and CI's Node 24 leg. ## Checkpoint <!-- flybot:checkpoint --> { "pr": 3304, "claim": "Dev", "issue": 3301, "summary": "Rebased onto main 664f48f with the implementation dropped: #3300 already tolerates a refused directory fsync, so the diff is one test file. Three cases pin it by refusing only directory handles through the fsyncSync binding store.js imports: EPERM still saves the segment and it is not rewritten, EINVAL is tolerated the same way, and a rotation still prunes to the cap. Emptying the tolerated set turns all three red.", "files": [ "plugins/timeline/test/store.test.js" ], "checks": { "ran": "cd plugins/timeline && TZ=UTC npm test; TZ=UTC node --import ./test/resolve.mjs --test test/store.test.js; node scripts/lint-plugins.mjs timeline; node .github/scripts/changelog.mjs check", "result": "green", "at": "c4940e7977d0edc75a9167258a84ad9d6500a636" }, "next": "none — ready for review" } <!-- /flybot:checkpoint --> Co-authored-by: Danyang Chen <birfychen@gmail.com>
1 parent 664f48f commit 66a7e40

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

‎plugins/timeline/test/store.test.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from 'node:assert/strict'
2+
import fs from 'node:fs'
23
import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs'
4+
import { syncBuiltinESMExports } from 'node:module'
35
import { tmpdir } from 'node:os'
46
import { join } from 'node:path'
57
import { test } from 'node:test'
@@ -277,3 +279,71 @@ test('a plain temporary file a crashed writer left behind (one name) is overwrit
277279
assert.equal(JSON.parse(readFileSync(join(dir, 'trace-000001.json'), 'utf8')).records.length, 1)
278280
trace.close()
279281
})
282+
283+
// Windows refuses `fsync` on a directory handle (EPERM from FlushFileBuffers; EINVAL on a filesystem
284+
// without directory flushes). It is the last step of `writeWhole`, after the temporary file was
285+
// synced and renamed, so the segment's bytes are already durable when it throws: the write must
286+
// count as saved, or the published segment is rewritten on every flush and the cap and the lock
287+
// hand-off never settle (#3298). A Linux directory fsync is a no-op success, so the case replaces
288+
// the `fsyncSync` binding `store.js` imports (node:module keeps that binding live) for as long as it
289+
// runs, and refuses only directory handles: the file sync stays real, and a failing *file* sync
290+
// would still reach the caller instead of being swallowed.
291+
const refuseDirectorySync = (t, code) => {
292+
const real = fs.fsyncSync
293+
const seen = { files: 0 }
294+
fs.fsyncSync = (fd) => {
295+
if (fs.fstatSync(fd).isDirectory()) throw Object.assign(new Error(`${code}: fsync failed`), { code })
296+
seen.files += 1
297+
return real(fd)
298+
}
299+
syncBuiltinESMExports()
300+
t.after(() => {
301+
fs.fsyncSync = real
302+
syncBuiltinESMExports()
303+
})
304+
return seen
305+
}
306+
307+
test('a directory fsync the platform refuses still saves the segment (Windows EPERM)', (t) => {
308+
const dir = tmp()
309+
t.after(() => rmSync(dir, { recursive: true, force: true }))
310+
const seen = refuseDirectorySync(t, 'EPERM')
311+
const trace = openTrace({ dir, now: () => 10_000 })
312+
trace.append(rec(9_000))
313+
assert.equal(trace.flush(), true, 'the bytes are durable once the file was synced and renamed')
314+
assert.equal(trace.stats().failing, false, 'a refused directory sync is not a failed write')
315+
assert.equal(seen.files, 1, 'the segment itself was synced before the directory was')
316+
assert.equal(trace.flush(), true, 'a flush with nothing new succeeds too')
317+
assert.equal(seen.files, 1, 'a saved segment is not rewritten by the next flush')
318+
trace.close()
319+
const reopened = openTrace({ dir, now: () => 10_000 })
320+
assert.deepEqual(reopened.records().map(r => r.t), [9_000])
321+
reopened.close()
322+
})
323+
324+
test('a directory fsync refused with EINVAL is tolerated the same way', (t) => {
325+
const dir = tmp()
326+
t.after(() => rmSync(dir, { recursive: true, force: true }))
327+
const seen = refuseDirectorySync(t, 'EINVAL')
328+
const trace = openTrace({ dir, now: () => 10_000 })
329+
trace.append(rec(9_000))
330+
assert.equal(trace.flush(), true)
331+
assert.equal(trace.stats().failing, false)
332+
assert.equal(seen.files, 1)
333+
trace.close()
334+
const reopened = openTrace({ dir, now: () => 10_000 })
335+
assert.deepEqual(reopened.records().map(r => r.t), [9_000])
336+
reopened.close()
337+
})
338+
339+
test('a rotation still prunes to the cap when the directory fsync is refused', (t) => {
340+
const dir = tmp()
341+
t.after(() => rmSync(dir, { recursive: true, force: true }))
342+
refuseDirectorySync(t, 'EPERM')
343+
const trace = openTrace({ dir, segmentRecords: 2, segments: 2, flushMs: 60_000, now: () => 1_000_000 })
344+
for (const at of [1, 2, 3, 4, 5, 6]) trace.append(rec(at))
345+
assert.equal(trace.flush(), true)
346+
assert.deepEqual(files(dir).filter(name => /^trace-\d{6}\.json$/.test(name)), ['trace-000002.json', 'trace-000003.json'])
347+
assert.deepEqual(trace.records().map(r => r.t), [3, 4, 5, 6])
348+
trace.close()
349+
})

0 commit comments

Comments
 (0)