Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I think child_process.fork() should officially support { detached: true } #17592

Closed
mmorearty opened this issue Dec 10, 2017 · 6 comments
Closed
Labels
child_process Issues and PRs related to the child_process subsystem. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors.

Comments

@mmorearty
Copy link
Contributor

  • Version: v9.2.1
  • Platform: Linux ip-172-31-29-251 3.13.0-135-generic lib: micro-optimize url.resolve() #184-Ubuntu SMP Wed Oct 18 11:55:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: child_process

child_process.spawn() supports an option detached which "makes it possible for the child process to continue running after the parent exits." child_process.fork() does not officially support detached as one of its options (by "officially support", I mean that it is not documented as a valid option); but I think it should be officially supported.

My reasons:

  1. It works today. (details below)
  2. It's useful. (details below)
  3. I can't think of any reason that it shouldn't be supported.

If you agree, then no code changes would be required, but the documentation for child_process.fork() would need to list detached as a valid option. (Also, TypeScript's @types/node would need to be updated, but that would probably be a separate GitHub issue somewhere else.)

1. It works today: First of all, if you look at the current source for child_process.fork(), it's clear (and not surprising) that fork() is just a simple wrapper around spawn(). It passes most options through unchanged.

To prove that detached works with fork(): save this as demo.js:

// launch with "node demo.js" or "node demo.js detached"

const child_process = require('child_process')

if (process.argv.indexOf('--daemon') === -1) {
    let options = {};
    if (process.argv.indexOf('detached') >= 0) {
        options.detached = true;
    }

    const child = child_process.fork(__filename, ['--daemon'], options);
    console.log('hello from parent; press ^C to terminate parent')
    process.stdin.read()
} else {
    console.log(`hello from child, my pid is ${process.pid}`)
    setInterval(() => {}, 5000)
}

To see the NON-detached behavior, launch it with node demo.js. It will call fork(), so there are now two instances running. Then press ^C; if you do ps aux | grep demo.js you will see that both instances terminated.

To see the detached behavior, repeat the above but with node demo.js detached. In this case, after ^C, the child process is still running.

2. It's useful: child_process.fork() can be useful for starting daemon processes, and detached is certainly useful for daemons.

@Trott Trott added the child_process Issues and PRs related to the child_process subsystem. label Dec 10, 2017
@Trott
Copy link
Member

Trott commented Dec 10, 2017

@bnoordhuis @cjihrig

@cjihrig
Copy link
Contributor

cjihrig commented Dec 11, 2017

I can think of at least one corner case, but it's probably unlikely to be affected since no code changes would be required. Code that checks for the existence of APIs like process.send() could break once the IPC channel is closed.

If it works already, people find it useful, and no one has a good reason not to support it, then we probably should document it (although I still think spawn() makes more sense here). We'd also need tests to make sure it works consistently on all supported platforms.

@ramsgoli
Copy link
Contributor

I'd be happy to work on a PR for this if the idea goes through! 👍

@maclover7 maclover7 added the feature request Issues that request new features to be added to Node.js. label Dec 25, 2017
@refack refack added the good first issue Issues that are suitable for first-time contributors. label Nov 11, 2018
@refack
Copy link
Contributor

refack commented Nov 11, 2018

FTR: this is basically a "need to document and test" issue.

@nanomosfet
Copy link
Contributor

nanomosfet commented Nov 12, 2018

Hi @refack. I would love to contribute to this issue as my first issue.

Would this be testing the functionality that @mmorearty documented?

const child_process = require('child_process')

if (process.argv.indexOf('--daemon') === -1) {
    let options = {};
    if (process.argv.indexOf('detached') >= 0) {
        options.detached = true;
    }

    const child = child_process.fork(__filename, ['--daemon'], options);
    console.log('hello from parent; press ^C to terminate parent')
    process.stdin.read()
} else {
    console.log(`hello from child, my pid is ${process.pid}`)
    setInterval(() => {}, 5000)
}

From some quick research I think the path would be to create a file named test-child-process-fork-detached.js under /test/parallel/.

There I would be creating the test for the above code. Then I might follow the test /test/paralell/test-child-process-detached.js as a guide for testing the functionality.

Did I miss anything?

Edit:
I went ahead and added a test demonstrating the above functionality.
master...nanomosfet:add-fork-detached-test

does this look right?

Also where would I update the documentation for this?

nanomosfet added a commit to nanomosfet/node that referenced this issue Nov 24, 2018
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: nodejs#17592
Trott pushed a commit to Trott/io.js that referenced this issue Nov 28, 2018
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: nodejs#17592

PR-URL: nodejs#24524
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
@Trott
Copy link
Member

Trott commented Nov 28, 2018

Fixed in f051737

@Trott Trott closed this as completed Nov 28, 2018
targos pushed a commit that referenced this issue Nov 29, 2018
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>
refack pushed a commit to refack/node that referenced this issue Jan 14, 2019
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: nodejs#17592

PR-URL: nodejs#24524
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 12, 2019
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>
rvagg pushed a commit that referenced this issue Feb 28, 2019
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests

7 participants