-
Notifications
You must be signed in to change notification settings - Fork 834
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
NodeJS cannot spawn a process that reads from STDIN #1774
Comments
I think this might be a bug with Node.JS because the following example works as expected: var child_process = require('child_process');
/**
* Create the child process, with output piped to the script's stdout
*/
var wc = child_process.spawn('wc');
/**
* Write some data to stdin, and then use stream.end() to signal that we're
* done writing data.
*/
wc.stdin.write('test test');
wc.stdin.end();
wc.stdout.on('data', (output) => {
console.log(output.toString());
}); |
I don't have a bash on ubuntu on windows environment. Is this an intermittent issue, or a consistent behavior? |
If I take the strace as a bona-fide replica of what happens in a Linux OS, here are the relevant portions pertinent to the piping descriptors and actions on them:
Under race condition scenarios, it is possible that the child's stdin (peer of 10) is closed before the child could even be spawned, and hence the error. Can you add a delay in parent and check? for example:
|
@gireeshpunathil This bug happens every time on Bash on Windows. I tested your suggestion and it works. The timeout on the Why do you think this problem occurs on Bash On Windows, Windows 7, and Windows Server 2012? I have tested the code Windows 10 and Debian Linux it works on those systems. Is it just because those operating systems hang on to the stdin fd longer. Do you think it is a bug with node.js? |
In terms of the code logic: the In terms of why this happens only in few systems: the question is when the parent clones a child, which process gets the CPU in the immediate scheduling interval which follows, and who gets the chance to execute first. If the child runs, it initializes its side of stdin and succeeds in reading. If the parent gets the CPU, it writes to child and closes the pipe. So the reason is probably deeply rooted in the way the scheduling works in different systems, in addition to the load in the system, adding to the race condition. In terms of whether it is an issue with the node: yes and no. Yes in terms of not handling an invalid fd at the startup - probably it could detect this and attach the null device to it. No in terms of the parent being responsible for maintaining the child streams healthy. On an HTTP stream, this is synonymous to doing response.end() followed by response.write(), which causes a similar fault. However, I recommend to raise it with node.js project. |
@gireeshpunathil Thanks for the response. Your logic makes sense. The reason I wanted to call end() was to let the child process know when all the data has been sent. I can send a null byte instead which is a better solution. This is not a bug in Bash on Windows so I will close this issue. |
The node folks may disagree (?) If this is not a bug in WSL, where's the bug? It's really crucial we pointpoint the cause and figure out how to fix this - node is an important part of the Linux eco-system, and one of the key reasons people use WSL in the first place. |
I happen to disagree with the node folks, they are being too strict about what types of file handles they allow as standard handles. But that's just like, my opinion, man... Regardless, this is fixed in recent skip-ahead insider builds. |
@benhillis to get this now, I just sign up for Windows 10 Insider Preview and install the Preview build? (anything else or is that it?) |
@benhillis which build number is it fixed in? |
@k00k - 17618 |
@benhillis I'm running 17686.1003 and I still see the problem. |
@k00k - interesting. Are you doing the same thing as the original post? If not can you please open a new issue? |
@benhillis Originally, I was doing something a bit different, I was/am using Apex Up to start my app which basically just proxies over to run my app. My app runs fine if I don't start with In any case, to test I did the |
I am also experiencing this issue as mentioned under #3307 (which was forwarded to this issue). |
When I originally read over this issue I misinterpreted and thought you were using the Windows version of node. This is a different issue and I'm taking a look now. |
I tracked this down to an issue with getsockaddr for unix sockets. Fix will be coming in an upcoming Insiders build. |
awesome news, thansk @benhillis ! mind sharing a summary of the issue? |
This should be fixed in Insider Build 17713 |
I have an issue that seems like it could be related: In child.js: let input = '';
process.stdin.on('readable', () => {
let chunk;
while (chunk = process.stdin.read()) input += chunk;
});
process.stdin.on('end', () => {
console.log(input);
}); Run: node -e "child_process.execSync('node child.js', {stdio: ['pipe','inherit','inherit'], input:'Hello'})" The Windows version: 10.0.17134.228 I didn't want to create a new issue in case this was just symptom of the same thing and/or was already fixed in an insider build (like the one mentioned just above). Let me know if this is still a problem, and if so, if I should open a new issue for it. |
Seems okay (prints "Hello") on 18219. |
Thanks @therealkenc. I actually now have reason to believe that it was working at some point on my machine. For my peace of mind can anyone run that snippet against stable (build 17134) and confirm that it does in fact hang for you too? |
@noinkling it just hangs for me? build 17134 |
@zanona Thanks for checking. I was concerned it might be just me. |
Yes this would have manifested from when interop was introduced in 14951, until 17713. Note the OP for this issue was 15055. |
Just confirming that my specific case is indeed fixed as of the October update (build 17763). |
A brief description
Using node.js v4.7.3 it is not possible to spawn a process that reads from stdin. An exception occurs that is only reproducible on Windows. On linux it works as expected.
Expected results
Should spawn process that can read from stdin
Actual results (with terminal output if applicable)
An exception occurs
15055
Create file spawn.js
Create file readStdin.js
Run command: node spawn.js
https://gist.github.com/soda0289/e6bdac390f2948d7a3499874ce5c0c45
nodejs
Here is the related node.js issue for the same problem on windows:
nodejs/node#11656
I would expect it to behave the same on bash for windows as it does in linux
The text was updated successfully, but these errors were encountered: