-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Accessing process.stdout terminates SSH connections #13278
Comments
Unable to reproduce with the steps you provided, on similar OS version and same node version.
Can you try the following code: const spawn = require('child_process').spawn;
const bat = spawn('ssh', ['user@host']);
bat.stdout.on('data', function(d) {
console.log(d.toString());
});
bat.stderr.on('data', function(d) {
console.log(d.toString());
});
bat.on('error', function(error) {
console.log('child err\'d with : ' + error);
});
bat.on('close', function(code, signal) {
console.log('child exited with code: ' + code + ' and signal: ' + signal);
});
process.stdin.on('readable', () => {
var chunk = process.stdin.read();
if (chunk !== null) {
bat.stdin.write(chunk);
}
}); This way, you can provide commands repeatedly and interactively. Also, in your failing scenario, pass --trace option to node to see how and why it is failing. (you need to capture the stdout of the child) |
On which remote linux are you testing? I was able to check the following versions of redhat enterprise linux:
Output with your example code (command
I fail to post the trace output for it is over 65536 characters, and attaching it as a file shows "Something went really wrong, and we can't process that file". So I uploaded it here: |
I expect that is a bug in ssh, not node. RHEL 5 ships openssh 4.3 from 2006. It has a huge list of known issues. |
Mine is RHEL 6.6 The trace file is good, and proves that the child node exited gracefully. So as @bnoordhuis pointed out, this can be a bug in the SSH itself. Probably
can confirm this? |
It will give me this, although I fail to interpret it correctly. The only thing which got my attention was Full output:
|
Nothing obvious found other than the ones which you pointed out. Will give it another try later. |
I'm going to close this as it sounds like it's a RHEL 5.7 problem, and we don't support 5.7 (or even 5.11, which is I think the only RHEL 5 version that still has extended support from Red Hat). Node 8 won't run at all on RHEL 5.x, and it's quite possible that Node 6 will cease to work on it at some point. I assume upgrading to RHEL 7.3 isn't an option for you, so feel free to continue updating this thread. I see you also raised npm/npm#16608. |
Yes, unfortunately I am bound to use RHEL 5.7 by customer requirement, so problably there is nothing that I can do. |
Here is the sequence of SSH protocol as understood by me, based on a modern SSH
The failing sequence which you showed above not only falling into an identifiable pattern, but also shows bad symptoms (close and ioctl calls) without evidence captured in the trace - probably decisions taken at the app (ssh) level not visible in the system calls. Can you reverse the direction (linux as source and mac as target host) and check? |
This is RHEL 5.7 -> RHEL 6.4:
And this is RHEL 5.7 -> MacOS
Also looks quite understandable to me, unlike the RHEL 5.7 output I posted earlier. |
Looks like the old ssh closed its stream(s) in an unknown manner is how the problem has surfaced. For example, if I add a line to force close the stdin of the ssh child, I get the same sequence as that of yours: setTimeout(() => {
bat.stdin.write('node test.js\n');
bat.stdin.end();
setTimeout(() => bat.stdin.write('pwd -P\n'), 2000);
}, 2000);
As a last experiment, can you trace the parent, much like you did it for the child? Just to make sure that node is not doing anything unexpected? |
I don't think you got the same output. There is some stuff missing, for example I am by no means an expert in neither SSH nor all the system calls, but my assumption was from the beginning (back when I thought the issue is caused by At least I find it especially interesting, that all the other remote commands (which of course also heavily use stdout/stderr) just work fine, for example When tracing the parent node, I would get this:
|
sorry for being vague - I meant |
Ah okay. This should be what I already posted in my first answer, now? Anyway heres the output again, starting from when I typed the remote command: |
no, I meant the parent (the script which invokes ssh). the last trace was for the child (test.js) right? |
Nope, Its |
thanks for the clarification, will have a look |
If this is of any importance, the local machine where the SSH connection was opened from is running MacOS 10.12.4. The info above applies to the remote machine where I connect to via SSH.
I am trying to write a library which serves to execute remote commads via SSH. In my first example I was using a Node C++ wrapper around
libssh
, but after researching this issue for a long time I am down to a very simple node.js sample program which demonstrates the error.Given the following test program:
With the following contents of
test.js
on the remote host:The program will disconnect the SSH session after the
node test.js
command is issued, resulting in the scheduledpwd -P
to fail and throw an exception.If you change the contents of
test.js
to justif (process) ;
(or anything else not "touching" process.stdout/process.stderr), the test program will run properly and execute all remote commands without problems.I have tried several commands, and none of the other commands I used (cp, cd, mkdir, ls, git, ssh, scp, ...) failed in this way, only running node.js scripts which use
process.stdout
/stderr
seem to fail.This is pretty unfortunate, because for my actual implementation (automatic build/deploy system) I must execute
npm install
, which however breaks the whole application because of the issue described above.I have researched this somewhat in detail here: https://stackoverflow.com/questions/43972863/ssh-npm-seems-to-break-ssh-non-pty-shells and also here: https://stackoverflow.com/questions/43784514/channel-in-libssh-gets-closed-for-no-obvious-reason?noredirect=1#comment74611459_43784514 (this one includes a working minimal C++ program using
libssh
and also the SSH protocol level packet/debug output which might help in investigating the issue.For me, as a non-SSH expert, it looks like that Node.js is trying to use a TTY, while the SSH connection it is running on doesn't have a TTY. Therefore the SSH server terminated the connection. So please note, the error happens only in this non-tty enviroment. Of course, if you open your terminal application and SSH to some machine, executing the above node script will work flawlessly.
The text was updated successfully, but these errors were encountered: