Node: Spawned process don't trigger close or exit event

Created on 31 Dec 2015  路  12Comments  路  Source: nodejs/node

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?

child_process

Most helpful comment

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');
});

All 12 comments

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()?

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.

Does this happen with the latest master version of node? Wondering if it's an echo of https://github.com/nodejs/node/issues/4049.

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

@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.

Closing, no follow-up.

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');
});

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.

@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.

+1

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.

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielstaleiny picture danielstaleiny  路  3Comments

cong88 picture cong88  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments