Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 26 additions & 40 deletions packages/cli-kit/src/public/node/tree-kill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable jsdoc/require-throws */
/* eslint-disable no-restricted-imports */

Expand Down Expand Up @@ -85,25 +84,17 @@ function adaptedTreeKill(
rootPid,
tree,
pidsToProcess,
function (parentPid: string) {
return spawn('pgrep', ['-lfP', parentPid])
},
function () {
killAll(tree, killSignal, rootPid, killRoot, callback)
},
(parentPid: string) => spawn('pgrep', ['-lfP', parentPid]),
() => killAll(tree, killSignal, rootPid, killRoot, callback),
)
break
default:
buildProcessTree(
rootPid,
tree,
pidsToProcess,
function (parentPid: string) {
return spawn('ps', ['-o', 'pid command', '--no-headers', '--ppid', parentPid])
},
function () {
killAll(tree, killSignal, rootPid, killRoot, callback)
},
(parentPid: string) => spawn('ps', ['-o', 'pid command', '--no-headers', '--ppid', parentPid]),
() => killAll(tree, killSignal, rootPid, killRoot, callback),
)
break
}
Expand All @@ -127,22 +118,21 @@ function killAll(
): void {
const killed = new Set<string>()
try {
Object.keys(tree).forEach(function (pid) {
tree[pid]!.forEach(function (pidpid) {
if (!killed.has(pidpid)) {
killPid(pidpid, killSignal)
killed.add(pidpid)
for (const [pid, children] of Object.entries(tree)) {
for (const childPid of children) {
if (!killed.has(childPid)) {
killPid(childPid, killSignal)
killed.add(childPid)
}
})
}
if (pid === rootPid && killRoot && !killed.has(pid)) {
killPid(pid, killSignal)
killed.add(pid)
}
})
}
// eslint-disable-next-line no-catch-all/no-catch-all
} catch (err: unknown) {
// @ts-ignore
callback(err)
callback(err instanceof Error ? err : new Error(String(err)))
return
}
callback()
Expand All @@ -158,8 +148,7 @@ function killPid(pid: string, killSignal: string) {
try {
process.kill(parseInt(pid, 10), killSignal)
} catch (err) {
// @ts-ignore
if (err.code !== 'ESRCH') throw err
if ((err as {code?: string}).code !== 'ESRCH') throw err
}
}

Expand All @@ -181,7 +170,7 @@ function buildProcessTree(
) {
const ps = spawnChildProcessesList(parentPid)
let allData = ''
ps.stdout?.on('data', function (data: Buffer) {
ps.stdout?.on('data', (data: Buffer) => {
const dataStr = data.toString('ascii')
allData += dataStr
})
Expand All @@ -198,21 +187,18 @@ function buildProcessTree(
return
}

allData
.trim()
.split('\n')
.forEach(function (line) {
const match = line.match(/^(\d+)\s(.*)$/)
if (match) {
const pid = match[1]!
const cmd = match[2]!
tree[parentPid]!.push(pid)
tree[pid] = []
outputDebug(`Killing process ${pid}: ${cmd}`)
pidsToProcess.add(pid)
buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb)
}
})
for (const line of allData.trim().split('\n')) {
const match = line.match(/^(\d+)\s(.*)$/)
if (match) {
const pid = match[1]!
const cmd = match[2]!
tree[parentPid]!.push(pid)
tree[pid] = []
outputDebug(`Killing process ${pid}: ${cmd}`)
pidsToProcess.add(pid)
buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb)
}
}
}

ps.on('close', onClose)
Expand Down
Loading