diff --git a/apps/host/server/native-job-store.mjs b/apps/host/server/native-job-store.mjs index e09e9bd69e..3ee477fade 100644 --- a/apps/host/server/native-job-store.mjs +++ b/apps/host/server/native-job-store.mjs @@ -47,7 +47,13 @@ export function createJobStore(path, { return { save(jobs) { if (closed) return; - const text = JSON.stringify(jobs); + let text; + try { text = JSON.stringify(jobs); } + catch (error) { + // A value JSON cannot hold (a BigInt, a cycle) is a programming error, not a disk problem: say so and keep the last good file. + report(`[strata host] Job history could not be turned into JSON (${error.message}); the last saved history is kept, inference continues.`); + return; + } if (text === saved) { pending = null; if (timer !== null) clearTimer(timer); diff --git a/apps/host/tests/native-job-store.test.mjs b/apps/host/tests/native-job-store.test.mjs index cd7cb5fddd..c62ea5da2d 100644 --- a/apps/host/tests/native-job-store.test.mjs +++ b/apps/host/tests/native-job-store.test.mjs @@ -104,3 +104,17 @@ test('an unexpected polling callback failure does not leave polling stuck or rej assert.equal(changes,2); await controller.close(); }); + +// A value JSON cannot hold is reported and skipped like a disk failure: save() never throws out of emit() (GUI-1116 review of #5802). +test('a job list JSON cannot hold is reported, the last good file stays, and save does not throw', () => { + const f=fixture(); + f.store.save([{id:'one',state:'done'}]); + const cyclic={id:'two'};cyclic.self=cyclic; + assert.doesNotThrow(()=>f.store.save([cyclic])); + assert.doesNotThrow(()=>f.store.save([{id:'three',size:1n}])); + assert.deepEqual(JSON.parse(fs.readFileSync(f.path)),[{id:'one',state:'done'}]); + assert.equal(f.warnings.filter(w=>/could not be turned into JSON/.test(w)).length,2); + f.store.save([{id:'four',state:'done'}]); + assert.deepEqual(JSON.parse(fs.readFileSync(f.path)),[{id:'four',state:'done'}],'the next good list is saved'); + f.store.close(); +});