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 <luigipinca@gmail.com>
1 parent 8c93bd4 commit 5966dbe

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎doc/api/child_process.md‎

Lines changed: 3 additions & 0 deletions
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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
constfork=require('child_process').fork;
2+
constpath=require('path');
3+
4+
constchild=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();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
constfork=require('child_process').fork;
5+
constfixtures=require('../common/fixtures');
6+
7+
constnonPersistentNode=fork(
8+
fixtures.path('parent-process-nonpersistent-fork.js'),
9+
[],
10+
{silent: true});
11+
12+
letchildId=-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)