Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Worker: a status file write waits out a reader instead of stopping the worker

Adarsha8f03ccc5ac6
diff --git a/bin/lib/worker/config.mjs b/bin/lib/worker/config.mjs
index 0b7aa878..ecaa0604 100644
--- a/bin/lib/worker/config.mjs
+++ b/bin/lib/worker/config.mjs
@@ -75,11 +75,34 @@ function readJson(p, fallback) {
}
}
+/**
+ * renameSync that waits out a reader. On Windows a rename onto a file another program has open (a helper reading
+ * status.json or site.json at that moment) fails with EPERM, EACCES or EBUSY for a few milliseconds: try again a few
+ * times before giving up. Never throws for that alone: the next write comes within seconds.
+ */
+export function renameRetry(from, to, tries = 8) {
+ for (let i = 1; ; i++) {
+ try {
+ fs.renameSync(from, to);
+ return true;
+ } catch (e) {
+ if (!["EPERM", "EACCES", "EBUSY"].includes(e?.code)) throw e;
+ if (i >= tries) {
+ try {
+ fs.rmSync(from, { force: true });
+ } catch {}
+ return false;
+ }
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25 * i);
+ }
+ }
+}
+
function writePrivate(p, data) {
fs.mkdirSync(path.dirname(p), { recursive: true });
const tmp = `${p}.tmp`;
fs.writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
- fs.renameSync(tmp, p);
+ if (!renameRetry(tmp, p)) return;
try {
fs.chmodSync(p, 0o600);
} catch {}
diff --git a/bin/lib/worker/turns.mjs b/bin/lib/worker/turns.mjs
index 3a9bb4d7..00273e78 100644
--- a/bin/lib/worker/turns.mjs
+++ b/bin/lib/worker/turns.mjs
@@ -15,6 +15,7 @@
// decides what to do with them.
import fs from "node:fs";
import path from "node:path";
+import { renameRetry } from "./config.mjs";
const HOUR = 3_600_000;
@@ -49,7 +50,7 @@ export function writeSite(folder, body) {
const p = path.join(folder, "site.json");
const tmp = `${p}.${process.pid}.tmp`;
fs.writeFileSync(tmp, `${JSON.stringify(body)}\n`);
- fs.renameSync(tmp, p);
+ renameRetry(tmp, p);
}
/**
@@ -65,7 +66,7 @@ export function writeMode(folder, q, t) {
const p = path.join(folder, "mode.json");
const tmp = `${p}.${process.pid}.tmp`;
fs.writeFileSync(tmp, `${JSON.stringify({ mode: q.mode, localWork: q.localWork, until: finite(q.until) ? q.until : null, at: t })}\n`);
- fs.renameSync(tmp, p);
+ renameRetry(tmp, p);
return true;
}