Skip to content

Commit

Permalink
fix: suppress error from process.kill when killing tasks on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Jun 8, 2022
1 parent c5cec0a commit f2c6bdd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/resolveTaskFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 47 additions & 0 deletions test/resolveTaskFn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.