| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8010c83 commit 0bcaf9c
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -351,6 +351,9 @@ controller.abort(); | |||
| 351 | 351 | <!-- YAML | |
| 352 | 352 | added: v0.5.0 | |
| 353 | 353 | changes: | |
| 354 | + - version: REPLACEME | ||
| 355 | + pr-url: https://github.com/nodejs/node/pull/37325 | ||
| 356 | + description: killSignal for AbortSignal was added. | ||
| 354 | 357 | - version: v14.17.0 | |
| 355 | 358 | pr-url: https://github.com/nodejs/node/pull/36603 | |
| 356 | 359 | description: AbortSignal support was added. | |
@@ -383,6 +386,8 @@ changes: | |||
| 383 | 386 | messages between processes. Possible values are `'json'` and `'advanced'`. | |
| 384 | 387 | See [Advanced serialization][] for more details. **Default:** `'json'`. | |
| 385 | 388 | * `signal` {AbortSignal} Allows closing the subprocess using an AbortSignal. | |
| 389 | + * `killSignal` {string} The signal value to be used when the spawned | ||
| 390 | + process will be killed by the abort signal. **Default:** `'SIGTERM'`. | ||
| 386 | 391 | * `silent` {boolean} If `true`, stdin, stdout, and stderr of the child will be | |
| 387 | 392 | piped to the parent, otherwise they will be inherited from the parent, see | |
| 388 | 393 | the `'pipe'` and `'inherit'` options for [`child_process.spawn()`][]'s | |
@@ -431,6 +436,9 @@ The `signal` option works exactly the same way it does in | |||
| 431 | 436 | <!-- YAML | |
| 432 | 437 | added: v0.1.90 | |
| 433 | 438 | changes: | |
| 439 | + - version: REPLACEME | ||
| 440 | + pr-url: https://github.com/nodejs/node/pull/37325 | ||
| 441 | + description: killSignal for AbortSignal was added. | ||
| 434 | 442 | - version: v14.17.0 | |
| 435 | 443 | pr-url: https://github.com/nodejs/node/pull/36432 | |
| 436 | 444 | description: AbortSignal support was added. | |
@@ -477,6 +485,8 @@ changes: | |||
| 477 | 485 | * `windowsHide` {boolean} Hide the subprocess console window that would | |
| 478 | 486 | normally be created on Windows systems. **Default:** `false`. | |
| 479 | 487 | * `signal` {AbortSignal} allows aborting the execFile using an AbortSignal. | |
| 488 | + * `killSignal` {string} The signal value to be used when the spawned | ||
| 489 | + process will be killed by the abort signal. **Default:** `'SIGTERM'`. | ||
| 480 | 490 | ||
| 481 | 491 | * Returns: {ChildProcess} | |
| 482 | 492 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -610,6 +610,18 @@ function normalizeSpawnArguments(file, args, options) { | |||
| 610 | 610 | }; | |
| 611 | 611 | } | |
| 612 | 612 | ||
| 613 | + function abortChildProcess(child, killSignal) { | ||
| 614 | + if (!child) | ||
| 615 | + return; | ||
| 616 | + try { | ||
| 617 | + if (child.kill(killSignal)) { | ||
| 618 | + child.emit('error', new AbortError()); | ||
| 619 | + } | ||
| 620 | + } catch (err) { | ||
| 621 | + child.emit('error', err); | ||
| 622 | + } | ||
| 623 | + } | ||
| 624 | + | ||
| 613 | 625 | ||
| 614 | 626 | /** | |
| 615 | 627 | * Spawns a new process using the given `file`. | |
@@ -641,21 +653,19 @@ function spawn(file, args, options) { | |||
| 641 | 653 | const signal = options.signal; | |
| 642 | 654 | // Validate signal, if present | |
| 643 | 655 | validateAbortSignal(signal, 'options.signal'); | |
| 644 | - | ||
| 656 | + const killSignal = sanitizeKillSignal(options.killSignal); | ||
| 645 | 657 | // Do nothing and throw if already aborted | |
| 646 | 658 | if (signal.aborted) { | |
| 647 | 659 | onAbortListener(); | |
| 648 | 660 | } else { | |
| 649 | 661 | signal.addEventListener('abort', onAbortListener, { once: true }); | |
| 650 | - child.once('close', | ||
| 662 | + child.once('exit', | ||
| 651 | 663 | () => signal.removeEventListener('abort', onAbortListener)); | |
| 652 | 664 | } | |
| 653 | 665 | ||
| 654 | 666 | function onAbortListener() { | |
| 655 | 667 | process.nextTick(() => { | |
| 656 | - child?.kill?.(options.killSignal); | ||
| 657 | - | ||
| 658 | - child.emit('error', new AbortError()); | ||
| 668 | + abortChildProcess(child, killSignal); | ||
| 659 | 669 | }); | |
| 660 | 670 | } | |
| 661 | 671 | } | |
@@ -877,19 +887,18 @@ function spawnWithSignal(file, args, options) { | |||
| 877 | 887 | } | |
| 878 | 888 | const child = spawn(file, args, opts); | |
| 879 | 889 | ||
| 880 | - if (options && options.signal) { | ||
| 890 | + if (options?.signal) { | ||
| 891 | + const killSignal = sanitizeKillSignal(options.killSignal); | ||
| 892 | + | ||
| 881 | 893 | function kill() { | |
| 882 | - if (child._handle) { | ||
| 883 | - child._handle.kill(options.killSignal || 'SIGTERM'); | ||
| 884 | - child.emit('error', new AbortError()); | ||
| 885 | - } | ||
| 894 | + abortChildProcess(child, killSignal); | ||
| 886 | 895 | } | |
| 887 | 896 | if (options.signal.aborted) { | |
| 888 | 897 | process.nextTick(kill); | |
| 889 | 898 | } else { | |
| 890 | - options.signal.addEventListener('abort', kill); | ||
| 899 | + options.signal.addEventListener('abort', kill, { once: true }); | ||
| 891 | 900 | const remove = () => options.signal.removeEventListener('abort', kill); | |
| 892 | - child.once('close', remove); | ||
| 901 | + child.once('exit', remove); | ||
| 893 | 902 | } | |
| 894 | 903 | } | |
| 895 | 904 | return child; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,48 +5,47 @@ const assert = require('assert'); | |||
| 5 | 5 | const exec = require('child_process').exec; | |
| 6 | 6 | const { promisify } = require('util'); | |
| 7 | 7 | ||
| 8 | - let pwdcommand, dir; | ||
| 9 | 8 | const execPromisifed = promisify(exec); | |
| 10 | 9 | const invalidArgTypeError = { | |
| 11 | 10 | code: 'ERR_INVALID_ARG_TYPE', | |
| 12 | 11 | name: 'TypeError' | |
| 13 | 12 | }; | |
| 14 | 13 | ||
| 15 | - | ||
| 14 | + let waitCommand = ''; | ||
| 16 | 15 | if (common.isWindows) { | |
| 17 | - pwdcommand = 'echo %cd%'; | ||
| 18 | - dir = 'c:\\windows'; | ||
| 16 | + waitCommand = 'TIMEOUT 120'; | ||
| 19 | 17 | } else { | |
| 20 | - pwdcommand = 'pwd'; | ||
| 21 | - dir = '/dev'; | ||
| 18 | + waitCommand = 'sleep 2m'; | ||
| 22 | 19 | } | |
| 23 | 20 | ||
| 24 | - | ||
| 25 | 21 | { | |
| 26 | 22 | const ac = new AbortController(); | |
| 27 | 23 | const signal = ac.signal; | |
| 28 | - const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 29 | - assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
| 24 | + const promise = execPromisifed(waitCommand, { signal }); | ||
| 25 | + assert.rejects(promise, /AbortError/, 'post aborted sync signal failed') | ||
| 26 | + .then(common.mustCall()); | ||
| 30 | 27 | ac.abort(); | |
| 31 | 28 | } | |
| 32 | 29 | ||
| 33 | 30 | { | |
| 34 | 31 | assert.throws(() => { | |
| 35 | - execPromisifed(pwdcommand, { cwd: dir, signal: {} }); | ||
| 32 | + execPromisifed(waitCommand, { signal: {} }); | ||
| 36 | 33 | }, invalidArgTypeError); | |
| 37 | 34 | } | |
| 38 | 35 | ||
| 39 | 36 | { | |
| 40 | 37 | function signal() {} | |
| 41 | 38 | assert.throws(() => { | |
| 42 | - execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 39 | + execPromisifed(waitCommand, { signal }); | ||
| 43 | 40 | }, invalidArgTypeError); | |
| 44 | 41 | } | |
| 45 | 42 | ||
| 46 | 43 | { | |
| 47 | 44 | const ac = new AbortController(); | |
| 48 | - const signal = (ac.abort(), ac.signal); | ||
| 49 | - const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 45 | + const { signal } = ac; | ||
| 46 | + ac.abort(); | ||
| 47 | + const promise = execPromisifed(waitCommand, { signal }); | ||
| 50 | 48 | ||
| 51 | - assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
| 49 | + assert.rejects(promise, /AbortError/, 'pre aborted signal failed') | ||
| 50 | + .then(common.mustCall()); | ||
| 52 | 51 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | // Flags: --experimental-abortcontroller | |
| 2 | 2 | 'use strict'; | |
| 3 | 3 | ||
| 4 | - const { mustCall } = require('../common'); | ||
| 4 | + const { mustCall, mustNotCall } = require('../common'); | ||
| 5 | 5 | const { strictEqual } = require('assert'); | |
| 6 | 6 | const fixtures = require('../common/fixtures'); | |
| 7 | 7 | const { fork } = require('child_process'); | |
@@ -13,7 +13,10 @@ const { fork } = require('child_process'); | |||
| 13 | 13 | const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | |
| 14 | 14 | signal | |
| 15 | 15 | }); | |
| 16 | - cp.on('exit', mustCall()); | ||
| 16 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 17 | + strictEqual(code, null); | ||
| 18 | + strictEqual(killSignal, 'SIGTERM'); | ||
| 19 | + })); | ||
| 17 | 20 | cp.on('error', mustCall((err) => { | |
| 18 | 21 | strictEqual(err.name, 'AbortError'); | |
| 19 | 22 | })); | |
@@ -27,8 +30,44 @@ const { fork } = require('child_process'); | |||
| 27 | 30 | const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | |
| 28 | 31 | signal | |
| 29 | 32 | }); | |
| 30 | - cp.on('exit', mustCall()); | ||
| 33 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 34 | + strictEqual(code, null); | ||
| 35 | + strictEqual(killSignal, 'SIGTERM'); | ||
| 36 | + })); | ||
| 37 | + cp.on('error', mustCall((err) => { | ||
| 38 | + strictEqual(err.name, 'AbortError'); | ||
| 39 | + })); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + { | ||
| 43 | + // Test passing a different kill signal | ||
| 44 | + const ac = new AbortController(); | ||
| 45 | + const { signal } = ac; | ||
| 46 | + ac.abort(); | ||
| 47 | + const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
| 48 | + signal, | ||
| 49 | + killSignal: 'SIGKILL', | ||
| 50 | + }); | ||
| 51 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 52 | + strictEqual(code, null); | ||
| 53 | + strictEqual(killSignal, 'SIGKILL'); | ||
| 54 | + })); | ||
| 31 | 55 | cp.on('error', mustCall((err) => { | |
| 32 | 56 | strictEqual(err.name, 'AbortError'); | |
| 33 | 57 | })); | |
| 34 | 58 | } | |
| 59 | + | ||
| 60 | + { | ||
| 61 | + // Test aborting a cp before close but after exit | ||
| 62 | + const ac = new AbortController(); | ||
| 63 | + const { signal } = ac; | ||
| 64 | + const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
| 65 | + signal | ||
| 66 | + }); | ||
| 67 | + cp.on('exit', mustCall(() => { | ||
| 68 | + ac.abort(); | ||
| 69 | + })); | ||
| 70 | + cp.on('error', mustNotCall()); | ||
| 71 | + | ||
| 72 | + setTimeout(() => cp.kill(), 1); | ||
| 73 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,20 +3,25 @@ | |||
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | - const cp = require('child_process'); | ||
| 6 | + const { spawn } = require('child_process'); | ||
| 7 | + const fixtures = require('../common/fixtures'); | ||
| 7 | 8 | ||
| 9 | + const aliveScript = fixtures.path('child-process-stay-alive-forever.js'); | ||
| 8 | 10 | { | |
| 9 | 11 | // Verify that passing an AbortSignal works | |
| 10 | 12 | const controller = new AbortController(); | |
| 11 | 13 | const { signal } = controller; | |
| 12 | 14 | ||
| 13 | - const echo = cp.spawn('echo', ['fun'], { | ||
| 14 | - encoding: 'utf8', | ||
| 15 | - shell: true, | ||
| 16 | - signal | ||
| 15 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 16 | + signal, | ||
| 17 | 17 | }); | |
| 18 | 18 | ||
| 19 | - echo.on('error', common.mustCall((e) => { | ||
| 19 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 20 | + assert.strictEqual(code, null); | ||
| 21 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 22 | + })); | ||
| 23 | + | ||
| 24 | + cp.on('error', common.mustCall((e) => { | ||
| 20 | 25 | assert.strictEqual(e.name, 'AbortError'); | |
| 21 | 26 | })); | |
| 22 | 27 | ||
@@ -30,13 +35,76 @@ const cp = require('child_process'); | |||
| 30 | 35 | ||
| 31 | 36 | controller.abort(); | |
| 32 | 37 | ||
| 33 | - const echo = cp.spawn('echo', ['fun'], { | ||
| 34 | - encoding: 'utf8', | ||
| 35 | - shell: true, | ||
| 36 | - signal | ||
| 38 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 39 | + signal, | ||
| 40 | + }); | ||
| 41 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 42 | + assert.strictEqual(code, null); | ||
| 43 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 44 | + })); | ||
| 45 | + | ||
| 46 | + cp.on('error', common.mustCall((e) => { | ||
| 47 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 48 | + })); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + { | ||
| 52 | + // Verify that waiting a bit and closing works | ||
| 53 | + const controller = new AbortController(); | ||
| 54 | + const { signal } = controller; | ||
| 55 | + | ||
| 56 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 57 | + signal, | ||
| 58 | + }); | ||
| 59 | + | ||
| 60 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 61 | + assert.strictEqual(code, null); | ||
| 62 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 63 | + })); | ||
| 64 | + | ||
| 65 | + cp.on('error', common.mustCall((e) => { | ||
| 66 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 67 | + })); | ||
| 68 | + | ||
| 69 | + setTimeout(() => controller.abort(), 1); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + { | ||
| 73 | + // Test passing a different killSignal | ||
| 74 | + const controller = new AbortController(); | ||
| 75 | + const { signal } = controller; | ||
| 76 | + | ||
| 77 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 78 | + signal, | ||
| 79 | + killSignal: 'SIGKILL', | ||
| 37 | 80 | }); | |
| 38 | 81 | ||
| 39 | - echo.on('error', common.mustCall((e) => { | ||
| 82 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 83 | + assert.strictEqual(code, null); | ||
| 84 | + assert.strictEqual(killSignal, 'SIGKILL'); | ||
| 85 | + })); | ||
| 86 | + | ||
| 87 | + cp.on('error', common.mustCall((e) => { | ||
| 40 | 88 | assert.strictEqual(e.name, 'AbortError'); | |
| 41 | 89 | })); | |
| 90 | + | ||
| 91 | + setTimeout(() => controller.abort(), 1); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + { | ||
| 95 | + // Test aborting a cp before close but after exit | ||
| 96 | + const controller = new AbortController(); | ||
| 97 | + const { signal } = controller; | ||
| 98 | + | ||
| 99 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 100 | + signal, | ||
| 101 | + }); | ||
| 102 | + | ||
| 103 | + cp.on('exit', common.mustCall(() => { | ||
| 104 | + controller.abort(); | ||
| 105 | + })); | ||
| 106 | + | ||
| 107 | + cp.on('error', common.mustNotCall()); | ||
| 108 | + | ||
| 109 | + setTimeout(() => cp.kill(), 1); | ||
| 42 | 110 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments