Skip to content

Commit

Permalink
Fix task killing by using built-in kill method (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
marioparaschiv committed Aug 10, 2024
1 parent 058a3ae commit 3b2eba0
Showing 1 changed file with 12 additions and 52 deletions.
64 changes: 12 additions & 52 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,8 @@ const cleanExit = async (message?: string | Error, exit?: boolean) => {
if (message) console.log(message);
exit = exit ?? true;

if (process.platform == "win32") {
if (child) {
await new Promise((resolve, reject) => {
exec(
"taskkill /pid " + child.pid + " /T /F",
(error: any, stdout: any, stderr: any) => {
if (error) {
console.warn(error);
}
resolve(stdout);
}
);
});
}
} else {
if (child) {
// For Linux/Darwin OS
await new Promise((resolve, reject) => {
process.kill(-child.pid);
if (exit) process.exit();
});
}
}
child?.kill();
if (exit) process.exit();
};
process.on("SIGINT", () => cleanExit());
process.on("SIGTERM", () => cleanExit());
Expand All @@ -101,7 +80,7 @@ const handleSpawn = (debug: boolean, fileName: string, port: number, filePath?:
const execPath = filePath ? `"${filePath}"` : `"${path.join(__dirname, fileName)}"`;
child = spawn(execPath, {
env: { WS_PORT: port.toString() },
shell: true,
shell: false,
windowsHide: true,
detached: process.platform !== "win32"
});
Expand Down Expand Up @@ -302,34 +281,15 @@ class Golang extends EventEmitter {
}

exit(): Promise<undefined> {
if (process.platform == "win32") {
return new Promise((resolve, reject) => {
this.server.close();
if (this.host) {
exec(
"taskkill /pid " + child.pid + " /T /F",
(error: any, stdout: any, stderr: any) => {
if (error) {
console.warn(error);
}
resolve(stdout ? stdout : stderr);
}
);
} else {
resolve(null);
}
});
} else {
return new Promise((resolve, reject) => {
this.server.close();
if (this.host) {
process.kill(-child.pid);
resolve(null);
} else {
resolve(null);
}
});
}
return new Promise((resolve, reject) => {
this.server.close();
if (this.host) {
child?.kill();
resolve(null);
} else {
resolve(null);
}
});
}
}
export interface CycleTLSClient {
Expand Down

0 comments on commit 3b2eba0

Please sign in to comment.