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 lock from before the computer last started is taken over

Adarshb7ecfd390462
diff --git a/bin/lib/worker/lock.mjs b/bin/lib/worker/lock.mjs
index 9c7cb2c0..c7964a0f 100644
--- a/bin/lib/worker/lock.mjs
+++ b/bin/lib/worker/lock.mjs
@@ -4,9 +4,13 @@
// whose worker has stopped (its process is gone, or its process id now belongs to another program) is taken over.
import crypto from "node:crypto";
import fs from "node:fs";
+import os from "node:os";
import path from "node:path";
import { commandLineOf } from "./comfy.mjs";
+/** A lock this much older than the computer's start is from before it (the slack covers the clock and the uptime's rounding). */
+const BOOT_SLACK_MS = 60_000;
+
/** Another worker runs on this worker folder. */
export class WorkerRunning extends Error {
constructor(pid, file) {
@@ -40,6 +44,10 @@ export async function workerLives(rec, o = {}) {
const pid = Number(rec?.pid);
if (!Number.isInteger(pid) || pid <= 0) return false;
if (pid === process.pid) return [...held.values()].includes(rec.id);
+ // A lock written before this computer last started: its worker stopped with the computer, and its process id may
+ // now belong to another program (one whose command line can't be read would count as a worker's below).
+ const bootAt = (o.now ?? Date.now)() - (o.uptimeS ?? os.uptime()) * 1000;
+ if (typeof rec.at === "number" && rec.at < bootAt - BOOT_SLACK_MS) return false;
if (!running(pid)) return false;
const line = await (o.commandLineOf ?? commandLineOf)(pid);
if (line === null || line === undefined) return true;
diff --git a/tests/worker-parts.test.ts b/tests/worker-parts.test.ts
index 9d952837..cff66ea8 100644
--- a/tests/worker-parts.test.ts
+++ b/tests/worker-parts.test.ts
@@ -402,6 +402,14 @@ describe("one worker at a time on a worker folder", () => {
}
// Its command line can't be read (another user's process): it may be a worker, so refused.
await expect(takeLock(dir, { commandLineOf: async () => null })).rejects.toBeInstanceOf(WorkerRunning);
+ // ...unless the lock is from before the computer last started: after a crash its process id went to another
+ // program, so it is taken over.
+ const now = Date.now();
+ fs.writeFileSync(lockFile(dir), JSON.stringify({ pid: other.pid, id: "x", script: "/x/bin/fuobe.mjs", at: now - 3_600_000 }));
+ (await takeLock(dir, { commandLineOf: async () => null, now: () => now, uptimeS: 600 })).release();
+ // A lock written since the start still counts.
+ fs.writeFileSync(lockFile(dir), JSON.stringify({ pid: other.pid, id: "x", script: "/x/bin/fuobe.mjs", at: now - 300_000 }));
+ await expect(takeLock(dir, { commandLineOf: async () => null, now: () => now, uptimeS: 600 })).rejects.toBeInstanceOf(WorkerRunning);
expect(await workerLives({ pid: other.pid, id: "x", script: "" }, { commandLineOf: async () => "node other.js" })).toBe(false);
} finally {
other.kill();