Skip to content

Commit f2c6bdd

Browse files
committedJun 8, 2022
fix: suppress error from process.kill when killing tasks on failure
1 parent c5cec0a commit f2c6bdd

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed
 

‎lib/resolveTaskFn.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ const killExecaProcess = async (execaProcess) => {
5454
try {
5555
const childPids = await pidTree(execaProcess.pid)
5656
for (const childPid of childPids) {
57-
process.kill(childPid)
57+
try {
58+
process.kill(childPid)
59+
} catch (error) {
60+
debugLog(`Failed to kill process with pid "%d": %o`, childPid, error)
61+
}
5862
}
5963
} catch (error) {
6064
// Suppress "No matching pid found" error. This probably means
6165
// the process already died before executing.
62-
debugLog(`Failed to find process for pid %d`, execaProcess.pid)
66+
debugLog(`Failed to kill process with pid "%d": %o`, execaProcess.pid, error)
6367
}
6468

6569
// The execa process is killed separately in order to get the `KILLED` status.

‎test/resolveTaskFn.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,51 @@ describe('resolveTaskFn', () => {
484484
value: realKill,
485485
})
486486
})
487+
488+
it('should ignore error when trying to kill child processes', async () => {
489+
expect.assertions(3)
490+
491+
execa.mockImplementationOnce(() =>
492+
createExecaReturnValue(
493+
{
494+
stdout: 'a-ok',
495+
stderr: '',
496+
code: 0,
497+
cmd: 'mock cmd',
498+
failed: false,
499+
killed: false,
500+
signal: null,
501+
},
502+
1000
503+
)
504+
)
505+
506+
const realKill = process.kill
507+
const mockKill = jest.fn(() => {
508+
throw new Error('kill ESRCH')
509+
})
510+
Object.defineProperty(process, 'kill', {
511+
value: mockKill,
512+
})
513+
514+
pidTree.mockImplementationOnce(() => ['1234'])
515+
516+
const taskFn = resolveTaskFn({ command: 'node' })
517+
518+
const context = getInitialState()
519+
const taskPromise = taskFn(context)
520+
521+
context.events.emit('lint-staged:taskError')
522+
523+
jest.runAllTimers()
524+
525+
await expect(taskPromise).rejects.toThrowErrorMatchingInlineSnapshot(`"node [KILLED]"`)
526+
527+
expect(mockKill).toHaveBeenCalledTimes(1)
528+
expect(mockKill).toHaveBeenCalledWith('1234')
529+
530+
Object.defineProperty(process, 'kill', {
531+
value: realKill,
532+
})
533+
})
487534
})

0 commit comments

Comments
 (0)
Please sign in to comment.