Skip to content

Commit 4525a27

Browse files
committed
chore: move sigterm and sigkill main logic elsewhere
1 parent 800ac51 commit 4525a27

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

src/event-map.ts

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,11 @@
1-
import treeKill from 'tree-kill'
2-
31
import {TestInstance} from '../types'
4-
import {getConfig} from "./config";
2+
import {killProc} from "./process-helpers";
53

64
const isWin = process.platform === "win32";
75

8-
const kill = (instance: TestInstance, signal: string | undefined) =>
9-
new Promise<void>((resolve, reject) => {
10-
if (!instance.pid || (instance.pid && instance.hasExit())) {
11-
resolve()
12-
return
13-
}
14-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15-
treeKill(instance.pid, signal, async err => {
16-
try {
17-
if (err) {
18-
if (
19-
err.message.includes('The process') &&
20-
err.message.includes('not found.')
21-
) {
22-
resolve()
23-
return
24-
}
25-
if (
26-
err.message.includes('could not be terminated') &&
27-
err.message.includes('There is no running instance of the task.')
28-
) {
29-
const sleep = (t: number) => new Promise(r => setTimeout(r, t))
30-
await sleep(getConfig().errorDebounceTimeout);
31-
if (instance.hasExit()) {
32-
resolve();
33-
return;
34-
}
35-
console.warn('Ran into error while trying to kill process:')
36-
console.warn(err.toString())
37-
console.warn(`This is likely due to Window's permissions.
38-
Because this error is prevalent on CI Windows systems with the tree-kill package, we are attempting
39-
an alternative kill method.`)
40-
console.warn()
41-
console.warn(
42-
'Be aware that this alternative kill method is not guaranteed to work with subprocesses, and they may not exit properly as a result.',
43-
)
44-
45-
const didKill = instance.kill(signal as 'SIGKILL')
46-
if (didKill) {
47-
resolve()
48-
} else {
49-
console.error(
50-
'Alternative kill method failed. Rejecting with original error.',
51-
)
52-
reject(err)
53-
}
54-
return
55-
}
56-
reject(err)
57-
} else resolve()
58-
} catch (e: unknown) {
59-
reject(e);
60-
}
61-
})
62-
})
63-
646
const eventMap = {
65-
sigterm: (instance: TestInstance) => kill(instance, isWin ? undefined : 'SIGTERM'),
66-
sigkill: (instance: TestInstance) => kill(instance, isWin ? undefined : 'SIGKILL'),
7+
sigterm: (instance: TestInstance) => killProc(instance, isWin ? undefined : 'SIGTERM'),
8+
sigkill: (instance: TestInstance) => killProc(instance, isWin ? undefined : 'SIGKILL'),
679
write: (instance: TestInstance, props: {value: string}) =>
6810
instance.stdin.write(props.value),
6911
}

src/process-helpers.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import treeKill from "tree-kill";
2+
import {TestInstance} from "../types";
3+
import {getConfig} from "./config";
4+
5+
export const killProc = (instance: TestInstance, signal: string | undefined) =>
6+
new Promise<void>((resolve, reject) => {
7+
if (!instance.pid || (instance.pid && instance.hasExit())) {
8+
resolve()
9+
return
10+
}
11+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
12+
treeKill(instance.pid, signal, async err => {
13+
try {
14+
if (err) {
15+
if (
16+
err.message.includes('The process') &&
17+
err.message.includes('not found.')
18+
) {
19+
resolve()
20+
return
21+
}
22+
if (
23+
err.message.includes('could not be terminated') &&
24+
err.message.includes('There is no running instance of the task.')
25+
) {
26+
const sleep = (t: number) => new Promise(r => setTimeout(r, t))
27+
await sleep(getConfig().errorDebounceTimeout);
28+
if (instance.hasExit()) {
29+
resolve();
30+
return;
31+
}
32+
console.warn('Ran into error while trying to kill process:')
33+
console.warn(err.toString())
34+
console.warn(`This is likely due to Window's permissions.
35+
Because this error is prevalent on CI Windows systems with the tree-kill package, we are attempting
36+
an alternative kill method.`)
37+
console.warn()
38+
console.warn(
39+
'Be aware that this alternative kill method is not guaranteed to work with subprocesses, and they may not exit properly as a result.',
40+
)
41+
42+
const didKill = instance.kill(signal as 'SIGKILL')
43+
if (didKill) {
44+
resolve()
45+
} else {
46+
console.error(
47+
'Alternative kill method failed. Rejecting with original error.',
48+
)
49+
reject(err)
50+
}
51+
return
52+
}
53+
reject(err)
54+
} else resolve()
55+
} catch (e: unknown) {
56+
reject(e);
57+
}
58+
})
59+
})

0 commit comments

Comments
 (0)