-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,6 @@ words: | |
- turborepo | ||
- turbowatch | ||
- vitest | ||
- wholename | ||
- wholename | ||
- pidtree | ||
- pids |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* eslint-disable no-console */ | ||
|
||
const { spawn } = require('node:child_process'); | ||
const { resolve } = require('node:path'); | ||
|
||
spawn('node', [resolve(__dirname, 'b.js')], { | ||
stdio: 'inherit', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable no-console */ | ||
|
||
setInterval(() => { | ||
console.log('b'); | ||
}, 1_000); | ||
|
||
process.on('SIGTERM', () => { | ||
console.log('b: SIGTERM'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable no-console */ | ||
|
||
const { spawn } = require('node:child_process'); | ||
const { resolve } = require('node:path'); | ||
|
||
const b = spawn('node', [resolve(__dirname, 'b.js')]); | ||
|
||
b.stdout.on('data', (data) => { | ||
console.log(data.toString()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable no-console */ | ||
|
||
setInterval(() => { | ||
console.log('b'); | ||
}, 1_000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { killPsTree } from './killPsTree'; | ||
import { exec } from 'node:child_process'; | ||
import { join } from 'node:path'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
import { test } from 'vitest'; | ||
|
||
test('kills a good process tree', async () => { | ||
const childProcess = exec( | ||
`node ${join(__dirname, '__fixtures__/killPsTree/goodTree/a.js')}`, | ||
); | ||
|
||
if (!childProcess.pid) { | ||
throw new Error('Expected child process to have a pid'); | ||
} | ||
|
||
await setTimeout(500); | ||
|
||
await killPsTree(childProcess.pid); | ||
}); | ||
|
||
test('kills a bad process tree', async () => { | ||
const childProcess = exec( | ||
`node ${join(__dirname, '__fixtures__/killPsTree/badTree/a.js')}`, | ||
); | ||
|
||
if (!childProcess.pid) { | ||
throw new Error('Expected child process to have a pid'); | ||
} | ||
|
||
await setTimeout(500); | ||
|
||
await killPsTree(childProcess.pid, 1_000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Logger } from './Logger'; | ||
import findProcess from 'find-process'; | ||
import pidTree from 'pidtree'; | ||
|
||
const log = Logger.child({ | ||
namespace: 'killPsTree', | ||
}); | ||
|
||
export const killPsTree = async ( | ||
rootPid: number, | ||
gracefulTimeout: number = 30_000, | ||
) => { | ||
const childPids = await pidTree(rootPid); | ||
|
||
const pids = [rootPid, ...childPids]; | ||
|
||
for (const pid of pids) { | ||
process.kill(pid, 'SIGTERM'); | ||
} | ||
|
||
let hangingPids = [...pids]; | ||
|
||
let hitTimeout = false; | ||
|
||
const timeoutId = setTimeout(() => { | ||
hitTimeout = true; | ||
|
||
log.debug({ hangingPids }, 'sending SIGKILL to processes...'); | ||
|
||
for (const pid of hangingPids) { | ||
process.kill(pid, 'SIGKILL'); | ||
} | ||
}, gracefulTimeout); | ||
|
||
await Promise.all( | ||
hangingPids.map((pid) => { | ||
return new Promise((resolve) => { | ||
const interval = setInterval(async () => { | ||
if (hitTimeout) { | ||
clearInterval(interval); | ||
|
||
resolve(false); | ||
|
||
return; | ||
} | ||
|
||
const processes = await findProcess('pid', pid); | ||
|
||
if (processes.length === 0) { | ||
hangingPids = hangingPids.filter( | ||
(hangingPid) => hangingPid !== pid, | ||
); | ||
|
||
clearInterval(interval); | ||
|
||
resolve(true); | ||
} | ||
}, 100); | ||
}); | ||
}), | ||
); | ||
|
||
clearTimeout(timeoutId); | ||
|
||
log.debug('all processes terminated'); | ||
}; |