forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
child_process: add timeout and killSignal to spawn and fork
Add support for timeout and killSignal to spawn and fork. Fixes: nodejs#27639
- Loading branch information
Nitzan Uziely
committed
Feb 6, 2021
1 parent
a6de951
commit bad5187
Showing
6 changed files
with
256 additions
and
2 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
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
69 changes: 69 additions & 0 deletions
69
test/parallel/test-child-process-fork-timeout-kill-signal.js
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,69 @@ | ||
'use strict'; | ||
|
||
const { mustCall } = require('../common'); | ||
const { strictEqual, ok, throws } = require('assert'); | ||
const fixtures = require('../common/fixtures'); | ||
const { fork } = require('child_process'); | ||
const { getEventListeners } = require('events'); | ||
|
||
{ | ||
// Verify default signal + closes after at least 4 ms. | ||
let closed = false; | ||
setTimeout(() => ok(!closed), 4); | ||
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: 5, | ||
}); | ||
cp.on('close', mustCall(() => closed = true)); | ||
cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGTERM'))); | ||
} | ||
|
||
{ | ||
// Verify correct signal + closes after at least 4 ms. | ||
let closed = false; | ||
setTimeout(() => ok(!closed), 4); | ||
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: 5, | ||
killSignal: 'SIGKILL', | ||
}); | ||
cp.on('close', mustCall(() => closed = true)); | ||
cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGKILL'))); | ||
} | ||
|
||
{ | ||
// Verify timeout verification | ||
throws(() => fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: 'badValue', | ||
}), /ERR_OUT_OF_RANGE/); | ||
|
||
throws(() => fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: {}, | ||
}), /ERR_OUT_OF_RANGE/); | ||
} | ||
|
||
{ | ||
// Verify abort signal gets unregistered | ||
const signal = new EventTarget(); | ||
signal.aborted = false; | ||
|
||
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: 6, | ||
signal, | ||
}); | ||
strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
cp.on('close', mustCall(() => { | ||
strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
})); | ||
} | ||
|
||
{ | ||
// Verify no error happens when abort is called timeout | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
timeout: 100, | ||
signal, | ||
}); | ||
cp.on('error', mustCall()); | ||
setTimeout(() => controller.abort(), 1); | ||
} |
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
70 changes: 70 additions & 0 deletions
70
test/parallel/test-child-process-spawn-timeout-kill-signal.js
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,70 @@ | ||
'use strict'; | ||
|
||
const { mustCall } = require('../common'); | ||
const { strictEqual, ok, throws } = require('assert'); | ||
const fixtures = require('../common/fixtures'); | ||
const { spawn } = require('child_process'); | ||
const { getEventListeners } = require('events'); | ||
|
||
const aliveForeverFile = 'child-process-stay-alive-forever.js'; | ||
{ | ||
// Verify default signal + closes after at least 4 ms. | ||
let closed = false; | ||
setTimeout(() => ok(!closed), 4); | ||
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: 5, | ||
}); | ||
cp.on('close', mustCall(() => closed = true)); | ||
cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGTERM'))); | ||
} | ||
|
||
{ | ||
// Verify correct signal + closes after at least 4 ms. | ||
let closed = false; | ||
setTimeout(() => ok(!closed), 4); | ||
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: 5, | ||
killSignal: 'SIGKILL', | ||
}); | ||
cp.on('close', mustCall(() => closed = true)); | ||
cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGKILL'))); | ||
} | ||
|
||
{ | ||
// Verify timeout verification | ||
throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: 'badValue', | ||
}), /ERR_OUT_OF_RANGE/); | ||
|
||
throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: {}, | ||
}), /ERR_OUT_OF_RANGE/); | ||
} | ||
|
||
{ | ||
// Verify abort signal gets unregistered | ||
const signal = new EventTarget(); | ||
signal.aborted = false; | ||
|
||
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: 6, | ||
signal, | ||
}); | ||
strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
cp.on('close', mustCall(() => { | ||
strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
})); | ||
} | ||
|
||
{ | ||
// Verify no error happens when abort is called timeout | ||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { | ||
timeout: 100, | ||
signal, | ||
}); | ||
cp.on('error', mustCall()); | ||
setTimeout(() => controller.abort(), 1); | ||
} |