fix(lifecycle): use process group kill (-pid) in handleExit safety net
With detached:true children, the synchronous exit handler must kill the entire process group, not just the direct PID.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartexit",
|
"name": "@push.rocks/smartexit",
|
||||||
"version": "2.0.1",
|
"version": "2.0.2",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.",
|
"description": "A library for managing graceful shutdowns of Node.js processes by handling cleanup operations, including terminating child processes.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
|||||||
@@ -187,13 +187,19 @@ export class ProcessLifecycle {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Synchronous last-resort: SIGKILL any remaining tracked PIDs. */
|
/** Synchronous last-resort: SIGKILL any remaining tracked process groups. */
|
||||||
private handleExit(): void {
|
private handleExit(): void {
|
||||||
const instances = ProcessLifecycle.getInstances();
|
const instances = ProcessLifecycle.getInstances();
|
||||||
let killed = 0;
|
let killed = 0;
|
||||||
|
|
||||||
for (const instance of instances) {
|
for (const instance of instances) {
|
||||||
for (const pid of instance.trackedPids) {
|
for (const pid of instance.trackedPids) {
|
||||||
|
// Kill entire process group (negative PID) for detached children
|
||||||
|
try {
|
||||||
|
process.kill(-pid, 'SIGKILL');
|
||||||
|
killed++;
|
||||||
|
} catch {
|
||||||
|
// Process group may not exist, try single PID
|
||||||
try {
|
try {
|
||||||
process.kill(pid, 'SIGKILL');
|
process.kill(pid, 'SIGKILL');
|
||||||
killed++;
|
killed++;
|
||||||
@@ -202,6 +208,7 @@ export class ProcessLifecycle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (killed > 0 && !this.options.silent) {
|
if (killed > 0 && !this.options.silent) {
|
||||||
console.error(`[smartexit] Exit handler: force-killed ${killed} remaining child processes`);
|
console.error(`[smartexit] Exit handler: force-killed ${killed} remaining child processes`);
|
||||||
|
|||||||
Reference in New Issue
Block a user