fix(cutover): track direct detached listener pid

This commit is contained in:
chick
2026-07-18 17:51:40 +08:00
parent 020879f7e6
commit e4e2c32998
2 changed files with 21 additions and 13 deletions
+17 -9
View File
@@ -1,4 +1,4 @@
import { spawn, execFile } from 'node:child_process';
import { execFile, execFileSync } from 'node:child_process';
import { constants } from 'node:fs';
import {
access,
@@ -159,14 +159,22 @@ export const nodeCutoverSystem: CutoverSystem = {
throw new Error(`PID ${pid} did not exit after SIGTERM`);
},
start(argv, environment) {
const child = spawn(argv[0], argv.slice(1), {
detached: true,
stdio: 'ignore',
env: environment ?? process.env,
});
child.unref();
if (!child.pid) throw new Error('Failed to start command');
return child.pid;
const output = execFileSync(
'/usr/bin/python3',
[
'-c',
'import os,sys; pid=os.fork(); print(pid, flush=True); pid and sys.exit(0); os.setsid(); os.execve(sys.argv[1], sys.argv[1:], dict(os.environ))',
...argv,
],
{
encoding: 'utf8',
env: environment ?? process.env,
stdio: ['ignore', 'pipe', 'ignore'],
},
);
const pid = Number(output.trim());
if (!Number.isSafeInteger(pid) || pid <= 1) throw new Error('Failed to start command');
return pid;
},
delay: (milliseconds) => new Promise((resolveWait) => setTimeout(resolveWait, milliseconds)),
};