3D engine that AI agents drive
#462: give the job store lock back when the MCP server shuts down
Nguyen Xuan And91bb3a162f8
diff --git a/changes/462-sunbounce-window.md b/changes/462-sunbounce-window.mdnew file mode 100644index 00000000..9d266960--- /dev/null+++ b/changes/462-sunbounce-window.md@@ -0,0 +1 @@+- Give the job store lock back when the MCP server shuts down (issue #462). The server now closes the render job store before the browser, so `store.lock` is removed first when a client closes the connection and kills the process a couple of seconds later, and the job controller also removes its own lock when the process exits without a clean close. The next run no longer stops with "Stale job store lock requires explicit repair".diff --git a/scripts/agent-render-jobs.mjs b/scripts/agent-render-jobs.mjsindex 6ec721a9..660e6987 100644--- a/scripts/agent-render-jobs.mjs+++ b/scripts/agent-render-jobs.mjs@@ -1,3 +1,4 @@+import { readFileSync, unlinkSync } from 'node:fs';import { mkdir, open, readFile, writeFile, rename, unlink, rmdir, readdir, lstat, realpath } from 'node:fs/promises';import { randomUUID, createHash } from 'node:crypto';import { isAbsolute, join, resolve } from 'node:path';@@ -265,6 +266,7 @@ export async function createAgentRenderJobs({ root, run, limits: inputLimits = {try { await lock.writeFile(JSON.stringify({ schemaVersion: 1, pid: process.pid, token: lockToken })); }finally { await lock.close(); }lockHeld = true;+ process.once('exit', releaseLockOnExit);} catch (error) {if (error.code !== 'EEXIST') throw error;await regularFile(lockPath);@@ -282,11 +284,24 @@ export async function createAgentRenderJobs({ root, run, limits: inputLimits = {}}+ // A process that ends without a clean close (an exit call, an uncaught error, a signal Node turns into an exit) still gives the+ // lock back, so the next run does not meet a "stale" lock. Only the owner's own token is removed.+ function releaseLockOnExit() {+ if (!lockHeld) return;+ try {+ if (JSON.parse(readFileSync(lockPath, 'utf8')).token === lockToken) unlinkSync(lockPath);+ } catch {+ // The lock is already gone or no longer ours: nothing to give back.+ }+ lockHeld = false;+ }+async function unlock() {if (!lockHeld) return;if ((await readJson(lockPath, 4096)).token !== lockToken) throw new Error('Job store lock ownership changed.');await unlink(lockPath);lockHeld = false;+ process.removeListener('exit', releaseLockOnExit);}async function directoryFor(jobId) {diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjsindex 5a470506..fab2461a 100644--- a/scripts/agent-runtime.mjs+++ b/scripts/agent-runtime.mjs@@ -910,8 +910,10 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browcancelReview?.(new Error('The agent runtime is closing.'));closing = (async () => {try {- await reviewPage?.close().catch(() => {});+ // The job store goes first: a client that closes the connection waits only a couple of seconds before it kills this+ // process, and the lock must be given back before the slower browser shutdown.if (jobsPromise) await (await jobsPromise).close();+ await reviewPage?.close().catch(() => {});} finally {await queue;await shutdownResources();diff --git a/tests/mcp-server-job-lock.test.js b/tests/mcp-server-job-lock.test.jsnew file mode 100644index 00000000..088466d6--- /dev/null+++ b/tests/mcp-server-job-lock.test.js@@ -0,0 +1,45 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { existsSync } from 'node:fs';+import { mkdtemp, rm } from 'node:fs/promises';+import { spawnSync } from 'node:child_process';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { randomUUID } from 'node:crypto';+import { Client } from '@modelcontextprotocol/client';+import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';++const serverEntry = new URL('../bin/67js-mcp.mjs', import.meta.url).pathname.replace(/^\/(\w:)/, '$1');+const hasBuild = existsSync(new URL('../build/mcp/server.mjs', import.meta.url));++// Starts the server on `jobsDirectory`, takes the job store lock with a status call on an unknown job (no browser needed),+// then closes the connection the way client.close() does and reports whether the lock file is still there.+async function startAskAndClose(jobsDirectory) {+ const transport = new StdioClientTransport({ command: process.execPath, args: [serverEntry, '--backend', 'cpu', '--jobs', jobsDirectory], stderr: 'ignore' });+ const client = new Client({ name: 'job-lock-test', version: '1.0.0' });+ await client.connect(transport);+ const result = await client.callTool({ name: 'scene_render_status', arguments: { jobId: randomUUID() } });+ assert.equal(result.isError, true, 'an unknown job is an error, not a crash');+ assert.ok(existsSync(join(jobsDirectory, 'store.lock')), 'the server holds the lock while it runs');+ await client.close();+ return existsSync(join(jobsDirectory, 'store.lock'));+}++test('the MCP server gives back store.lock when the client closes, so the next run starts', { skip: !hasBuild && 'run npm run build:lib first', timeout: 60000 }, async context => {+ const jobsDirectory = await mkdtemp(join(tmpdir(), '67js-job-lock-'));+ context.after(() => rm(jobsDirectory, { recursive: true, force: true }));+ assert.equal(await startAskAndClose(jobsDirectory), false, 'first run leaves no lock');+ assert.equal(await startAskAndClose(jobsDirectory), false, 'second run starts and leaves no lock');+});++test('a process that exits without closing the job controller still removes its lock', { timeout: 30000 }, async context => {+ const jobsDirectory = await mkdtemp(join(tmpdir(), '67js-job-lock-'));+ context.after(() => rm(jobsDirectory, { recursive: true, force: true }));+ const controllerUrl = new URL('../scripts/agent-render-jobs.mjs', import.meta.url).href;+ const source = `import { createAgentRenderJobs } from ${JSON.stringify(controllerUrl)};+ await createAgentRenderJobs({ root: ${JSON.stringify(jobsDirectory)}, run: async () => {} });+ process.exit(0);`;+ const child = spawnSync(process.execPath, ['--input-type=module', '-e', source], { encoding: 'utf8' });+ assert.equal(child.status, 0, child.stderr);+ assert.equal(existsSync(join(jobsDirectory, 'store.lock')), false);+});