|
1 | | -import treeKill from 'tree-kill' |
2 | | - |
3 | 1 | import {TestInstance} from '../types' |
4 | | -import {getConfig} from "./config"; |
| 2 | +import {killProc} from "./process-helpers"; |
5 | 3 |
|
6 | 4 | const isWin = process.platform === "win32"; |
7 | 5 |
|
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 | | - |
64 | 6 | 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'), |
67 | 9 | write: (instance: TestInstance, props: {value: string}) => |
68 | 10 | instance.stdin.write(props.value), |
69 | 11 | } |
|
0 commit comments