Skip to content
Permalink
Browse files
fix: suppress error from process.kill when killing tasks on failure
  • Loading branch information
iiroj committed Jun 8, 2022
1 parent c5cec0a commit f2c6bdd9114a8d5ba8473cc647ef55a6ee5664e1
Showing 2 changed files with 53 additions and 2 deletions.
@@ -54,12 +54,16 @@ const killExecaProcess = async (execaProcess) => {
try {
const childPids = await pidTree(execaProcess.pid)
for (const childPid of childPids) {
process.kill(childPid)
try {
process.kill(childPid)
} catch (error) {
debugLog(`Failed to kill process with pid "%d": %o`, childPid, error)
}
}
} catch (error) {
// Suppress "No matching pid found" error. This probably means
// the process already died before executing.
debugLog(`Failed to find process for pid %d`, execaProcess.pid)
debugLog(`Failed to kill process with pid "%d": %o`, execaProcess.pid, error)
}

// The execa process is killed separately in order to get the `KILLED` status.
@@ -484,4 +484,51 @@ describe('resolveTaskFn', () => {
value: realKill,
})
})

it('should ignore error when trying to kill child processes', async () => {
expect.assertions(3)

execa.mockImplementationOnce(() =>
createExecaReturnValue(
{
stdout: 'a-ok',
stderr: '',
code: 0,
cmd: 'mock cmd',
failed: false,
killed: false,
signal: null,
},
1000
)
)

const realKill = process.kill
const mockKill = jest.fn(() => {
throw new Error('kill ESRCH')
})
Object.defineProperty(process, 'kill', {
value: mockKill,
})

pidTree.mockImplementationOnce(() => ['1234'])

const taskFn = resolveTaskFn({ command: 'node' })

const context = getInitialState()
const taskPromise = taskFn(context)

context.events.emit('lint-staged:taskError')

jest.runAllTimers()

await expect(taskPromise).rejects.toThrowErrorMatchingInlineSnapshot(`"node [KILLED]"`)

expect(mockKill).toHaveBeenCalledTimes(1)
expect(mockKill).toHaveBeenCalledWith('1234')

Object.defineProperty(process, 'kill', {
value: realKill,
})
})
})

0 comments on commit f2c6bdd

Please sign in to comment.