Skip to content

Commit 5966dbe

Browse files
nanomosfetBethGriggs
authored andcommitted
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: #17592 PR-URL: #24524 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 8c93bd4 commit 5966dbe

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/api/child_process.md

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ changes:
325325
* `args` {string[]} List of string arguments.
326326
* `options` {Object}
327327
* `cwd` {string} Current working directory of the child process.
328+
* `detached` {boolean} Prepare child to run independently of its parent
329+
process. Specific behavior depends on the platform, see
330+
[`options.detached`][]).
328331
* `env` {Object} Environment key-value pairs.
329332
* `execPath` {string} Executable used to create the child process.
330333
* `execArgv` {string[]} List of string arguments passed to the executable.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fork = require('child_process').fork;
2+
const path = require('path');
3+
4+
const child = fork(
5+
path.join(__dirname, 'child-process-persistent.js'),
6+
[],
7+
{ detached: true, stdio: 'ignore' }
8+
);
9+
10+
console.log(child.pid);
11+
12+
child.unref();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const fork = require('child_process').fork;
5+
const fixtures = require('../common/fixtures');
6+
7+
const nonPersistentNode = fork(
8+
fixtures.path('parent-process-nonpersistent-fork.js'),
9+
[],
10+
{ silent: true });
11+
12+
let childId = -1;
13+
14+
nonPersistentNode.stdout.on('data', (data) => {
15+
childId = parseInt(data, 10);
16+
nonPersistentNode.kill();
17+
});
18+
19+
process.on('exit', () => {
20+
assert.notStrictEqual(childId, -1);
21+
// Killing the child process should not throw an error
22+
process.kill(childId);
23+
});

0 commit comments

Comments
 (0)