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

child_process.spawn() is unreliable on Promise.all() completion #23428

Closed
alexpreynolds opened this issue Oct 11, 2018 · 3 comments
Closed

child_process.spawn() is unreliable on Promise.all() completion #23428

alexpreynolds opened this issue Oct 11, 2018 · 3 comments
Labels
child_process Issues and PRs related to the child_process subsystem. promises Issues and PRs related to ECMAScript promises.

Comments

@alexpreynolds
Copy link

alexpreynolds commented Oct 11, 2018

  • Version:
$ node -v
v10.12.0
  • Platform:
$ uname -a
Linux ip-172-31-8-12 4.15.0-1021-aws #21-Ubuntu SMP Tue Aug 28 10:23:07 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem:
...
var spawn = require('child_process').spawn;
...
  • Setup:

I have a few promises that I fulfill with Promise.all().

When fulfilled, within the finally() block, I try to spawn a process and detach it.

var p1 = new Promise(function(resolve, reject) { ... });
var p2 = new Promise(function(resolve, reject) { ... });
var p3 = new Promise(function(resolve, reject) { ... });
...
Promise.all([p1, p2, p3, ...])
    .then(function(vals) {
    })
    .catch(function(errs) {
        if (errs)
            console.log(errs);
    })
    .finally(function() {
        var myScript = path.join(__dirname, '..', 'bin', 'myScript.js');
        var myScriptSpawn = spawn(myScript
            ['-a', '-b', '-c', ...],
            {
                stdio : 'ignore',
                detached : true,
                shell : '/usr/bin/node',
                env : process.env
            });
        myScriptSpawn.unref();
    })
  • Problem:

Occasionally the process is spawned, occasionally the process is not spawned. This fails approximately 33% of the time. No warning, exception, or error rises when spawn fails.

  • Debugging:

The process myScript always works if run manually from the bash shell, outside of node. The process does not get spawned reliably when attempted within Promise.all() within node. Moving the function out of the Promise.all() fulfillment completion stage seems to work consistently.

@Trott Trott added child_process Issues and PRs related to the child_process subsystem. promises Issues and PRs related to ECMAScript promises. labels Oct 11, 2018
@apapirovski
Copy link
Member

Do all of your promises keep the event loop alive? It's possible for the process to die because there's nothing keeping the event loop alive (a promise in itself is not enough).

@alexpreynolds
Copy link
Author

I thought the node.js environment is not supposed to quit if there are items queued in the event loop.

@targos
Copy link
Member

targos commented Jun 14, 2020

I'm closing this issue because of its lack of recent activity. Feel free to reopen or ask that we reopen if you think that it should still be addressed.

@targos targos closed this as completed Jun 14, 2020
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. promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

No branches or pull requests

4 participants