Skip to content

Commit dd250f8

Browse files
brian6932dannydasilva-dev
authored andcommitted
fix: Uncaught ESRCH on SIGINT/SIGTERM (#370)
1 parent 775bbe6 commit dd250f8

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/index.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,35 @@ export interface CycleTLSResponse {
6666
let child: ChildProcessWithoutNullStreams;
6767
let lastRequestID: string
6868

69-
const cleanExit = async (message?: string | Error, exit?: boolean) => {
70-
if (message) console.log(message);
71-
exit = exit ?? true;
69+
const
70+
cleanExit = (message?: string | Error) => {
71+
if (message) console.log(message);
7272

73-
if (child) {
74-
75-
if (process.platform == "win32") {
76-
child?.kill();
77-
} else {
78-
process.kill(-child.pid);
73+
if (child) {
74+
if (process.platform === "win32") {
75+
child?.kill();
76+
} else {
77+
try {
78+
process.kill(-child.pid);
79+
}
80+
catch (error) {
81+
if (error.code !== "ESRCH")
82+
throw Error(error);
83+
}
84+
}
7985
}
80-
if (exit) process.exit();
86+
},
87+
close = () => cleanExit();
8188

82-
}
83-
};
84-
process.on("SIGINT", () => cleanExit());
85-
process.on("SIGTERM", () => cleanExit());
89+
process
90+
.once("SIGINT", close)
91+
.once("SIGTERM", close);
8692

8793
const handleSpawn = (debug: boolean, fileName: string, port: number, filePath?: string) => {
8894
try {
8995
// Determine the executable path
9096
let execPath: string;
91-
97+
9298
if (filePath) {
9399
// If filePath is provided, use it directly
94100
execPath = filePath;
@@ -127,9 +133,9 @@ const handleSpawn = (debug: boolean, fileName: string, port: number, filePath?:
127133
cleanExit(new Error(errorMessage));
128134
} else {
129135
cleanExit(
130-
`Error Processing Request (please open an issue https://github.com/Danny-Dasilva/CycleTLS/issues/new/choose) -> ${errorMessage}`,
131-
false
132-
).then(() => handleSpawn(debug, fileName, port));
136+
`Error Processing Request (please open an issue https://github.com/Danny-Dasilva/CycleTLS/issues/new/choose) -> ${errorMessage}`
137+
)
138+
handleSpawn(debug, fileName, port);
133139
}
134140
}
135141
});

0 commit comments

Comments
 (0)