| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3b7cb75 commit ddae112
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: v15.6.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: v15.5.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 | |
|---|---|---|---|
@@ -585,6 +585,18 @@ function normalizeSpawnArguments(file, args, options) { | |||
| 585 | 585 | }; | |
| 586 | 586 | } | |
| 587 | 587 | ||
| 588 | + function abortChildProcess(child, killSignal) { | ||
| 589 | + if (!child) | ||
| 590 | + return; | ||
| 591 | + try { | ||
| 592 | + if (child.kill(killSignal)) { | ||
| 593 | + child.emit('error', new AbortError()); | ||
| 594 | + } | ||
| 595 | + } catch (err) { | ||
| 596 | + child.emit('error', err); | ||
| 597 | + } | ||
| 598 | + } | ||
| 599 | + | ||
| 588 | 600 | ||
| 589 | 601 | function spawn(file, args, options) { | |
| 590 | 602 | const child = new ChildProcess(); | |
@@ -594,21 +606,19 @@ function spawn(file, args, options) { | |||
| 594 | 606 | const signal = options.signal; | |
| 595 | 607 | // Validate signal, if present | |
| 596 | 608 | validateAbortSignal(signal, 'options.signal'); | |
| 597 | - | ||
| 609 | + const killSignal = sanitizeKillSignal(options.killSignal); | ||
| 598 | 610 | // Do nothing and throw if already aborted | |
| 599 | 611 | if (signal.aborted) { | |
| 600 | 612 | onAbortListener(); | |
| 601 | 613 | } else { | |
| 602 | 614 | signal.addEventListener('abort', onAbortListener, { once: true }); | |
| 603 | - child.once('close', | ||
| 615 | + child.once('exit', | ||
| 604 | 616 | () => signal.removeEventListener('abort', onAbortListener)); | |
| 605 | 617 | } | |
| 606 | 618 | ||
| 607 | 619 | function onAbortListener() { | |
| 608 | 620 | process.nextTick(() => { | |
| 609 | - child?.kill?.(options.killSignal); | ||
| 610 | - | ||
| 611 | - child.emit('error', new AbortError()); | ||
| 621 | + abortChildProcess(child, killSignal); | ||
| 612 | 622 | }); | |
| 613 | 623 | } | |
| 614 | 624 | } | |
@@ -764,19 +774,18 @@ function spawnWithSignal(file, args, options) { | |||
| 764 | 774 | } | |
| 765 | 775 | const child = spawn(file, args, opts); | |
| 766 | 776 | ||
| 767 | - if (options && options.signal) { | ||
| 777 | + if (options?.signal) { | ||
| 778 | + const killSignal = sanitizeKillSignal(options.killSignal); | ||
| 779 | + | ||
| 768 | 780 | function kill() { | |
| 769 | - if (child._handle) { | ||
| 770 | - child._handle.kill(options.killSignal || 'SIGTERM'); | ||
| 771 | - child.emit('error', new AbortError()); | ||
| 772 | - } | ||
| 781 | + abortChildProcess(child, killSignal); | ||
| 773 | 782 | } | |
| 774 | 783 | if (options.signal.aborted) { | |
| 775 | 784 | process.nextTick(kill); | |
| 776 | 785 | } else { | |
| 777 | - options.signal.addEventListener('abort', kill); | ||
| 786 | + options.signal.addEventListener('abort', kill, { once: true }); | ||
| 778 | 787 | const remove = () => options.signal.removeEventListener('abort', kill); | |
| 779 | - child.once('close', remove); | ||
| 788 | + child.once('exit', remove); | ||
| 780 | 789 | } | |
| 781 | 790 | } | |
| 782 | 791 | return child; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,48 +4,47 @@ const assert = require('assert'); | |||
| 4 | 4 | const exec = require('child_process').exec; | |
| 5 | 5 | const { promisify } = require('util'); | |
| 6 | 6 | ||
| 7 | - let pwdcommand, dir; | ||
| 8 | 7 | const execPromisifed = promisify(exec); | |
| 9 | 8 | const invalidArgTypeError = { | |
| 10 | 9 | code: 'ERR_INVALID_ARG_TYPE', | |
| 11 | 10 | name: 'TypeError' | |
| 12 | 11 | }; | |
| 13 | 12 | ||
| 14 | - | ||
| 13 | + let waitCommand = ''; | ||
| 15 | 14 | if (common.isWindows) { | |
| 16 | - pwdcommand = 'echo %cd%'; | ||
| 17 | - dir = 'c:\\windows'; | ||
| 15 | + waitCommand = 'TIMEOUT 120'; | ||
| 18 | 16 | } else { | |
| 19 | - pwdcommand = 'pwd'; | ||
| 20 | - dir = '/dev'; | ||
| 17 | + waitCommand = 'sleep 2m'; | ||
| 21 | 18 | } | |
| 22 | 19 | ||
| 23 | - | ||
| 24 | 20 | { | |
| 25 | 21 | const ac = new AbortController(); | |
| 26 | 22 | const signal = ac.signal; | |
| 27 | - const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 28 | - assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
| 23 | + const promise = execPromisifed(waitCommand, { signal }); | ||
| 24 | + assert.rejects(promise, /AbortError/, 'post aborted sync signal failed') | ||
| 25 | + .then(common.mustCall()); | ||
| 29 | 26 | ac.abort(); | |
| 30 | 27 | } | |
| 31 | 28 | ||
| 32 | 29 | { | |
| 33 | 30 | assert.throws(() => { | |
| 34 | - execPromisifed(pwdcommand, { cwd: dir, signal: {} }); | ||
| 31 | + execPromisifed(waitCommand, { signal: {} }); | ||
| 35 | 32 | }, invalidArgTypeError); | |
| 36 | 33 | } | |
| 37 | 34 | ||
| 38 | 35 | { | |
| 39 | 36 | function signal() {} | |
| 40 | 37 | assert.throws(() => { | |
| 41 | - execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 38 | + execPromisifed(waitCommand, { signal }); | ||
| 42 | 39 | }, invalidArgTypeError); | |
| 43 | 40 | } | |
| 44 | 41 | ||
| 45 | 42 | { | |
| 46 | 43 | const ac = new AbortController(); | |
| 47 | - const signal = (ac.abort(), ac.signal); | ||
| 48 | - const promise = execPromisifed(pwdcommand, { cwd: dir, signal }); | ||
| 44 | + const { signal } = ac; | ||
| 45 | + ac.abort(); | ||
| 46 | + const promise = execPromisifed(waitCommand, { signal }); | ||
| 49 | 47 | ||
| 50 | - assert.rejects(promise, /AbortError/).then(common.mustCall()); | ||
| 48 | + assert.rejects(promise, /AbortError/, 'pre aborted signal failed') | ||
| 49 | + .then(common.mustCall()); | ||
| 51 | 50 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { mustCall } = require('../common'); | ||
| 3 | + const { mustCall, mustNotCall } = require('../common'); | ||
| 4 | 4 | const { strictEqual } = require('assert'); | |
| 5 | 5 | const fixtures = require('../common/fixtures'); | |
| 6 | 6 | const { fork } = require('child_process'); | |
@@ -12,7 +12,10 @@ const { fork } = require('child_process'); | |||
| 12 | 12 | const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | |
| 13 | 13 | signal | |
| 14 | 14 | }); | |
| 15 | - cp.on('exit', mustCall()); | ||
| 15 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 16 | + strictEqual(code, null); | ||
| 17 | + strictEqual(killSignal, 'SIGTERM'); | ||
| 18 | + })); | ||
| 16 | 19 | cp.on('error', mustCall((err) => { | |
| 17 | 20 | strictEqual(err.name, 'AbortError'); | |
| 18 | 21 | })); | |
@@ -26,8 +29,44 @@ const { fork } = require('child_process'); | |||
| 26 | 29 | const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | |
| 27 | 30 | signal | |
| 28 | 31 | }); | |
| 29 | - cp.on('exit', mustCall()); | ||
| 32 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 33 | + strictEqual(code, null); | ||
| 34 | + strictEqual(killSignal, 'SIGTERM'); | ||
| 35 | + })); | ||
| 36 | + cp.on('error', mustCall((err) => { | ||
| 37 | + strictEqual(err.name, 'AbortError'); | ||
| 38 | + })); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + { | ||
| 42 | + // Test passing a different kill signal | ||
| 43 | + const ac = new AbortController(); | ||
| 44 | + const { signal } = ac; | ||
| 45 | + ac.abort(); | ||
| 46 | + const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
| 47 | + signal, | ||
| 48 | + killSignal: 'SIGKILL', | ||
| 49 | + }); | ||
| 50 | + cp.on('exit', mustCall((code, killSignal) => { | ||
| 51 | + strictEqual(code, null); | ||
| 52 | + strictEqual(killSignal, 'SIGKILL'); | ||
| 53 | + })); | ||
| 30 | 54 | cp.on('error', mustCall((err) => { | |
| 31 | 55 | strictEqual(err.name, 'AbortError'); | |
| 32 | 56 | })); | |
| 33 | 57 | } | |
| 58 | + | ||
| 59 | + { | ||
| 60 | + // Test aborting a cp before close but after exit | ||
| 61 | + const ac = new AbortController(); | ||
| 62 | + const { signal } = ac; | ||
| 63 | + const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), { | ||
| 64 | + signal | ||
| 65 | + }); | ||
| 66 | + cp.on('exit', mustCall(() => { | ||
| 67 | + ac.abort(); | ||
| 68 | + })); | ||
| 69 | + cp.on('error', mustNotCall()); | ||
| 70 | + | ||
| 71 | + setTimeout(() => cp.kill(), 1); | ||
| 72 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,20 +2,25 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const assert = require('assert'); | |
| 5 | - const cp = require('child_process'); | ||
| 5 | + const { spawn } = require('child_process'); | ||
| 6 | + const fixtures = require('../common/fixtures'); | ||
| 6 | 7 | ||
| 8 | + const aliveScript = fixtures.path('child-process-stay-alive-forever.js'); | ||
| 7 | 9 | { | |
| 8 | 10 | // Verify that passing an AbortSignal works | |
| 9 | 11 | const controller = new AbortController(); | |
| 10 | 12 | const { signal } = controller; | |
| 11 | 13 | ||
| 12 | - const echo = cp.spawn('echo', ['fun'], { | ||
| 13 | - encoding: 'utf8', | ||
| 14 | - shell: true, | ||
| 15 | - signal | ||
| 14 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 15 | + signal, | ||
| 16 | 16 | }); | |
| 17 | 17 | ||
| 18 | - echo.on('error', common.mustCall((e) => { | ||
| 18 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 19 | + assert.strictEqual(code, null); | ||
| 20 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 21 | + })); | ||
| 22 | + | ||
| 23 | + cp.on('error', common.mustCall((e) => { | ||
| 19 | 24 | assert.strictEqual(e.name, 'AbortError'); | |
| 20 | 25 | })); | |
| 21 | 26 | ||
@@ -29,13 +34,76 @@ const cp = require('child_process'); | |||
| 29 | 34 | ||
| 30 | 35 | controller.abort(); | |
| 31 | 36 | ||
| 32 | - const echo = cp.spawn('echo', ['fun'], { | ||
| 33 | - encoding: 'utf8', | ||
| 34 | - shell: true, | ||
| 35 | - signal | ||
| 37 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 38 | + signal, | ||
| 39 | + }); | ||
| 40 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 41 | + assert.strictEqual(code, null); | ||
| 42 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 43 | + })); | ||
| 44 | + | ||
| 45 | + cp.on('error', common.mustCall((e) => { | ||
| 46 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 47 | + })); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + { | ||
| 51 | + // Verify that waiting a bit and closing works | ||
| 52 | + const controller = new AbortController(); | ||
| 53 | + const { signal } = controller; | ||
| 54 | + | ||
| 55 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 56 | + signal, | ||
| 57 | + }); | ||
| 58 | + | ||
| 59 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 60 | + assert.strictEqual(code, null); | ||
| 61 | + assert.strictEqual(killSignal, 'SIGTERM'); | ||
| 62 | + })); | ||
| 63 | + | ||
| 64 | + cp.on('error', common.mustCall((e) => { | ||
| 65 | + assert.strictEqual(e.name, 'AbortError'); | ||
| 66 | + })); | ||
| 67 | + | ||
| 68 | + setTimeout(() => controller.abort(), 1); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + { | ||
| 72 | + // Test passing a different killSignal | ||
| 73 | + const controller = new AbortController(); | ||
| 74 | + const { signal } = controller; | ||
| 75 | + | ||
| 76 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 77 | + signal, | ||
| 78 | + killSignal: 'SIGKILL', | ||
| 36 | 79 | }); | |
| 37 | 80 | ||
| 38 | - echo.on('error', common.mustCall((e) => { | ||
| 81 | + cp.on('exit', common.mustCall((code, killSignal) => { | ||
| 82 | + assert.strictEqual(code, null); | ||
| 83 | + assert.strictEqual(killSignal, 'SIGKILL'); | ||
| 84 | + })); | ||
| 85 | + | ||
| 86 | + cp.on('error', common.mustCall((e) => { | ||
| 39 | 87 | assert.strictEqual(e.name, 'AbortError'); | |
| 40 | 88 | })); | |
| 89 | + | ||
| 90 | + setTimeout(() => controller.abort(), 1); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + { | ||
| 94 | + // Test aborting a cp before close but after exit | ||
| 95 | + const controller = new AbortController(); | ||
| 96 | + const { signal } = controller; | ||
| 97 | + | ||
| 98 | + const cp = spawn(process.execPath, [aliveScript], { | ||
| 99 | + signal, | ||
| 100 | + }); | ||
| 101 | + | ||
| 102 | + cp.on('exit', common.mustCall(() => { | ||
| 103 | + controller.abort(); | ||
| 104 | + })); | ||
| 105 | + | ||
| 106 | + cp.on('error', common.mustNotCall()); | ||
| 107 | + | ||
| 108 | + setTimeout(() => cp.kill(), 1); | ||
| 41 | 109 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments