From 072987dab20f352ba5a9a2a8cbe937b834e04e55 Mon Sep 17 00:00:00 2001 From: Paul Rouget Date: Wed, 18 Oct 2023 17:37:35 +0800 Subject: [PATCH] Kill any processes started by coapp on exit --- app/src/converter.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/converter.js b/app/src/converter.js index 82183c0d..875af50f 100644 --- a/app/src/converter.js +++ b/app/src/converter.js @@ -2,7 +2,6 @@ import open from 'open'; const os = require("os"); const path = require('path'); -const { spawn } = require('child_process'); const logger = require('./logger'); const rpc = require('./weh-rpc'); @@ -16,6 +15,27 @@ if (os.platform() == "win32") { ffprobe += ".exe"; } +// Record all started processes, and kill them if the coapp +// ends, crash or is killed by the browser. +let to_kill = new Set(); +function spawn(arg0, argv) { + const { spawn } = require('child_process'); + let process = spawn(arg0, argv); + if (process.pid) { + to_kill.add(process); + process.on("exit", () => to_kill.delete(process)); + } + return process; +} +for (let e of ["exit", "SIGINT", "SIGTERM", "uncaughtException"]) { + process.on(e, () => { + for (let process of to_kill) { + try { process.kill(9); } catch (_) { /* */ } + } + process.exit(0); + }); +} + function ExecConverter(args) { return new Promise((resolve, reject) => { let convProcess = spawn(ffmpeg, args);