3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Retry a blocked status.json rename instead of failing the job (#2270)
Nguyen Xuan An32f67854e2f0
diff --git a/changes/2270.md b/changes/2270.mdnew file mode 100644index 00000000..c07ace02--- /dev/null+++ b/changes/2270.md@@ -0,0 +1 @@+- Render jobs retry a blocked `status.json` rename for under a second and keep the last good status with a warning instead of failing the job (#2270).diff --git a/docs/agent-render-jobs.md b/docs/agent-render-jobs.mdindex ea900b5e..1eb75ac5 100644--- a/docs/agent-render-jobs.md+++ b/docs/agent-render-jobs.md@@ -379,6 +379,8 @@ The controller rejects links, file aliases, unknown filenames, foreign directoriIt verifies completed artifact hashes when reopening a store.It removes files individually during release, without recursive deletion.+A program that polls `status.json` can hold it open, and on Windows that blocks the rename that replaces it. The controller retries that rename for under a second on `EPERM`, `EBUSY` and `EACCES`. If the file stays held, the last good `status.json` stays on disk, a `CRUCIBLE_STATUS_BLOCKED` process warning is emitted and the job carries on. `scene_render_status` answers from memory, so prefer it to reading the file.+The lock protects cooperating controller processes.The filesystem must remain private to the owner while jobs run.It does not isolate an arbitrary callback or hostile concurrent filesystem writer.diff --git a/scripts/agent-render-jobs.mjs b/scripts/agent-render-jobs.mjsindex ca493d79..add3c7e3 100644--- a/scripts/agent-render-jobs.mjs+++ b/scripts/agent-render-jobs.mjs@@ -381,6 +381,9 @@ function allowedFile(name) {}const FILE_IN_USE_CODES = new Set(['EBUSY', 'EPERM', 'EACCES']);+// 7 attempts with 10 ms doubling waits is 630 ms in the worst case.+const STATUS_RENAME_ATTEMPTS = 7;+const STATUS_RENAME_BACKOFF_MS = 10;async function regularFile(path) {const details = await lstat(path);@@ -445,7 +448,7 @@ export async function createAgentRenderJobs({ root, run, acquireGpu, limits: inp// or the disk fills. Where hard links are not supported it falls back to creating the target with `wx` and writing into it.// The file operations used here can be replaced through `lockTakeoverHooks.fileOperations`, so tests can make one of them fail.const removeJobFile = lockTakeoverHooks.removeJobFile ?? unlink;- const publishOperations = { writeFile, link, open, unlink, ...lockTakeoverHooks.fileOperations };+ const publishOperations = { writeFile, link, open, unlink, rename, ...lockTakeoverHooks.fileOperations };// Removing the temporary name can fail for a moment (a virus scanner holding the file on Windows), so it is retried with a// short backoff. The target must never be left with two names: that makes it fail the single-link check of the store.@@ -808,6 +811,27 @@ export async function createAgentRenderJobs({ root, run, acquireGpu, limits: inpreturn jobs.get(jobId);}+ // A program that polls status.json (an agent's shell on Windows) can hold it open for a moment, and the rename onto it then fails+ // with EPERM, EBUSY or EACCES. Retry for under a second. If it stays blocked, the last good status.json stays on disk, the job+ // keeps running from its in-memory record, and a warning says so: a status write must never fail a render.+ async function replaceStatusFile(job, pending, target) {+ for (let attempt = 0; attempt < STATUS_RENAME_ATTEMPTS; attempt += 1) {+ try {+ await publishOperations.rename(pending, target);+ return;+ } catch (error) {+ if (!FILE_IN_USE_CODES.has(error.code)) throw error;+ if (attempt === STATUS_RENAME_ATTEMPTS - 1) {+ job.statusWarning = `Could not update status.json (${error.code}); another program holds it open. The previous status stays on disk until it is released.`;+ process.emitWarning(job.statusWarning, { code: 'CRUCIBLE_STATUS_BLOCKED' });+ await publishOperations.unlink(pending).catch(() => {});+ return;+ }+ await new Promise(resolveWait => setTimeout(resolveWait, STATUS_RENAME_BACKOFF_MS * 2 ** attempt));+ }+ }+ }+function persist(job, record = job.record) {const text = JSON.stringify(record);// A write that failed already told its own caller; it must not make every later write reject with the same old error.@@ -815,7 +839,7 @@ export async function createAgentRenderJobs({ root, run, acquireGpu, limits: inpconst directory = await directoryFor(job.record.jobId);const pending = join(directory, 'status.json.pending');await writeFile(pending, text, { mode: 0o600 });- await rename(pending, join(directory, 'status.json'));+ await replaceStatusFile(job, pending, join(directory, 'status.json'));});return job.persistence;}diff --git a/tests/agent-render-jobs.test.js b/tests/agent-render-jobs.test.jsindex 006bde34..d93fd480 100644--- a/tests/agent-render-jobs.test.js+++ b/tests/agent-render-jobs.test.js@@ -1,6 +1,6 @@import nodeTest from 'node:test';import assert from 'node:assert/strict';-import { mkdtemp, mkdir, writeFile, readFile, readdir, rm, link, unlink, open } from 'node:fs/promises';+import { mkdtemp, mkdir, writeFile, readFile, readdir, rm, link, unlink, open, rename as renameFile } from 'node:fs/promises';import { watch, writeFileSync } from 'node:fs';import { tmpdir, constants, setPriority } from 'node:os';import { join } from 'node:path';@@ -992,3 +992,54 @@ test('a GPU queue that fails the wait fails the job with the queue message', asyassert.match(failed.reason, /GPU queue command did not grant/);await controller.close();});++function blockedRename(failuresBeforeSuccess, code) {+ const counter = { failures: 0 };+ const rename = async (from, to) => {+ if (to.endsWith('status.json') && counter.failures < failuresBeforeSuccess) {+ counter.failures += 1;+ throw Object.assign(new Error(`${code}: operation not permitted, rename`), { code });+ }+ return renameFile(from, to);+ };+ return { counter, rename };+}++test('a job survives a status.json rename that another reader blocks a few times', async context => {+ const root = await temporary(context);+ const { counter, rename } = blockedRename(3, 'EPERM');+ const controller = await createAgentRenderJobs({ root, limits, run: writeImage, lockTakeoverHooks: { fileOperations: { rename } } });+ const submitted = await controller.submit(request());+ const finished = await terminal(controller, submitted.jobId);+ assert.equal(finished.state, 'completed');+ assert.equal(counter.failures, 3);+ const onDisk = JSON.parse(await readFile(join(root, submitted.jobId, 'status.json'), 'utf8'));+ assert.equal(onDisk.state, 'completed');+});++test('a status.json rename that stays blocked keeps the last good status and does not fail the job', async context => {+ const root = await temporary(context);+ const { rename } = blockedRename(Infinity, 'EBUSY');+ const warnings = [];+ const onWarning = warning => warnings.push(warning.code);+ process.on('warning', onWarning);+ context.after(() => process.off('warning', onWarning));+ const controller = await createAgentRenderJobs({ root, limits, run: writeImage, lockTakeoverHooks: { fileOperations: { rename } } });+ const submitted = await controller.submit(request());+ const finished = await terminal(controller, submitted.jobId);+ assert.equal(finished.state, 'completed');+ await new Promise(resolve => setImmediate(resolve));+ assert.ok(warnings.includes('CRUCIBLE_STATUS_BLOCKED'));+ const onDisk = JSON.parse(await readFile(join(root, submitted.jobId, 'status.json'), 'utf8'));+ assert.equal(onDisk.jobId, submitted.jobId);+ assert.ok(!(await readdir(join(root, submitted.jobId))).includes('status.json.pending'));+});++test('a status.json rename that fails for another reason still fails the write', async context => {+ const root = await temporary(context);+ const { rename } = blockedRename(Infinity, 'ENOSPC');+ const controller = await createAgentRenderJobs({ root, limits, run: writeImage, lockTakeoverHooks: { fileOperations: { rename } } });+ const submitted = await controller.submit(request());+ const finished = await terminal(controller, submitted.jobId);+ assert.equal(finished.state, 'failed');+});