mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
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
21 lines
929 B
JavaScript
21 lines
929 B
JavaScript
// 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 });
|
|
}
|
|
});
|