Skip to content

Commit 8ff72cc

Browse files
OnurGumusclaude
andcommitted
Engine: a step that crashes ends by itself too, unless something it left keeps the process alive
An exception still exited at once through process.exit, which can hang for good (nodejs/node#64274). Now Cv.finish prints it, sets the exit status to 1 and lets the process end by itself; only if something still keeps it alive a second later does process.exit end it (an unref'd timer, so it never keeps the process alive itself). So that a crash leaves as little as possible behind: when a Chrome launch or a render throws, withChrome and Scan close the page server and the browser before the exception goes on (closingOnError, Render.closeQuietly), and Narrate releases the voice Worker when voicing throws. Scan's sampling moves into its own function for that. Under the exit-hold amplifier, a Chrome that cannot launch and a voice model that throws mid-voicing hang 40/40 each with process.exit and 0/40 now, with the same stderr and exit code. The 37 failure cases are unchanged; the Chrome-missing case ends in 0.27 s as before. A mini render (video, stills, scan) gives the same logs, mp4, stills, scan.json and webm frames as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a477dbd commit 8ff72cc

10 files changed

Lines changed: 256 additions & 157 deletions

File tree

‎engine/cli/Cv.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { run, describe as describe_1, Failure_$reflection as Failure_$reflection
77
import { run as run_9, describe as describe_2, Failure_$reflection as Failure_$reflection_2 } from "./History.js";
88
import { run as run_5, describe as describe_3, Failure_$reflection as Failure_$reflection_3 } from "./Present.js";
99
import { startAsPromise } from "./fable_modules/fable-library-js.5.19.0/Async.js";
10-
import { argv, resolve, engineDir, copyFile, mkdirp, isDir, writeText, readText, join, exists, exit, setExitCode, eprint } from "./Node.js";
10+
import { argv, resolve, engineDir, copyFile, mkdirp, isDir, writeText, readText, join, exists, exitUnlessDone, setExitCode, eprint } from "./Node.js";
1111
import { singleton as singleton_1 } from "./fable_modules/fable-library-js.5.19.0/AsyncBuilder.js";
1212
import { FSharpResult$2 } from "./fable_modules/fable-library-js.5.19.0/Result.js";
1313
import { run as run_1, requireReady } from "./Setup.js";
@@ -90,13 +90,13 @@ export function describe(failure) {
9090

9191
/**
9292
* Runs a step to its end, then ends with its code; an Error prints its lines and ends with its code; an exception
93-
* is printed and exits with 1. The engine's one Async.StartAsPromise, and the one caller of process.exit: every
93+
* is printed and ends with 1. The engine's one Async.StartAsPromise, and the one caller of process.exit: every
9494
* step below is an Async. A step that returns (Ok whatever its code, or Error) only sets process.exitCode: the
9595
* process ends by itself once the event loop is empty, so stdout reaches a slow pipe whole and V8 is torn down
9696
* before its threads are joined (process.exit joins them first and, rarely, hangs there for good:
9797
* nodejs/node#64274). A step leaves nothing running when it returns: Chrome and the page server are closed, and the
98-
* voice Worker is released, also when voicing fails. An exception exits at once, as it can leave any of them
99-
* running.
98+
* voice Worker is released, also when voicing fails. An exception can leave any of them running, so it also ends
99+
* by itself when nothing is left, and with process.exit a second later when something is.
100100
*/
101101
export function finish(step) {
102102
startAsPromise(step).then((outcome) => {
@@ -112,7 +112,7 @@ export function finish(step) {
112112
}
113113
}).catch((e) => {
114114
eprint(toString(e));
115-
exit(1);
115+
exitUnlessDone(1, 1000);
116116
});
117117
}
118118

‎engine/cli/Narrate.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Operators_IsNull } from "./fable_modules/fable-library-js.5.19.0/FSharp
1212
import { tryPick, iterate, sumBy, last as last_2, tryFind, item as item_1, length, indexed, sort, concat, unzip, exists as exists_1, singleton, append, filter, empty, head, tail as tail_1, cons, fold, reverse, mapIndexed, toArray, mapFold, isEmpty, map as map_2, ofArray } from "./fable_modules/fable-library-js.5.19.0/List.js";
1313
import { defaultArg, some, value as value_22 } from "./fable_modules/fable-library-js.5.19.0/Option.js";
1414
import { FSharpSet__Contains, toList as toList_1, ofSeq } from "./fable_modules/fable-library-js.5.19.0/Set.js";
15-
import { mkdirp, parseJson, readText, resolve, writeText, rename, toJson, sha1Hex, readJson, dirname, requireFromHome, deliver, url, fromJs, nodeModule, engineDir, toolHome, join as join_1, exists } from "./Node.js";
15+
import { mkdirp, parseJson, readText, resolve, closingOnError, writeText, rename, toJson, sha1Hex, readJson, dirname, requireFromHome, deliver, url, fromJs, nodeModule, engineDir, toolHome, join as join_1, exists } from "./Node.js";
1616
import { FSharpResult$2 } from "./fable_modules/fable-library-js.5.19.0/Result.js";
1717
import { FSharpMap__Remove, FSharpMap__Add, empty as empty_2, FSharpMap__TryFind, FSharpMap__ContainsKey, ofArray as ofArray_1, ofList } from "./fable_modules/fable-library-js.5.19.0/Map.js";
1818
import { singleton as singleton_2 } from "./fable_modules/fable-library-js.5.19.0/AsyncBuilder.js";
@@ -1810,7 +1810,7 @@ function narrate(settings) {
18101810
const markedScenes = settings.Marked.scenes;
18111811
const scenes = toList(delay(() => map((si) => [si, item(si, sceneObjs)], rangeDouble(0, 1, sceneObjs.length - 1))));
18121812
const emptyTrack = new Track(empty(), 0, empty());
1813-
const asyncResult = singleton_2.Delay(() => singleton_2.Bind(foldAR((tupledArg, tupledArg_1) => voiceScene(settings, markedScenes, tupledArg[0], tupledArg[1], tupledArg_1[0], tupledArg_1[1]), [emptyTrack, empty()], scenes), (_arg) => singleton_2.Bind(release(), () => singleton_2.Return(_arg))));
1813+
const asyncResult = singleton_2.Delay(() => singleton_2.Bind(closingOnError(release, foldAR((tupledArg, tupledArg_1) => voiceScene(settings, markedScenes, tupledArg[0], tupledArg[1], tupledArg_1[0], tupledArg_1[1]), [emptyTrack, empty()], scenes)), (_arg) => singleton_2.Bind(release(), () => singleton_2.Return(_arg))));
18141814
return singleton_2.Bind(asyncResult, (input_1_1) => {
18151815
const input_6 = input_1_1;
18161816
if (input_6.tag === 1) {

‎engine/cli/Node.js‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import * as node$003Acrypto from "node:crypto";
88
import * as node$003Achild_process from "node:child_process";
99
import * as node$003Ahttp from "node:http";
1010
import * as node$003Amodule from "node:module";
11-
import { singleton as singleton_1, sort, tail, isEmpty, empty, ofArray, toArray } from "./fable_modules/fable-library-js.5.19.0/List.js";
11+
import { singleton as singleton_2, sort, tail, isEmpty, empty, ofArray, toArray } from "./fable_modules/fable-library-js.5.19.0/List.js";
1212
import { Operators_IsNull } from "./fable_modules/fable-library-js.5.19.0/FSharp.Core.js";
13-
import { singleton, map, collect, delay, toList } from "./fable_modules/fable-library-js.5.19.0/Seq.js";
13+
import { singleton } from "./fable_modules/fable-library-js.5.19.0/AsyncBuilder.js";
14+
import { singleton as singleton_1, map, collect, delay, toList } from "./fable_modules/fable-library-js.5.19.0/Seq.js";
1415
import { disposeSafe, getEnumerator, defaultOf, uncurry2, comparePrimitives } from "./fable_modules/fable-library-js.5.19.0/Util.js";
1516
import { toString } from "./fable_modules/fable-library-js.5.19.0/Types.js";
1617
import { awaitPromise } from "./fable_modules/fable-library-js.5.19.0/Async.js";
@@ -110,6 +111,26 @@ export function setExitCode(code) {
110111
(process).exitCode = code;
111112
}
112113

114+
/**
115+
* `body`; when it throws, `cleanup` runs first and the exception then goes on as it was, so that a step that fails
116+
* part-way leaves nothing (a server, a browser, the voice Worker) keeping the process alive.
117+
*/
118+
export function closingOnError(cleanup, body) {
119+
return singleton.Delay(() => singleton.TryWith(singleton.Delay(() => singleton.ReturnFrom(body)), (_arg) => singleton.Bind(cleanup(), () => singleton.Return((() => {
120+
throw _arg;
121+
})()))));
122+
}
123+
124+
/**
125+
* Sets the exit status; the process ends by itself if nothing else keeps it alive, else `exit` ends it after `ms`.
126+
*/
127+
export function exitUnlessDone(code, ms) {
128+
setExitCode(code);
129+
setTimeout((() => {
130+
exit(code);
131+
}), ms).unref();
132+
}
133+
113134
export function cwd() {
114135
return (process).cwd();
115136
}
@@ -221,7 +242,7 @@ export function fileSize(p) {
221242
export function walk(dir) {
222243
return toList(delay(() => collect((name) => {
223244
const p = join(ofArray([dir, name]));
224-
return isDir(p) ? map((rest) => ((name + "/") + rest), walk(p)) : singleton(name);
245+
return isDir(p) ? map((rest) => ((name + "/") + rest), walk(p)) : singleton_1(name);
225246
}, sort(readDir(dir), {
226247
Compare: (x, y) => (comparePrimitives(x, y) | 0),
227248
}))));
@@ -289,7 +310,7 @@ export function runCapture(cmd, args) {
289310
* True when the command is on PATH.
290311
*/
291312
export function hasCommand(cmd) {
292-
return runCapture((platform === "win32") ? "where" : "which", singleton_1(cmd))[0] === 0;
313+
return runCapture((platform === "win32") ? "where" : "which", singleton_2(cmd))[0] === 0;
293314
}
294315

295316
export function awaitJs(p) {

‎engine/cli/Render.js‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
import { toArray, choose, reverse, chunkBySize, iterateIndexed, indexed, iterate, filter as filter_1, collect as collect_1, length, truncate, mapIndexed, append, exists as exists_1, map, empty, cons, tail, head, isEmpty, singleton as singleton_1, tryFind, ofArray } from "./fable_modules/fable-library-js.5.19.0/List.js";
3-
import { requireFromHome, childProcess, readDir, remove, eprint, run as run_1, writeBytes, fromJs, deliver, extname, fsp, awaitJs, sep as sep_3, join, engineDir, http, resolve, exists, env } from "./Node.js";
3+
import { requireFromHome, closingOnError, childProcess, readDir, remove, eprint, run as run_1, writeBytes, deliver, extname, fsp, awaitJs, sep as sep_3, join, engineDir, http, resolve, fromJs, exists, env } from "./Node.js";
44
import { Operators_IsNull } from "./fable_modules/fable-library-js.5.19.0/FSharp.Core.js";
55
import { defaultArg, value as value_9, ofNullable } from "./fable_modules/fable-library-js.5.19.0/Option.js";
6+
import { singleton } from "./fable_modules/fable-library-js.5.19.0/AsyncBuilder.js";
67
import { Record, Union } from "./fable_modules/fable-library-js.5.19.0/Types.js";
78
import { tuple_type, obj_type, option_type, list_type, int32_type, float64_type, class_type, bool_type, record_type, lambda_type, unit_type, string_type, union_type } from "./fable_modules/fable-library-js.5.19.0/Reflection.js";
89
import { empty as empty_2, FSharpMap__Add, FSharpMap__Remove, FSharpMap__TryFind, tryFind as tryFind_1, ofSeq } from "./fable_modules/fable-library-js.5.19.0/Map.js";
910
import { disposeSafe, getEnumerator, int32ToString, Exception, equals, comparePrimitives } from "./fable_modules/fable-library-js.5.19.0/Util.js";
10-
import { singleton } from "./fable_modules/fable-library-js.5.19.0/AsyncBuilder.js";
1111
import { padLeft, join as join_1, split, concat, substring } from "./fable_modules/fable-library-js.5.19.0/String.js";
1212
import { parallel, fromContinuations } from "./fable_modules/fable-library-js.5.19.0/Async.js";
1313
import { post, postAndAsyncReply, receive, start as start_1 } from "./fable_modules/fable-library-js.5.19.0/MailboxProcessor.js";
@@ -61,6 +61,18 @@ export function releaseChrome(browser) {
6161
}
6262
}
6363

64+
/**
65+
* Closes a browser after a failure, as far as it can: one that has crashed may not answer.
66+
*/
67+
export function closeQuietly(browser) {
68+
return singleton.Delay(() => singleton.Combine(singleton.TryWith(singleton.Delay(() => singleton.Bind(fromJs(browser.close()), () => singleton.Return(undefined))), (_arg_1) => {
69+
return singleton.Zero();
70+
}), singleton.Delay(() => {
71+
releaseChrome(browser);
72+
return singleton.Zero();
73+
})));
74+
}
75+
6476
/**
6577
* Who the server is for. The scan's server (scan.mjs) differs on purpose: it answers 404 for .wav (no audio is
6678
* needed to scan), knows fewer types, and sends no cache header. Its 404s show up in scan.json's logs, so keep them.
@@ -647,23 +659,30 @@ function withChrome(clip, job) {
647659
const matchValue = findChrome();
648660
if (matchValue != null) {
649661
const chrome = matchValue;
650-
const puppeteer = requireFromHome("puppeteer-core");
651-
return singleton.Bind(fromJs(puppeteer.launch({
652-
executablePath: chrome,
653-
headless: true,
654-
args: ["--font-render-hinting=none", "--force-color-profile=srgb", "--autoplay-policy=no-user-gesture-required"],
655-
})), (_arg_1) => {
656-
const browser = _arg_1;
657-
const session = Session_$ctor_Z6861C5C0(browser, server.Url);
658-
return singleton.Bind(Session__OpenPage(session), (_arg_2) => singleton.Bind(job(session, _arg_2), (_arg_3) => {
659-
const code = _arg_3 | 0;
660-
return singleton.Bind(fromJs(browser.close()), () => {
661-
releaseChrome(browser);
662-
server.Close();
663-
return singleton.Bind(Session__IsFailed(session), (_arg_5) => singleton.Return((code !== 0) ? code : (_arg_5 ? 1 : 0)));
664-
});
665-
}));
666-
});
662+
return singleton.ReturnFrom(closingOnError(() => singleton.Delay(() => {
663+
server.Close();
664+
return singleton.Zero();
665+
}), singleton.Delay(() => {
666+
const puppeteer = requireFromHome("puppeteer-core");
667+
return singleton.Bind(fromJs(puppeteer.launch({
668+
executablePath: chrome,
669+
headless: true,
670+
args: ["--font-render-hinting=none", "--force-color-profile=srgb", "--autoplay-policy=no-user-gesture-required"],
671+
})), (_arg_1) => {
672+
const browser = _arg_1;
673+
return singleton.ReturnFrom(closingOnError(() => closeQuietly(browser), singleton.Delay(() => {
674+
const session = Session_$ctor_Z6861C5C0(browser, server.Url);
675+
return singleton.Bind(Session__OpenPage(session), (_arg_2) => singleton.Bind(job(session, _arg_2), (_arg_3) => {
676+
const code = _arg_3 | 0;
677+
return singleton.Bind(fromJs(browser.close()), () => {
678+
releaseChrome(browser);
679+
server.Close();
680+
return singleton.Bind(Session__IsFailed(session), (_arg_5) => singleton.Return((code !== 0) ? code : (_arg_5 ? 1 : 0)));
681+
});
682+
}));
683+
})));
684+
});
685+
})));
667686
}
668687
else {
669688
eprint("no Chrome found: set CHROME to the browser\'s executable");

0 commit comments

Comments
 (0)