Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/processes.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { ChildProcess } from 'child_process';
7
import { promisify } from 'util';
8
import * as treekill from 'tree-kill';
9
import { Logger } from './logger';
10
11
export async function teardown(p: ChildProcess, logger: Logger, retryCount = 3): Promise<void> {
12
const pid = p.pid;
13
if (typeof pid !== 'number') {
14
return;
15
}
16
17
let retries = 0;
18
while (retries < retryCount) {
19
retries++;
20
21
try {
22
return await promisify(treekill)(pid);
23
} catch (error) {
24
try {
25
process.kill(pid, 0); // throws an exception if the process doesn't exist anymore
26
logger.log(`Error tearing down process (pid: ${pid}, attempt: ${retries}): ${error}`);
27
} catch (error) {
28
return; // Expected when process is gone
29
}
30
}
31
}
32
33
logger.log(`Gave up tearing down process client after ${retries} attempts...`);
34
}
35
36