Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Log utility process output and unexpected exits

Manav Rathi6b692dd9e116
diff --git a/desktop/src/main/services/workers.ts b/desktop/src/main/services/workers.ts
index 916292ceb6..a67ddb9ce0 100644
--- a/desktop/src/main/services/workers.ts
+++ b/desktop/src/main/services/workers.ts
@@ -108,17 +108,56 @@ export const triggerCreateUtilityProcess = (
const terminateMLProcessIfRunning = () => {
if (_utilityProcessML) {
log.debug(() => "Terminating running ML utility process");
+ _utilityProcessML.removeAllListeners("exit");
_utilityProcessML.kill();
_utilityProcessML = undefined;
}
};
+/**
+ * Fork a utility process running {@link scriptName}, mirroring its stdout and
+ * stderr into our log (prefixed with {@link logTag}), and logging an error if
+ * it exits on its own.
+ *
+ * The usual log flow for utility processes (see `log-worker.ts`) requires
+ * their module graph to have loaded. Without this mirroring, a crash during
+ * module load (e.g. a missing native library) would be written only to the
+ * inherited stderr, which is invisible in packaged apps, and the log file
+ * would not contain any trace of it.
+ *
+ * Callers that deliberately kill the returned process should remove its
+ * "exit" listeners first.
+ */
+const forkWatchedUtilityProcess = (scriptName: string, logTag: string) => {
+ const child = utilityProcess.fork(path.join(__dirname, scriptName), [], {
+ stdio: "pipe",
+ });
+ child.stdout?.on("data", (chunk: Buffer) => {
+ process.stdout.write(chunk);
+ log.info(`${logTag} ${String(chunk).trimEnd()}`);
+ });
+ child.stderr?.on("data", (chunk: Buffer) => {
+ process.stderr.write(chunk);
+ log.error(`${logTag} ${String(chunk).trimEnd()}`);
+ });
+ child.on("exit", (code) => {
+ log.error(`${logTag} utility process exited with code ${code}`);
+ });
+ return child;
+};
+
+// Electron tears down utility processes on quit; don't report those exits.
+app.on("before-quit", () => {
+ _utilityProcessML?.removeAllListeners("exit");
+ _utilityProcessFFmpeg?.removeAllListeners("exit");
+});
+
export const triggerCreateMLUtilityProcess = (window: BrowserWindow) => {
terminateMLProcessIfRunning();
const { port1, port2 } = new MessageChannelMain();
- const child = utilityProcess.fork(path.join(__dirname, "ml-worker.js"));
+ const child = forkWatchedUtilityProcess("ml-worker.js", "[ml-worker]");
const userDataPath = app.getPath("userData");
child.postMessage(/* MLWorkerInitData */ { userDataPath }, [port1]);
@@ -193,6 +232,7 @@ export const ffmpegUtilityProcessEndpoint = () =>
const terminateFFmpegProcessIfRunning = () => {
if (_utilityProcessFFmpeg) {
log.debug(() => "Terminating running FFmpeg utility process");
+ _utilityProcessFFmpeg.removeAllListeners("exit");
_utilityProcessFFmpeg.kill();
_utilityProcessFFmpeg = undefined;
_utilityProcessFFmpegEndpoint = undefined;
@@ -210,7 +250,10 @@ const createFFmpegUtilityProcessEndpoint = () => {
const { port1, port2 } = new MessageChannelMain();
- const child = utilityProcess.fork(path.join(__dirname, "ffmpeg-worker.js"));
+ const child = forkWatchedUtilityProcess(
+ "ffmpeg-worker.js",
+ "[ffmpeg-worker]",
+ );
// Send a handle to the port (one end of the message channel) to the utility
// process (alongwith any other init data). The utility process will reply
// with an "ack" when it get it.