@theia/core 1.78.0-next.4 → 1.78.0-next.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/catalog.json +37 -10
- package/lib/node/process-utils.d.ts +125 -2
- package/lib/node/process-utils.d.ts.map +1 -1
- package/lib/node/process-utils.js +297 -25
- package/lib/node/process-utils.js.map +1 -1
- package/lib/node/process-utils.slow-spec.js +78 -3
- package/lib/node/process-utils.slow-spec.js.map +1 -1
- package/lib/node/process-utils.spec.js +237 -2
- package/lib/node/process-utils.spec.js.map +1 -1
- package/lib/node/test/exiting-backend.d.ts +5 -0
- package/lib/node/test/exiting-backend.d.ts.map +1 -1
- package/lib/node/test/exiting-backend.js +16 -3
- package/lib/node/test/exiting-backend.js.map +1 -1
- package/package.json +4 -4
- package/src/node/process-utils.slow-spec.ts +90 -3
- package/src/node/process-utils.spec.ts +268 -3
- package/src/node/process-utils.ts +342 -24
- package/src/node/test/exiting-backend.ts +17 -3
|
@@ -29,6 +29,47 @@ const WIN_LIST_CHILDREN_TIMEOUT_MS = 5000;
|
|
|
29
29
|
*/
|
|
30
30
|
const WIN_LIST_CHILDREN_OPTIONS = { windowsHide: true, timeout: WIN_LIST_CHILDREN_TIMEOUT_MS };
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The arguments of `ps` that list all processes with their parent, process group and start time.
|
|
34
|
+
* `lstart` goes last, as it contains spaces.
|
|
35
|
+
*/
|
|
36
|
+
const UNIX_LIST_PROCESSES_ARGS = ['-A', '-o', 'pid=,ppid=,pgid=,lstart='];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* How long to wait for `ps` to list all processes before giving up, in milliseconds.
|
|
40
|
+
*/
|
|
41
|
+
const UNIX_LIST_PROCESSES_TIMEOUT_MS = 1000;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How often to check whether processes that were sent `SIGTERM` have exited, in milliseconds.
|
|
45
|
+
*/
|
|
46
|
+
const UNIX_FORCE_KILL_POLL_INTERVAL_MS = 100;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A process as listed by `ps`.
|
|
50
|
+
*/
|
|
51
|
+
export interface UnixProcessInfo {
|
|
52
|
+
pid: number;
|
|
53
|
+
ppid: number;
|
|
54
|
+
pgid: number;
|
|
55
|
+
/**
|
|
56
|
+
* As printed by `ps`. Only meant to be compared with another listing, to tell a process apart from a later
|
|
57
|
+
* one with the same PID.
|
|
58
|
+
*/
|
|
59
|
+
startTime: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A process that was sent `SIGTERM` and is killed with `SIGKILL` if it is still running at its deadline.
|
|
64
|
+
*/
|
|
65
|
+
export interface UnixForceKillTarget {
|
|
66
|
+
startTime: string;
|
|
67
|
+
/**
|
|
68
|
+
* In milliseconds, on the monotonic clock of `performance.now()`.
|
|
69
|
+
*/
|
|
70
|
+
deadline: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
32
73
|
/**
|
|
33
74
|
* `@theia/core` service with some process-related utilities.
|
|
34
75
|
*/
|
|
@@ -38,6 +79,31 @@ export class ProcessUtils {
|
|
|
38
79
|
@inject(ILogger) @named('core:ProcessUtils')
|
|
39
80
|
protected readonly logger: ILogger;
|
|
40
81
|
|
|
82
|
+
/**
|
|
83
|
+
* How long the processes of a tree have to exit after `SIGTERM` before they are killed with `SIGKILL`,
|
|
84
|
+
* in milliseconds, see {@link unixTerminateProcessTree}.
|
|
85
|
+
*/
|
|
86
|
+
protected readonly unixForceKillDelay: number = 5000;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* How long {@link terminateChildProcessTrees} waits for processes to exit after `SIGTERM` before it kills
|
|
90
|
+
* them with `SIGKILL`, in milliseconds, measured from the start of the call. It is shorter than
|
|
91
|
+
* {@link unixForceKillDelay}, as it delays the exit of the current process. Listing the processes before
|
|
92
|
+
* `SIGKILL` can take up to another second.
|
|
93
|
+
*/
|
|
94
|
+
protected readonly unixChildProcessTreesForceKillDelay: number = 2000;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The processes that were sent `SIGTERM` and are to be killed with `SIGKILL` if they are still running
|
|
98
|
+
* at their deadline, by PID.
|
|
99
|
+
*/
|
|
100
|
+
protected readonly unixForceKillTargets = new Map<number, UnixForceKillTarget>();
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The timer of {@link unixScheduleForceKill}.
|
|
104
|
+
*/
|
|
105
|
+
protected unixForceKillTimer: ReturnType<typeof setTimeout> | undefined;
|
|
106
|
+
|
|
41
107
|
/**
|
|
42
108
|
* Kills the given process together with all of its descendants.
|
|
43
109
|
*
|
|
@@ -55,7 +121,8 @@ export class ProcessUtils {
|
|
|
55
121
|
|
|
56
122
|
/**
|
|
57
123
|
* Kills all of the descendants of the given process, but not the process itself, without blocking
|
|
58
|
-
* the event loop.
|
|
124
|
+
* the event loop. On Unix, it resolves once the processes have exited, or have been killed with `SIGKILL`
|
|
125
|
+
* after {@link unixChildProcessTreesForceKillDelay}.
|
|
59
126
|
*
|
|
60
127
|
* @throws if the descendants of the process cannot be listed
|
|
61
128
|
*/
|
|
@@ -175,14 +242,34 @@ export class ProcessUtils {
|
|
|
175
242
|
return path.win32.join(systemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
|
|
176
243
|
}
|
|
177
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Sends `SIGTERM` to the given process, its descendants and, if it leads a process group, the members of
|
|
247
|
+
* its group. Those that are still running {@link unixForceKillDelay} later are then killed with `SIGKILL`.
|
|
248
|
+
*
|
|
249
|
+
* The tree of the current process is only terminated when it exits, so its own processes only get `SIGTERM`.
|
|
250
|
+
* The processes of earlier trees that are still waiting for their deadline are killed with `SIGKILL` right
|
|
251
|
+
* away instead, as they would outlive the current process otherwise.
|
|
252
|
+
*/
|
|
178
253
|
protected unixTerminateProcessTree(ppid: number): void {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (pid !== process.pid) {
|
|
182
|
-
this.unixKill(pid);
|
|
183
|
-
}
|
|
254
|
+
if (ppid === process.pid) {
|
|
255
|
+
this.unixForceKillPendingTargets();
|
|
184
256
|
}
|
|
185
|
-
|
|
257
|
+
const processes = ppid === process.pid ? undefined : this.unixTryListProcesses();
|
|
258
|
+
if (!processes) {
|
|
259
|
+
this.unixSendSigterm(ppid, this.unixGetChildrenRecursive(ppid), ppid === this.unixGetPGID(ppid));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const isGroupLeader = processes.get(ppid)?.pgid === ppid;
|
|
263
|
+
const descendants = this.unixGetDescendants(processes.values(), ppid);
|
|
264
|
+
this.unixSendSigterm(ppid, descendants, isGroupLeader);
|
|
265
|
+
const groupMembers = isGroupLeader ? this.unixGetGroupMembers(processes, ppid) : [];
|
|
266
|
+
this.unixAddForceKillTargets(processes, [ppid, ...descendants, ...groupMembers], performance.now() + this.unixForceKillDelay);
|
|
267
|
+
this.unixScheduleForceKill();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
protected unixSendSigterm(ppid: number, descendants: Iterable<number>, isGroupLeader: boolean): void {
|
|
271
|
+
this.unixSendSigtermToAll(descendants);
|
|
272
|
+
if (isGroupLeader) {
|
|
186
273
|
// When a process pgid === pid this means the the process is a group leader.
|
|
187
274
|
// We can then kill every process part of its group by doing `kill(-pgid)`.
|
|
188
275
|
// This can catch leaked processes under `init` that are still part of the group.
|
|
@@ -191,9 +278,39 @@ export class ProcessUtils {
|
|
|
191
278
|
this.unixKill(ppid);
|
|
192
279
|
}
|
|
193
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Sends `SIGTERM` to the descendants of the given process and, if it leads a process group, to the other
|
|
283
|
+
* members of its group, which may have been reparented. The ones that are still running
|
|
284
|
+
* {@link unixChildProcessTreesForceKillDelay} after the call started are killed with `SIGKILL`, together
|
|
285
|
+
* with the processes of earlier calls of {@link unixTerminateProcessTree} that are still running.
|
|
286
|
+
*/
|
|
194
287
|
protected async unixTerminateChildProcessTrees(ppid: number): Promise<void> {
|
|
195
|
-
const
|
|
196
|
-
|
|
288
|
+
const deadline = performance.now() + this.unixChildProcessTreesForceKillDelay;
|
|
289
|
+
let processes: Map<number, UnixProcessInfo>;
|
|
290
|
+
try {
|
|
291
|
+
processes = await this.unixListProcessesAsync();
|
|
292
|
+
} catch (error) {
|
|
293
|
+
this.logger.warn('Failed to list the start times of processes, child processes will only be sent SIGTERM', error);
|
|
294
|
+
const { stdout } = await this.execFile('ps', ['ax', '-o', 'ppid=,pid='], { timeout: UNIX_LIST_PROCESSES_TIMEOUT_MS });
|
|
295
|
+
this.unixSendSigtermToAll(this.unixParseChildrenRecursive(stdout, ppid));
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const pids = this.unixGetDescendants(processes.values(), ppid);
|
|
299
|
+
if (processes.get(ppid)?.pgid === ppid) {
|
|
300
|
+
this.unixGetGroupMembers(processes, ppid).forEach(pid => pids.add(pid));
|
|
301
|
+
pids.delete(ppid);
|
|
302
|
+
}
|
|
303
|
+
this.unixSendSigtermToAll(pids);
|
|
304
|
+
this.unixAddForceKillTargets(processes, pids, deadline);
|
|
305
|
+
for (const target of this.unixForceKillTargets.values()) {
|
|
306
|
+
target.deadline = Math.min(target.deadline, deadline);
|
|
307
|
+
}
|
|
308
|
+
await this.unixWaitForForceKillTargets(deadline);
|
|
309
|
+
await this.unixForceKillDueTargets();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
protected unixSendSigtermToAll(pids: Iterable<number>): void {
|
|
313
|
+
for (const pid of pids) {
|
|
197
314
|
// Prevent killing the current process:
|
|
198
315
|
if (pid !== process.pid) {
|
|
199
316
|
this.unixKill(pid);
|
|
@@ -201,9 +318,222 @@ export class ProcessUtils {
|
|
|
201
318
|
}
|
|
202
319
|
}
|
|
203
320
|
|
|
204
|
-
|
|
321
|
+
/**
|
|
322
|
+
* Records the given processes as {@link unixForceKillTargets}, except for the current process. A process
|
|
323
|
+
* that is already recorded keeps its earlier deadline.
|
|
324
|
+
*/
|
|
325
|
+
protected unixAddForceKillTargets(processes: Map<number, UnixProcessInfo>, pids: Iterable<number>, deadline: number): void {
|
|
326
|
+
for (const pid of pids) {
|
|
327
|
+
const info = processes.get(pid);
|
|
328
|
+
if (pid === process.pid || !info) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const existing = this.unixForceKillTargets.get(pid);
|
|
332
|
+
if (existing?.startTime === info.startTime && existing.deadline <= deadline) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
this.unixForceKillTargets.set(pid, { startTime: info.startTime, deadline });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Schedules {@link unixForceKillDueTargets} for the earliest deadline of the {@link unixForceKillTargets},
|
|
341
|
+
* and again after that for the remaining ones. The timer does not keep the current process alive.
|
|
342
|
+
*/
|
|
343
|
+
protected unixScheduleForceKill(): void {
|
|
344
|
+
clearTimeout(this.unixForceKillTimer);
|
|
345
|
+
this.unixForceKillTimer = undefined;
|
|
346
|
+
if (this.unixForceKillTargets.size === 0) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const deadline = Math.min(...Array.from(this.unixForceKillTargets.values(), target => target.deadline));
|
|
350
|
+
this.unixForceKillTimer = setTimeout(async () => {
|
|
351
|
+
this.unixForceKillTimer = undefined;
|
|
352
|
+
await this.unixForceKillDueTargets();
|
|
353
|
+
this.unixScheduleForceKill();
|
|
354
|
+
}, Math.max(0, deadline - performance.now())).unref();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Waits until all of the {@link unixForceKillTargets} have exited, or until the given deadline.
|
|
359
|
+
*/
|
|
360
|
+
protected async unixWaitForForceKillTargets(deadline: number): Promise<void> {
|
|
361
|
+
for (; ;) {
|
|
362
|
+
for (const pid of this.unixForceKillTargets.keys()) {
|
|
363
|
+
if (!this.unixIsAlive(pid)) {
|
|
364
|
+
this.unixForceKillTargets.delete(pid);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const remaining = deadline - performance.now();
|
|
368
|
+
if (this.unixForceKillTargets.size === 0 || remaining <= 0) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(remaining, UNIX_FORCE_KILL_POLL_INTERVAL_MS)));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Kills the {@link unixForceKillTargets} whose deadline has passed with `SIGKILL`, if they are still running
|
|
377
|
+
* and their PID has not been reused since. They stay in the map until the processes have been listed, so that
|
|
378
|
+
* a shutdown in the meantime still finds them. Never rejects.
|
|
379
|
+
*/
|
|
380
|
+
protected async unixForceKillDueTargets(): Promise<void> {
|
|
381
|
+
const now = performance.now();
|
|
382
|
+
const due = new Map<number, UnixForceKillTarget>();
|
|
383
|
+
for (const [pid, target] of this.unixForceKillTargets) {
|
|
384
|
+
if (target.deadline <= now) {
|
|
385
|
+
due.set(pid, target);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const survivors = this.unixGetSurvivors(due);
|
|
389
|
+
for (const pid of due.keys()) {
|
|
390
|
+
if (!survivors.has(pid)) {
|
|
391
|
+
this.unixForceKillTargets.delete(pid);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (survivors.size === 0) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
let processes: Map<number, UnixProcessInfo> | undefined;
|
|
205
398
|
try {
|
|
206
|
-
|
|
399
|
+
processes = await this.unixListProcessesAsync();
|
|
400
|
+
} catch (error) {
|
|
401
|
+
this.logger.error('Failed to kill the remaining processes of a process tree', error);
|
|
402
|
+
}
|
|
403
|
+
// Leave out the targets that were handled or replaced in the meantime, e.g. on exit.
|
|
404
|
+
const pending = new Map(Array.from(survivors).filter(([pid, target]) => this.unixForceKillTargets.get(pid) === target));
|
|
405
|
+
pending.forEach((_, pid) => this.unixForceKillTargets.delete(pid));
|
|
406
|
+
if (processes) {
|
|
407
|
+
this.unixForceKillSurvivors(pending, processes);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Kills all of the {@link unixForceKillTargets} with `SIGKILL` without waiting for their deadline, if they are
|
|
413
|
+
* still running and their PID has not been reused since. This blocks the event loop.
|
|
414
|
+
*/
|
|
415
|
+
protected unixForceKillPendingTargets(): void {
|
|
416
|
+
const targets = new Map(this.unixForceKillTargets);
|
|
417
|
+
this.unixForceKillTargets.clear();
|
|
418
|
+
clearTimeout(this.unixForceKillTimer);
|
|
419
|
+
this.unixForceKillTimer = undefined;
|
|
420
|
+
const survivors = this.unixGetSurvivors(targets);
|
|
421
|
+
if (survivors.size === 0) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
try {
|
|
425
|
+
this.unixForceKillSurvivors(survivors, this.unixListProcesses());
|
|
426
|
+
} catch (error) {
|
|
427
|
+
this.logger.error('Failed to kill the remaining processes of a process tree', error);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* @returns the given targets that are still running
|
|
433
|
+
*/
|
|
434
|
+
protected unixGetSurvivors(targets: Map<number, UnixForceKillTarget>): Map<number, UnixForceKillTarget> {
|
|
435
|
+
return new Map(Array.from(targets).filter(([pid]) => this.unixIsAlive(pid)));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Kills the given targets with `SIGKILL` if they are listed in `processes` with the same start time.
|
|
440
|
+
*/
|
|
441
|
+
protected unixForceKillSurvivors(targets: Map<number, UnixForceKillTarget>, processes: Map<number, UnixProcessInfo>): void {
|
|
442
|
+
for (const [pid, target] of targets) {
|
|
443
|
+
if (processes.get(pid)?.startTime === target.startTime) {
|
|
444
|
+
this.logger.warn(`[${pid}] still running after SIGTERM, killing it with SIGKILL`);
|
|
445
|
+
this.unixKill(pid, 'SIGKILL');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
protected unixIsAlive(pid: number): boolean {
|
|
451
|
+
try {
|
|
452
|
+
process.kill(pid, 0);
|
|
453
|
+
return true;
|
|
454
|
+
} catch (error) {
|
|
455
|
+
return (error as NodeJS.ErrnoException | undefined)?.code !== 'ESRCH';
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @returns the PIDs of all of the descendants of the given process
|
|
461
|
+
*/
|
|
462
|
+
protected unixGetDescendants(processes: Iterable<{ pid: number, ppid: number }>, ppid: number): Set<number> {
|
|
463
|
+
const list = Array.from(processes);
|
|
464
|
+
const pids = new Set<number>([ppid]);
|
|
465
|
+
// Keep looking for parent/child relationships while we keep finding new parents:
|
|
466
|
+
let size; do {
|
|
467
|
+
size = pids.size;
|
|
468
|
+
for (const child of list) {
|
|
469
|
+
if (pids.has(child.ppid)) {
|
|
470
|
+
pids.add(child.pid);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
} while (size !== pids.size);
|
|
474
|
+
// Exclude the requested parent id:
|
|
475
|
+
pids.delete(ppid);
|
|
476
|
+
return pids;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* @returns the PIDs of the members of the given process group, including those that were reparented
|
|
481
|
+
*/
|
|
482
|
+
protected unixGetGroupMembers(processes: Map<number, UnixProcessInfo>, pgid: number): number[] {
|
|
483
|
+
return Array.from(processes.values()).filter(info => info.pgid === pgid).map(info => info.pid);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* @returns all processes, see {@link unixListProcesses}, or `undefined` if they cannot be listed
|
|
488
|
+
*/
|
|
489
|
+
protected unixTryListProcesses(): Map<number, UnixProcessInfo> | undefined {
|
|
490
|
+
try {
|
|
491
|
+
return this.unixListProcesses();
|
|
492
|
+
} catch (error) {
|
|
493
|
+
this.logger.warn('Failed to list the start times of processes, a process tree will only be sent SIGTERM', error);
|
|
494
|
+
return undefined;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @returns all processes by PID, from a single run of `ps`
|
|
500
|
+
*/
|
|
501
|
+
protected unixListProcesses(): Map<number, UnixProcessInfo> {
|
|
502
|
+
const { stdout } = this.spawnSync('ps', UNIX_LIST_PROCESSES_ARGS, this.unixListProcessesOptions());
|
|
503
|
+
return this.unixParseProcesses(stdout);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* @returns all processes by PID, from a single run of `ps`, without blocking the event loop
|
|
508
|
+
*/
|
|
509
|
+
protected async unixListProcessesAsync(): Promise<Map<number, UnixProcessInfo>> {
|
|
510
|
+
const { stdout } = await this.execFile('ps', UNIX_LIST_PROCESSES_ARGS, this.unixListProcessesOptions());
|
|
511
|
+
return this.unixParseProcesses(stdout);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
protected unixListProcessesOptions(): cp.SpawnSyncOptions & cp.ExecFileOptions {
|
|
515
|
+
// A fixed locale and time zone keep the output of `lstart` comparable across runs.
|
|
516
|
+
return { env: { ...process.env, LC_ALL: 'C', TZ: 'UTC' }, timeout: UNIX_LIST_PROCESSES_TIMEOUT_MS };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* @param psOutput the output of `ps` with {@link UNIX_LIST_PROCESSES_ARGS}
|
|
521
|
+
*/
|
|
522
|
+
protected unixParseProcesses(psOutput: string): Map<number, UnixProcessInfo> {
|
|
523
|
+
const processes = new Map<number, UnixProcessInfo>();
|
|
524
|
+
for (const line of psOutput.split('\n')) {
|
|
525
|
+
const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\d+)\s+(.+)$/);
|
|
526
|
+
if (match) {
|
|
527
|
+
const pid = Number.parseInt(match[1], 10);
|
|
528
|
+
processes.set(pid, { pid, ppid: Number.parseInt(match[2], 10), pgid: Number.parseInt(match[3], 10), startTime: match[4] });
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return processes;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
protected unixKill(pid: number, signal?: NodeJS.Signals): void {
|
|
535
|
+
try {
|
|
536
|
+
process.kill(pid, signal);
|
|
207
537
|
} catch (error) {
|
|
208
538
|
// ESRCH means the process is already gone, which is the goal here. Log
|
|
209
539
|
// anything else but keep going so the rest of the tree is still killed.
|
|
@@ -228,7 +558,6 @@ export class ProcessUtils {
|
|
|
228
558
|
* @returns the PIDs of all of the descendants of the given process
|
|
229
559
|
*/
|
|
230
560
|
protected unixParseChildrenRecursive(psOutput: string, ppid: number): Set<number> {
|
|
231
|
-
const pids = new Set<number>([ppid]);
|
|
232
561
|
const matcher = /(\d+)\s+(\d+)/;
|
|
233
562
|
const psList = psOutput
|
|
234
563
|
.trim()
|
|
@@ -240,18 +569,7 @@ export class ProcessUtils {
|
|
|
240
569
|
pid: Number.parseInt(match[2], 10),
|
|
241
570
|
};
|
|
242
571
|
});
|
|
243
|
-
|
|
244
|
-
let size; do {
|
|
245
|
-
size = pids.size;
|
|
246
|
-
for (const child of psList) {
|
|
247
|
-
if (pids.has(child.ppid)) {
|
|
248
|
-
pids.add(child.pid);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
} while (size !== pids.size);
|
|
252
|
-
// Exclude the requested parent id:
|
|
253
|
-
pids.delete(ppid);
|
|
254
|
-
return pids;
|
|
572
|
+
return this.unixGetDescendants(psList, ppid);
|
|
255
573
|
}
|
|
256
574
|
|
|
257
575
|
/**
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
* The `exit` handler of the `BackendApplication` then terminates the process tree.
|
|
21
21
|
* Set {@link EXITING_BACKEND_NO_POWERSHELL} to make the listing of the children of the process fail.
|
|
22
22
|
* Set {@link EXITING_BACKEND_GRACEFUL} to shut down as on `SIGTERM` instead, which exits with code 1.
|
|
23
|
+
* Set {@link EXITING_BACKEND_CHILD_IGNORES_SIGTERM} to make the child ignore `SIGTERM`.
|
|
23
24
|
*/
|
|
24
25
|
|
|
25
26
|
import 'reflect-metadata';
|
|
@@ -46,6 +47,11 @@ export const EXITING_BACKEND_NO_POWERSHELL = 'THEIA_TEST_EXITING_BACKEND_NO_POWE
|
|
|
46
47
|
*/
|
|
47
48
|
export const EXITING_BACKEND_GRACEFUL = 'THEIA_TEST_EXITING_BACKEND_GRACEFUL';
|
|
48
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Environment variable that makes the child of the script ignore `SIGTERM`.
|
|
52
|
+
*/
|
|
53
|
+
export const EXITING_BACKEND_CHILD_IGNORES_SIGTERM = 'THEIA_TEST_EXITING_BACKEND_CHILD_IGNORES_SIGTERM';
|
|
54
|
+
|
|
49
55
|
class NoPowerShellProcessUtils extends ProcessUtils {
|
|
50
56
|
protected override winGetPowerShellPath(): string {
|
|
51
57
|
return 'C:\\does-not-exist\\powershell.exe';
|
|
@@ -65,13 +71,21 @@ if (process.argv[1] && path.resolve(process.argv[1]) === import.meta.filename) {
|
|
|
65
71
|
container.bind(BackendApplication).toSelf().inSingletonScope();
|
|
66
72
|
container.get(BackendApplication);
|
|
67
73
|
|
|
68
|
-
const
|
|
69
|
-
child.on('spawn', () => process.stdout.write(`${child.pid}\n`, () => {
|
|
74
|
+
const exit = (): void => {
|
|
70
75
|
if (process.env[EXITING_BACKEND_GRACEFUL]) {
|
|
71
76
|
// Signals cannot be sent to a process on Windows, so invoke the handler of the `BackendApplication` directly.
|
|
72
77
|
process.emit('SIGTERM', 'SIGTERM');
|
|
73
78
|
} else {
|
|
74
79
|
process.exit(EXITING_BACKEND_EXIT_CODE);
|
|
75
80
|
}
|
|
76
|
-
}
|
|
81
|
+
};
|
|
82
|
+
if (process.env[EXITING_BACKEND_CHILD_IGNORES_SIGTERM]) {
|
|
83
|
+
// The child reports when it ignores `SIGTERM`.
|
|
84
|
+
const child = cp.spawn(process.execPath, ['-e', 'process.on("SIGTERM", () => {}); console.log(); setInterval(() => {}, 1000)'],
|
|
85
|
+
{ stdio: ['ignore', 'pipe', 'ignore'] });
|
|
86
|
+
child.stdout.once('data', () => process.stdout.write(`${child.pid}\n`, exit));
|
|
87
|
+
} else {
|
|
88
|
+
const child = cp.spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' });
|
|
89
|
+
child.on('spawn', () => process.stdout.write(`${child.pid}\n`, exit));
|
|
90
|
+
}
|
|
77
91
|
}
|