From c4940e7977d0edc75a9167258a84ad9d6500a636 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Sun, 22 Nov 2026 17:36:09 +0800 Subject: [PATCH] test(timeline): pin a refused directory fsync to the saved segment Windows answers EPERM for `fsync` on a directory handle (EINVAL on a filesystem without directory flushes), and it is the last step of `writeWhole`, after the temporary file was synced and renamed. Since #3300 `flushDirectory` tolerates that refusal, so a segment that is already in place still counts as saved and is not rewritten on every flush (#3298, #3301). A Linux directory fsync is a no-op success, so the three cases pin the `fsyncSync` binding `store.js` imports (through node:module's `syncBuiltinESMExports()`) and refuse only directory handles: EPERM still saves the segment (flush true, `failing` false, not rewritten by the next flush, records read back after a reopen), EINVAL is tolerated the same way, and a rotation still prunes to the cap while the directory fsync is refused. Emptying the tolerated set turns all three red. Docs-not-needed: tests only; no product behaviour and no documented write order changes. Refs #3301 --- plugins/timeline/test/store.test.js | 70 +++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/plugins/timeline/test/store.test.js b/plugins/timeline/test/store.test.js index 7dbfb94cd..b823013dd 100644 --- a/plugins/timeline/test/store.test.js +++ b/plugins/timeline/test/store.test.js @@ -1,5 +1,7 @@ import assert from 'node:assert/strict' +import fs from 'node:fs' import { chmodSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs' +import { syncBuiltinESMExports } from 'node:module' import { tmpdir } from 'node:os' import { join } from 'node:path' import { test } from 'node:test' @@ -277,3 +279,71 @@ test('a plain temporary file a crashed writer left behind (one name) is overwrit assert.equal(JSON.parse(readFileSync(join(dir, 'trace-000001.json'), 'utf8')).records.length, 1) trace.close() }) + +// Windows refuses `fsync` on a directory handle (EPERM from FlushFileBuffers; EINVAL on a filesystem +// without directory flushes). It is the last step of `writeWhole`, after the temporary file was +// synced and renamed, so the segment's bytes are already durable when it throws: the write must +// count as saved, or the published segment is rewritten on every flush and the cap and the lock +// hand-off never settle (#3298). A Linux directory fsync is a no-op success, so the case replaces +// the `fsyncSync` binding `store.js` imports (node:module keeps that binding live) for as long as it +// runs, and refuses only directory handles: the file sync stays real, and a failing *file* sync +// would still reach the caller instead of being swallowed. +const refuseDirectorySync = (t, code) => { + const real = fs.fsyncSync + const seen = { files: 0 } + fs.fsyncSync = (fd) => { + if (fs.fstatSync(fd).isDirectory()) throw Object.assign(new Error(`${code}: fsync failed`), { code }) + seen.files += 1 + return real(fd) + } + syncBuiltinESMExports() + t.after(() => { + fs.fsyncSync = real + syncBuiltinESMExports() + }) + return seen +} + +test('a directory fsync the platform refuses still saves the segment (Windows EPERM)', (t) => { + const dir = tmp() + t.after(() => rmSync(dir, { recursive: true, force: true })) + const seen = refuseDirectorySync(t, 'EPERM') + const trace = openTrace({ dir, now: () => 10_000 }) + trace.append(rec(9_000)) + assert.equal(trace.flush(), true, 'the bytes are durable once the file was synced and renamed') + assert.equal(trace.stats().failing, false, 'a refused directory sync is not a failed write') + assert.equal(seen.files, 1, 'the segment itself was synced before the directory was') + assert.equal(trace.flush(), true, 'a flush with nothing new succeeds too') + assert.equal(seen.files, 1, 'a saved segment is not rewritten by the next flush') + trace.close() + const reopened = openTrace({ dir, now: () => 10_000 }) + assert.deepEqual(reopened.records().map(r => r.t), [9_000]) + reopened.close() +}) + +test('a directory fsync refused with EINVAL is tolerated the same way', (t) => { + const dir = tmp() + t.after(() => rmSync(dir, { recursive: true, force: true })) + const seen = refuseDirectorySync(t, 'EINVAL') + const trace = openTrace({ dir, now: () => 10_000 }) + trace.append(rec(9_000)) + assert.equal(trace.flush(), true) + assert.equal(trace.stats().failing, false) + assert.equal(seen.files, 1) + trace.close() + const reopened = openTrace({ dir, now: () => 10_000 }) + assert.deepEqual(reopened.records().map(r => r.t), [9_000]) + reopened.close() +}) + +test('a rotation still prunes to the cap when the directory fsync is refused', (t) => { + const dir = tmp() + t.after(() => rmSync(dir, { recursive: true, force: true })) + refuseDirectorySync(t, 'EPERM') + const trace = openTrace({ dir, segmentRecords: 2, segments: 2, flushMs: 60_000, now: () => 1_000_000 }) + for (const at of [1, 2, 3, 4, 5, 6]) trace.append(rec(at)) + assert.equal(trace.flush(), true) + assert.deepEqual(files(dir).filter(name => /^trace-\d{6}\.json$/.test(name)), ['trace-000002.json', 'trace-000003.json']) + assert.deepEqual(trace.records().map(r => r.t), [3, 4, 5, 6]) + trace.close() +})