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

Spawned process don't trigger close or exit event #4500

Closed
Geelik opened this issue Dec 31, 2015 · 13 comments
Closed

Spawned process don't trigger close or exit event #4500

Geelik opened this issue Dec 31, 2015 · 13 comments
Labels
child_process Issues and PRs related to the child_process subsystem.

Comments

@Geelik
Copy link

Geelik commented Dec 31, 2015

Hi,

When i spawn a process and write in his stdin he will not trigger "close" or "exit" event.

emuObject.currentProcess = spawn(externalCommands[cmd], cmdArgs, {cwd:emuObject.cd});

            emuObject.stdin.pipe(emuObject.currentProcess.stdin);
            emuObject.currentProcess.stdout.on('data', function(data){
                self.emit('createOutput', emuObject, data.toString());
            });

            emuObject.currentProcess.stderr.on('data', function(data){
                self.emit('createError', emuObject, data.toString());
            });

            emuObject.currentProcess.on('exit', function(code, signal){
                console.log('exited process');
            });

            emuObject.currentProcess.on('close', function(code, signal){
                console.log('closed process');
                if(code == 0){
                    emuObject.currentProcess = null;
                    self.emit('createLine', emuObject, emuObject.cd);
                }
                else{
                    emuObject.currentProcess = null;
                    self.emit('createLine', emuObject, emuObject.cd);
                    self.emit('killEmu', emuObject, true);
                }
            });

i tried:

    emuObject.currentProcess.stdin.resume();
    emuObject.currentProcess.stdin.write(cmd + '\n');
    emuObject.currentProcess.stdin.pause();

but it does nothing
I tried too

    emuObject.currentProcess.stdin.write(cmd + '\n');
    emuObject.currentProcess.stdin.end();

but it end the child process instantly.

Just in case it can have something to do with this issue, i'm using nw.js

Any idea?

@mscdex mscdex added the child_process Issues and PRs related to the child_process subsystem. label Dec 31, 2015
@mscdex
Copy link
Contributor

mscdex commented Dec 31, 2015

What is the child process you're trying to spawn? Does it look for '\r\n' instead of '\n'? Did you mean/try emuObject.currentProcess.stdin.write() instead of emuObject.stdin.write()?

@Geelik
Copy link
Author

Geelik commented Dec 31, 2015

i spawn npm, if i spawn with -v it will print the version and exit properly but when i call with `ìnit`` after writing at each prompt, the package.json file is correctly created with all the information i writed but the process end without emitting any events.

Writing \r\n instead of \n does nothing too.

for emuObject.currentProcess.stdin.write() it's just a mistake when i wrote, i'll edit my initial post.

@davidvgalbraith
Copy link

Does this happen with the latest master version of node? Wondering if it's an echo of #4049.

@Geelik
Copy link
Author

Geelik commented Jan 1, 2016

Don't think it's related, my code work fine when the process executed don't need user input.My issue occur only when i write to the process's stdin. Itried to attach every single event possible and none got triggered after the process stop

@cjihrig
Copy link
Contributor

cjihrig commented Feb 16, 2016

@PIC-27 is this still an issue? If it is, can you please post a self contained example that reproduces the bug? Your original code sample can't be executed by collaborators trying to reproduce your problem.

@bnoordhuis
Copy link
Member

Closing, no follow-up.

@cpamp
Copy link

cpamp commented Mar 23, 2017

This appears to still be an issue. Not sure if it's npm or just how I'm using child_process.

Edit: I found I needed to end npm's stdin npm.stdin.end() and then the child process closes.


Works (close and exit fired):

var spawn = require('child_process').spawn;
var npm = spawn('npm.cmd');

npm.stdout.pipe(process.stdout);
process.stdin.pipe(npm.stdin);

npm.on('exit', function() {
    console.log('exit');
})

npm.on('close', function() {
    console.log('close');
});

Does not work (close and exit NOT fired):

var spawn = require('child_process').spawn;
var npm = spawn('npm.cmd', ['init']);

npm.stdout.pipe(process.stdout);
process.stdin.pipe(npm.stdin);

npm.on('exit', function() {
    console.log('exit');
})

npm.on('close', function() {
    console.log('close');
});

@motet-a
Copy link

motet-a commented Jul 14, 2017

I encountered the same issue in a Travis container, with the old Ubuntu Precise image. Switching to Ubuntu Trusty solved it.

Edit: I used Node.js v8.1.4.

@devinivy
Copy link

devinivy commented Oct 10, 2017

@cpamp in the case of npm init how did you know when to end npm.stdin? This behavior seems to occur only prior to node v8.

Edit: it also could be a change to npm. My version of npm is changing as I switch node versions.

@shanthakumarf22
Copy link

+1

@j03m
Copy link

j03m commented May 12, 2020

I can still replicate @cpamp's issue in node v12.14.1 but notably, only on windows. The same implementation swapping npm.cmd for npm works as expected on osx.

@arnotes
Copy link

arnotes commented May 24, 2020

i have a weird issue where sometimes it fires exit event and sometimes not.
it was a pretty simple command. spawn('dotnet', [--version]);

any can point me on a workaround?

@andrisi
Copy link

andrisi commented Apr 22, 2021

on Windows: for me "close" is not triggered if the process is started by task scheduler or pm2 but is if I start it myself from the command line. exit is triggered in both cases.

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.
Projects
None yet
Development

No branches or pull requests