mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
gateway: serialize write-then-rename per file
Every JSON store (ping, store, news, sync, admin auth, config) wrote to one shared "<file>.tmp" and renamed it. Parallel requests raced on that path: one rename won, the others failed with ENOENT, and overlapping writes could leave a mangled file that reads as empty after a restart. Seen live from a parallel /api/ping probe. writeFileAtomic() in net.ts queues writes per file, so the last caller's data always lands last. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
54a580dc39
commit
f2d9115782
8 files changed
+55
-28
No files matched your search
@@ -0,0 +1,20 @@
|
||||
// Concurrent saves to one file used to share a .tmp path: all but one rename
|
||||
// failed with ENOENT (seen live from a parallel /api/ping probe).
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { writeFileAtomic } from "../dist/net.js";
|
||||
|
||||
test("parallel writes to one file all succeed, last one wins", async () => {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), "ws3-atomic-"));
|
||||
try {
|
||||
const file = path.join(dir, "data.json");
|
||||
await Promise.all(Array.from({ length: 50 }, (_, i) => writeFileAtomic(file, JSON.stringify({ i, pad: "x".repeat(i * 500) }))));
|
||||
assert.equal(JSON.parse(readFileSync(file, "utf-8")).i, 49);
|
||||
assert.deepEqual(readdirSync(dir), ["data.json"]);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user