Skip to content

Commit 21c4435

Browse files
authored
test: do kill testProcesses accordingly (#9818)
* test: do kill testProcesses accordingly * fix it
1 parent a8d276e commit 21c4435

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

tests/config/commonFixtures.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import net from 'net';
2121
type TestChildParams = {
2222
command: string[],
2323
cwd?: string,
24-
env?: { [key: string]: string | number | boolean | undefined },
24+
env?: NodeJS.ProcessEnv,
2525
shell?: boolean,
2626
onOutput?: () => void;
2727
};
@@ -42,9 +42,13 @@ export class TestChildProcess {
4242
env: {
4343
...process.env,
4444
...params.env,
45-
} as any,
45+
},
4646
cwd: params.cwd,
4747
shell: params.shell,
48+
// On non-windows platforms, `detached: true` makes child process a leader of a new
49+
// process group, making it possible to kill child process tree with `.kill(-pid)` command.
50+
// @see https://nodejs.org/api/child_process.html#child_process_options_detached
51+
detached: process.platform !== 'win32',
4852
});
4953
if (process.env.PWTEST_DEBUG)
5054
process.stdout.write(`\n\nLaunching ${params.command.join(' ')}\n`);
@@ -63,32 +67,34 @@ export class TestChildProcess {
6367
this.process.stderr.on('data', appendChunk);
6468
this.process.stdout.on('data', appendChunk);
6569

66-
const onExit = () => {
67-
if (!this.process.pid || this.process.killed)
68-
return;
69-
try {
70-
if (process.platform === 'win32')
71-
execSync(`taskkill /pid ${this.process.pid} /T /F /FI "MEMUSAGE gt 0"`);
72-
else
73-
process.kill(-this.process.pid, 'SIGKILL');
74-
} catch (e) {
75-
// the process might have already stopped
76-
}
77-
};
78-
process.on('exit', onExit);
70+
const killProcessGroup = this._killProcessGroup.bind(this);
71+
process.on('exit', killProcessGroup);
7972
this.exited = new Promise(f => {
8073
this.process.on('exit', (exitCode, signal) => f({ exitCode, signal }));
81-
process.off('exit', onExit);
74+
process.off('exit', killProcessGroup);
8275
});
8376
this.exitCode = this.exited.then(r => r.exitCode);
8477
}
8578

8679
async close() {
8780
if (!this.process.killed)
88-
this.process.kill();
81+
this._killProcessGroup();
8982
return this.exited;
9083
}
9184

85+
private _killProcessGroup() {
86+
if (!this.process.pid || this.process.killed)
87+
return;
88+
try {
89+
if (process.platform === 'win32')
90+
execSync(`taskkill /pid ${this.process.pid} /T /F /FI "MEMUSAGE gt 0"`);
91+
else
92+
process.kill(-this.process.pid, 'SIGKILL');
93+
} catch (e) {
94+
// the process might have already stopped
95+
}
96+
}
97+
9298
async cleanExit() {
9399
const r = await this.exited;
94100
if (r.exitCode)

0 commit comments

Comments
 (0)