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

Unable to write to child.stdin on Windows #4909

Closed
dmytrokyrychuk opened this issue Jan 27, 2016 · 3 comments
Closed

Unable to write to child.stdin on Windows #4909

dmytrokyrychuk opened this issue Jan 27, 2016 · 3 comments
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.

Comments

@dmytrokyrychuk
Copy link

I wrote the two scripts to illustrate a problem. Script in JS uses child_process to execute a script written in Python. JS one then writes something to child's stdin. Child then should print everything to stdout.

const child_process = require('child_process');

function log(data) {
  process.stdout.write(data);
}

log('Initializing child process\n');

const program = child_process.exec('python echo.py', {stdio: 'pipe'});
program.stdin.setEncoding('utf8');
program.stdout.pipe(process.stdout);
program.stderr.on('data', function(data) {
  log('error: ' + data);
});

const result = program.stdin.write('check\n', function(){
  log('write completed\n');
});
log('write succeded: ' + result + '\n');

setTimeout(function(){program.kill();}, 3000);

log('Initializing completed\n');
import sys
import msvcrt

while True:
    print 'Input: ', msvcrt.getch()

Below is the output I get when I run the script.

>node.exe echo.js
Initializing child process
write succeded: true
Initializing completed
write completed

Nothing happens after. However, when I run python script directly and type anything into a terminal, it prints input back as expected, which means that the python script works correctly and the problem is probably caused on the js side.

I don't experience such bug on Linux.

Can somebody explain, why can't I write into a child's stdin on Windows?

Thanks in advance.

@mscdex mscdex added child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform. labels Jan 27, 2016
@ivankravets
Copy link

+1

@cjihrig
Copy link
Contributor

cjihrig commented Feb 16, 2016

I would suggest using spawn() or even execFile(), as they do not run your program in a shell like exec() does. I think python might have an interactive flag, similar to Node's -i flag. Either way, both of these work for me on OS X and Windows 7 with the latest from master.

Using spawn():

'use strict';
const cp = require('child_process');
const child = cp.spawn('node', ['-i']);

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

child.stdin.write('process.versions;\n');
child.stdin.end();

Using execFile():

'use strict';
const cp = require('child_process');
const child = cp.execFile('node', ['-i'], (err, stdout, stderr) => {
  console.log(stdout);
});

child.stdin.write('process.versions;\n');
child.stdin.end();

@jasnell
Copy link
Member

jasnell commented Jun 7, 2016

Closing as there does not appear to be anything further to do here. Can reopen if that assumption is incorrect.

@jasnell jasnell closed this as completed Jun 7, 2016
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. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

5 participants