From f051737ee4ebb7dfa25abc91f7a4797bd8fe0cb6 Mon Sep 17 00:00:00 2001 From: timothy searcy Date: Tue, 20 Nov 2018 08:16:40 -0800 Subject: [PATCH] test: test and docs for detached fork process This tests child process fork component in detached mode by spawning a parent process that creates a child process. We kill the parent process and check if the child is still running. Fixes: https://github.com/nodejs/node/issues/17592 PR-URL: https://github.com/nodejs/node/pull/24524 Reviewed-By: Luigi Pinca --- doc/api/child_process.md | 3 +++ .../parent-process-nonpersistent-fork.js | 12 ++++++++++ .../test-child-process-fork-detached.js | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 test/fixtures/parent-process-nonpersistent-fork.js create mode 100644 test/parallel/test-child-process-fork-detached.js diff --git a/doc/api/child_process.md b/doc/api/child_process.md index b3079422bad..34f0e612f82 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -325,6 +325,9 @@ changes: * `args` {string[]} List of string arguments. * `options` {Object} * `cwd` {string} Current working directory of the child process. + * `detached` {boolean} Prepare child to run independently of its parent + process. Specific behavior depends on the platform, see + [`options.detached`][]). * `env` {Object} Environment key-value pairs. * `execPath` {string} Executable used to create the child process. * `execArgv` {string[]} List of string arguments passed to the executable. diff --git a/test/fixtures/parent-process-nonpersistent-fork.js b/test/fixtures/parent-process-nonpersistent-fork.js new file mode 100644 index 00000000000..49c0a1d1838 --- /dev/null +++ b/test/fixtures/parent-process-nonpersistent-fork.js @@ -0,0 +1,12 @@ +const fork = require('child_process').fork; +const path = require('path'); + +const child = fork( + path.join(__dirname, 'child-process-persistent.js'), + [], + { detached: true, stdio: 'ignore' } +); + +console.log(child.pid); + +child.unref(); diff --git a/test/parallel/test-child-process-fork-detached.js b/test/parallel/test-child-process-fork-detached.js new file mode 100644 index 00000000000..87c15173284 --- /dev/null +++ b/test/parallel/test-child-process-fork-detached.js @@ -0,0 +1,23 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const fork = require('child_process').fork; +const fixtures = require('../common/fixtures'); + +const nonPersistentNode = fork( + fixtures.path('parent-process-nonpersistent-fork.js'), + [], + { silent: true }); + +let childId = -1; + +nonPersistentNode.stdout.on('data', (data) => { + childId = parseInt(data, 10); + nonPersistentNode.kill(); +}); + +process.on('exit', () => { + assert.notStrictEqual(childId, -1); + // Killing the child process should not throw an error + process.kill(childId); +});