Skip to content

Commit 8e8ca92

Browse files
committed
test(node-host): write fake container-engine state atomically
checks-node-compact-large-3 failed twice on this branch in different tests of node-worker-supervisor.container.test.ts, most recently with "SyntaxError: Unexpected end of JSON input" raised from the fake engine's load() while the supervisor ran `docker rm --force`. The shim saved container state with a truncating fs.writeFileSync while sibling shim invocations (rm, inspect, wait, ps) read the same path, so a reader could observe the zero-length window and exit 1. Write to a ".pending" sibling and rename over the target instead; rename is atomic, so readers always see a complete previous or next state. The ps handler filters on the ".container.json" suffix, so pending files are ignored. A concurrent write/read probe over the old pattern produced 2014 partial reads in 212900; the same probe over write-then-rename produced 0 in 203468. Not reproducible end-to-end on macOS, where the truncation window is far narrower than on the loaded Linux shard.
1 parent 1480908 commit 8e8ca92

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/node-host/node-worker-supervisor.container.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ const args = process.argv.slice(2);
5555
const command = args[0];
5656
const statePath = (id) => path.join(engineRoot, id + ".container.json");
5757
const load = (id) => JSON.parse(fs.readFileSync(statePath(id), "utf8"));
58-
const save = (container) => fs.writeFileSync(statePath(container.id), JSON.stringify(container));
58+
// Sibling shim invocations (rm/inspect/wait) read this state while another
59+
// writes it. A truncating write exposes a zero-length window, so a reader
60+
// parses partial JSON and the shim exits 1; rename is atomic, so readers
61+
// always see either the previous or the next complete state.
62+
const save = (container) => {
63+
const target = statePath(container.id);
64+
const pending = target + "." + process.pid + ".pending";
65+
fs.writeFileSync(pending, JSON.stringify(container));
66+
fs.renameSync(pending, target);
67+
};
5968
const launchIdFor = (container) =>
6069
Buffer.from(container.labels["openclaw.node-worker.launch"], "base64url").toString("utf8");
6170
const journalState = (launchId) => {

0 commit comments

Comments
 (0)