Skip to content

Handle case when proxy command dies during a read - #963

Closed
nbartos wants to merge 1 commit into
paramiko:masterfrom
nbartos:proxydeath
Closed

nbartos wants to merge 1 commit into
paramiko:masterfrom
nbartos:proxydeath

Conversation

@nbartos

@nbartos nbartos commented May 18, 2017

Copy link
Copy Markdown

If the proxy command dies during a read loop, we end up in a tight
loop that causes the process to max out a cpu until it's forcefully
killed.

In my experience, the easiest way to trigger this condition is to
use a proxy command and add some text to /etc/nologin on the
destination host (not the proxy host).

Another way to trigger this condition is to send a CTRL+C wile the
second command is running:

chan.exec_command('sleep 100')
stderr = chan.makefile_stderr('rb').read()

Comment thread paramiko/proxy.py Outdated
"""
if self.process.poll() is not None:
# Cleanup zombie.
self.process.wait()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.wait() is not really needed here - if .poll() did not return None, it already did the wait on the child and got the returncode and returned it. poll() is just like using libc wait() with WNOHANG

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@ploxiln

ploxiln commented May 18, 2017

Copy link
Copy Markdown
Contributor

A separate question which I'm not sure of, is if this is poll() is added in the best place. Maybe it would be better inside the while loop?

(Honestly not sure ... also btw I'm not the one who merges :)

@nbartos

nbartos commented May 18, 2017

Copy link
Copy Markdown
Author

In my tests it didn't matter if it was inside or outside, so I put it outside for performance reasons. What mattered was breaking the infinite loop of whatever was calling recv().

@nbartos

nbartos commented May 22, 2017

Copy link
Copy Markdown
Author

@bitprophet Any chance I could get a merge on this?

@bitprophet

Copy link
Copy Markdown
Member

Mildly worried there might be situations where, somehow, poll() is None and yet the subproc is still in the middle of tidying up (race condition or otherwise). Not worried enough to say no though. Adding to milestone. thanks!

@bitprophet

Copy link
Copy Markdown
Member

IF you could rebase against 2.0 that'd be grand, btw!

@nbartos

nbartos commented Jun 13, 2017

Copy link
Copy Markdown
Author

I've rebased this PR against master, and created a backport PR to the 2.0 branch: #995

@nbartos
nbartos changed the base branch from master to 2.0 June 14, 2017 23:32
@nbartos
nbartos force-pushed the proxydeath branch 2 times, most recently from c9430c5 to 76be849 Compare October 5, 2017 21:45
@nbartos

nbartos commented Jan 1, 2018

Copy link
Copy Markdown
Author

@bitprophet Does new code still go into the 2.0 branch first before master? Based on more recent PRs, new changes seem to be going into master.

@nbartos
nbartos changed the base branch from 2.0 to master January 15, 2018 00:42
@vincentbernat

Copy link
Copy Markdown

From my observation, when the proxy command dies, os.read() will return 0 bytes and we will be stuck inside the while loop. What about also raising a failure when b is empty? This happens to me all the time when I move my laptop to another network.

If the proxy command dies during a read loop, we end up in a tight
loop that causes the process to max out a cpu until it's forcefully
killed.

In my experience, the easiest way to trigger this condition is to
use a proxy command and add some text to /etc/nologin on the
destination host (not the proxy host).

Another way to trigger this condition is to send a CTRL+C wile the
second command is running:

chan.exec_command('sleep 100')
stderr = chan.makefile_stderr('rb').read()
@nbartos

nbartos commented May 27, 2018

Copy link
Copy Markdown
Author

@vincentbernat I believe the updated patch should address your problem as well. Would you mind testing to confirm?

@vincentbernat

Copy link
Copy Markdown

I think it should work for most cases. It doesn't handle the EOF condition (subprocess still alive but read has been closed) and it looses the current buffer (a very uncommon edge case). See #1180 for a proposition handling the root cause.

@nbartos

nbartos commented May 27, 2018

Copy link
Copy Markdown
Author

#1180 is a better implementation of this, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants