Skip to content

Error reading SSH protocol banner with paramiko with eventlet.spawn #337

Closed

Description

@vikt0rs

Hello All.

I've got SSHException: Error reading SSH protocol banner exception when I tried to spawn ssh connection with eventlet. You can run the following code to repeat this error:

import time

import eventlet
import paramiko

eventlet.monkey_patch(os=False)

COUNT = 20

def _get_ssh():
    try: 
        global COUNT
        new_ssh = paramiko.SSHClient()
        new_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        new_ssh.connect(hostname='localhost', username='user', password='top-secret')
        COUNT -= 1
        print COUNT
    finally:
        new_ssh.close()

for i in range(COUNT):
    eventlet.spawn(_get_ssh)

while COUNT > 0:
    time.sleep(0)

Activity

  1. jasondunsmore commented on Sep 11, 2014

    @jasondunsmore

    I'm seeing this with when using a ProxyCommand socket. Paramiko hangs at this line until the timeout is reached, then I see the SSH protocol banner error:
    https://github.com/paramiko/paramiko/blob/752507a29/paramiko/transport.py#L1700

    This works:

    >>> import paramiko
    >>> paramiko.Transport(paramiko.proxy.ProxyCommand("ssh -l user -i /path/to/.ssh/id_rsa -W dest_host:22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no jump_host")).start_client()
    

    This doesn't:

    >>> import paramiko
    >>> import eventlet
    >>> eventlet.monkey_patch()
    >>> paramiko.Transport(paramiko.proxy.ProxyCommand("ssh -l user -i /path/to/.ssh/id_rsa -W dest_host:22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no jump_host")).start_client()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/heat/lib/python2.7/site-packages/paramiko/transport.py", line 347, in start_client
        raise e
    paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
    

    This also works:

    >>> import paramiko
    >>> import eventlet
    >>> eventlet.monkey_patch(os=False, thread=False)
    >>> paramiko.Transport(paramiko.proxy.ProxyCommand("ssh -l user -i /path/to/.ssh/id_rsa -W dest_host:22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no jump_host")).start_client()
    

    If there are any workarounds, please let me know.

  2. jasondunsmore commented on Sep 11, 2014

    @jasondunsmore

    In the failure cases above, the CPU spikes during the 15 seconds until the banner timeout. During that time, paramiko is stuck in a loop between Packetizer._read_timeout() and ProxyCommand.recv():

    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    -> x = self.__socket.recv(128)
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(428)_read_timeout()
    -> except socket.timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(429)_read_timeout()
    -> pass
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(436)_read_timeout()
    -> if self.__closed:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(438)_read_timeout()
    -> now = time.time()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(439)_read_timeout()
    -> if now - start >= timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(422)_read_timeout()
    -> while True:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(423)_read_timeout()
    -> try:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    -> x = self.__socket.recv(128)
    (Pdb)
    --Call--
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(70)recv()
    -> def recv(self, size):
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(78)recv()
    -> try:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(79)recv()
    -> start = datetime.now()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(80)recv()
    -> while len(self.buffer) < size:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(81)recv()
    -> if self.timeout is not None:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(82)recv()
    -> elapsed = (datetime.now() - start).microseconds
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(83)recv()
    -> timeout = self.timeout * 1000 * 1000  # to microseconds
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(84)recv()
    -> if elapsed >= timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(85)recv()
    -> raise socket.timeout()
    (Pdb)
    timeout: timeout()
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(85)recv()
    -> raise socket.timeout()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(96)recv()
    -> except socket.timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(97)recv()
    -> raise  # socket.timeout is a subclass of IOError
    (Pdb)
    --Return--
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(97)recv()->None
    -> raise  # socket.timeout is a subclass of IOError
    (Pdb)
    timeout: timeout()
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    -> x = self.__socket.recv(128)
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(428)_read_timeout()
    -> except socket.timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(429)_read_timeout()
    -> pass
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(436)_read_timeout()
    -> if self.__closed:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(438)_read_timeout()
    -> now = time.time()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(439)_read_timeout()
    -> if now - start >= timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(422)_read_timeout()
    -> while True:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(423)_read_timeout()
    -> try:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    -> x = self.__socket.recv(128)
    (Pdb)
    --Call--
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(70)recv()
    -> def recv(self, size):
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(78)recv()
    -> try:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(79)recv()
    -> start = datetime.now()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(80)recv()
    -> while len(self.buffer) < size:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(81)recv()
    -> if self.timeout is not None:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(82)recv()
    -> elapsed = (datetime.now() - start).microseconds
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(83)recv()
    -> timeout = self.timeout * 1000 * 1000  # to microseconds
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(84)recv()
    -> if elapsed >= timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(85)recv()
    -> raise socket.timeout()
    (Pdb)
    timeout: timeout()
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(85)recv()
    -> raise socket.timeout()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(96)recv()
    -> except socket.timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(97)recv()
    -> raise  # socket.timeout is a subclass of IOError
    (Pdb)
    --Return--
    > /opt/heat/lib/python2.7/site-packages/paramiko/proxy.py(97)recv()->None
    -> raise  # socket.timeout is a subclass of IOError
    (Pdb)
    timeout: timeout()
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    -> x = self.__socket.recv(128)
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(428)_read_timeout()
    -> except socket.timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(429)_read_timeout()
    -> pass
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(436)_read_timeout()
    -> if self.__closed:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(438)_read_timeout()
    -> now = time.time()
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(439)_read_timeout()
    -> if now - start >= timeout:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(422)_read_timeout()
    -> while True:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(423)_read_timeout()
    -> try:
    (Pdb)
    > /opt/heat/lib/python2.7/site-packages/paramiko/packet.py(424)_read_timeout()
    [...]
    
  3. bitprophet commented on Sep 11, 2014

    @bitprophet
    Member

    No idea why I marked this as "needs investigation" before - we have never supported eventlet/greenlets :( That's not to say we wouldn't like to at some point but it's more of a feature request than an actual bug in my opinion.

    I've opened a new proper feature ticket for this (so hopefully anybody searching will see that it's an unimplemented feature) and will link the two together for posterity.

  4. lndbrg commented on Sep 11, 2014

    @lndbrg
    Contributor

    Greenthread/eventlet/gevent has never really been supported by paramiko. Since paramiko is written to use threads as its concurrency model there might be several weird raises with how eventlet interacts with our threading model.

  5. bitprophet commented on Sep 11, 2014

    @bitprophet
    Member

    See #389.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions