Skip to content

paramiko.ssh_exception.SSHException: Unable to open channel. #1508

Open

Description

@kmoza

I am using paramiko==2.6.0 for ssh connection from one device to another. For almost all the time I was successful in establishing the connection when executing any command on remote Linux device. However, for one instance the connection failed with below error.

    _, stdout, stderr = self.sshObj.exec_command(psCommand)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 508, in exec_command
    chan = self._transport.open_session(timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/transport.py", line 879, in open_session
    timeout=timeout,
  File "/usr/local/lib/python2.7/dist-packages/paramiko/transport.py", line 1006, in open_channel
    raise e
paramiko.ssh_exception.SSHException: Unable to open channel.

Activity

  1. timothy-thomas commented on Dec 5, 2019

    @timothy-thomas

    I have experienced this as well. I am using paramiko to ping a linux cluster for job completion every 10s. It seems like it is the volume of subsequent calls that is causing the error. Any ideas on this issue?

  2. bitprophet commented on Dec 9, 2019

    @bitprophet
    Member

    This is new to me (though I haven't searched tracker for other examples of it yet). If someone who's able to reproduce this can try a few different versions of Paramiko, and also provide details about their local and remote ends (eg which version of remote sshd is this, etc) that might help us narrow it down or reproduce.

  3. dsegan commented on Feb 20, 2020

    @dsegan

    I believe the problem is the "timeout loop" at https://github.com/paramiko/paramiko/blob/master/paramiko/transport.py#L1000 — effectively, it does not loop at all. If connection is established in the initial event.wait(0.1), it succeeds, otherwise it fails with SSHException("Unable to open channel.").

    It seems the intent was to do something along these lines (patch):

      start_ts = time.time()
      while True:
          event.wait(0.1)
          if not self.active:
              e = self.get_exception()
              if e is not None: 
                  raise e
          if event.is_set():
              break
          elif start_ts + timeout < time.time():
              raise SSHException("Timeout opening channel")
    

    The default timeout of 3600s might be too long, but that's probably a separate discussion from having a timeout of 0.1s which we have now.

    There might be a similar problem on https://github.com/paramiko/paramiko/blob/master/paramiko/transport.py#L1132

  4. dsegan commented on Feb 20, 2020

    @dsegan

    Alternatively, if the intent was to have a timeout of 0.1s, the entire "while True" loop can be scrapped, and "Unable to open channel" exception can be replaced with "Timeout opening channel", with the timeout being whatever time is passed into event.wait(). The "if event.is_set()" is also unneeded in that case, since there is nothing to break out of. Threading.event documentation suggests that event.wait() will return True if the flag has been set, and something else if it has timed out.

    I am assuming that another thread that establishes a connection will call event.set() once done, and it only has those 0.1s to do it: I haven't dove deeper into code.

  5. dsegan commented on Feb 20, 2020

    @dsegan

    Upon further reading, I can see that it does loop if SSH connection is already active. So perhaps it's the callsite's responsibility to ensure that the SSH connection has been established and the transport is active before attempting to open_channel()s?

    In that case, it would be good to communicate that with an error message instead: "raise SSHException('Transport not active yet')" instead of the "Unable to open channel".

  6. dsegan commented on Feb 20, 2020

    @dsegan

    And for some final context, we are using fabric2 to manage a bunch of runners, and they sometime fail with "Unable to open channel". It seems the problem is that it is transport become inactive while attempting to open channels (using open_session). While it's a bit confusing how transport can become inactive (only stop_thread() sets self.active = False), there seems to be an issue somewhere (perhaps in the fabric Connection.exit()).

  7. stanislavkozlovski commented on Aug 4, 2020

    @stanislavkozlovski

    Seeing the same thing

  8. phocheng commented on Sep 21, 2020

    @phocheng

    Have the same issue when a parallel job trying to connect to different hosts and execute task. The issue did not appear at the beginning. And it seems no relationship with timeout because some tasks succeeded with a longer execution time(for example: more than 1 hour) and some failed in a short time(17 mins).
    It works well in single execution.
    Any thought or workaround to fix the issue?
    Error:
    packages/paramiko/transport.py", line 1006, in open_channel
    paramiko.ssh_exception.SSHException: Unable to open channel.

    background: we switched fabric from 1.14.1 to 2.5.0. In fab1, we do not have such issue in parallel job.
    fabric=2.5.0
    invoke==1.4.1
    paramiko==2.6.0

  9. phocheng commented on Sep 22, 2020

    @phocheng

    More trace info:
    22:32:49 result = self.conn.run(cmd, warn=True, hide=True)
    22:32:49 File "", line 2, in run
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/fabric/connection.py", line 30, in opens
    22:32:49 return method(self, *args, **kwargs)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/fabric/connection.py", line 721, in run
    22:32:49 return self._run(self._remote_runner(), command, **kwargs)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/invoke/context.py", line 101, in _run
    22:32:49 return runner.run(command, **kwargs)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/invoke/runners.py", line 363, in run
    22:32:49 return self._run_body(command, **kwargs)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/invoke/runners.py", line 411, in _run_body
    22:32:49 self.start(command, self.opts["shell"], self.env)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/fabric/runners.py", line 36, in start
    22:32:49 self.channel = self.context.create_session()
    22:32:49 File "", line 2, in create_session
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/fabric/connection.py", line 30, in opens
    22:32:49 return method(self, *args, **kwargs)
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/fabric/connection.py", line 698, in create_session
    22:32:49 channel = self.transport.open_session()
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/paramiko/transport.py", line 879, in open_session
    22:32:49 timeout=timeout,
    22:32:49 File "/home/jenkins/venv/lib/python2.7/site-packages/paramiko/transport.py", line 1006, in open_channel
    22:32:49 raise e
    22:32:49 paramiko.ssh_exception.SSHException: Unable to open channel

  10. phocheng commented on Sep 29, 2020

    @phocheng

    @bitprophet would you please take a look at the issue? any suggestion or workaround to fix this kind of issue?

  11. added this to the Next bugfix milestone on Oct 1, 2020
  12. sivachandranp commented on Nov 25, 2020

    @sivachandranp

    Is this issue got fixed? Im using paramiko 2.7.1 version.

  13. phocheng commented on Nov 25, 2020

    @phocheng

    same question. Do we have any estimate about the fixing? Or, do we have any quick workaround?

  14. deleted a comment from dmrlx on Dec 23, 2020
  15. remdragon commented on Apr 15, 2021

    @remdragon

    I am also being affected by this bug. Is there an ETA? Thanks!

  16. 12 remaining items

  17. bitprophet commented on Jan 27, 2023

    @bitprophet
    Member

    Comments on #1705 imply that the issue is only reliably reproduced on Python 2.7, which is no longer in scope. If one of y'all can confirm steps to reliably repro under Python 3.6+, I'll take a look at importing the alleged fix from paramiko-ng. Thanks!

  18. remdragon commented on Jan 27, 2023

    @remdragon
  19. remdragon commented on Jan 27, 2023

    @remdragon
  20. modified the milestones: p2, Next bugfix on Jan 28, 2023
  21. bitprophet commented on Mar 10, 2023

    @bitprophet
    Member

    I tried reproducing this and still cannot under Python 3:

    @remdragon and others - if you can give me a better idea of your local parameters for reproducing under Python 3 (eg a similar test script as in that other ticket, or at least something like "we're seeing it every N connection attempts" so I have some idea of a reasonable upper bound) that'd be ideal, because I'm still very loathe to patch a critical code path for an issue that I cannot personally reproduce.

  22. gtpalmer commented on Mar 10, 2023

    @gtpalmer

    So luckily this script still exists randomly on my work computer's home directory.

    I ran it the first time and nothing happened within 5-10 minutes.

    I ran it again without restarting sshd and switched my focus and it did indeed fail, sometime within 30 minutes:

    $ python3 test_ssh.py 
    Traceback (most recent call last):
      File "test_ssh.py", line 44, in <module>
        raise caught_exception
      File "/home/gpalmer/python/lib/python3.8/concurrent/futures/thread.py", line 57, in run
        result = self.fn(*self.args, **self.kwargs)
      File "test_ssh.py", line 36, in run_command_in_thread
        raise e
      File "test_ssh.py", line 21, in run_command_in_thread
        ret = ssh.exec_command(cmd)
      File "/home/gpalmer/python/lib/python3.8/site-packages/paramiko/client.py", line 516, in exec_command
        chan.exec_command(command)
      File "/home/gpalmer/python/lib/python3.8/site-packages/paramiko/channel.py", line 72, in _check
        return func(self, *args, **kwds)
      File "/home/gpalmer/python/lib/python3.8/site-packages/paramiko/channel.py", line 257, in exec_command
        self._wait_for_event()
      File "/home/gpalmer/python/lib/python3.8/site-packages/paramiko/channel.py", line 1226, in _wait_for_event
        raise e
    paramiko.ssh_exception.SSHException: Channel closed.
    

    And in the sshd window:

    channel_by_id: 2: bad id: channel free
    Disconnecting user gpalmer ::1 port 58238: ieof packet referred to nonexistent channel 2
    

    I'm running Python 3.8.5 and pip show paramiko yields:

    Name: paramiko
    Version: 2.10.4
    Summary: SSH2 protocol library
    Home-page: https://paramiko.org
    Author: Jeff Forcier
    Author-email: [email protected]
    License: LGPL
    Location: /home/gpalmer/python/lib/python3.8/site-packages
    Requires: bcrypt, cryptography, pynacl, six
    Required-by: ep-shell, fabric, mock-ssh-server, scp
    
  23. gtpalmer commented on Mar 10, 2023

    @gtpalmer

    Also the sshd I'm running is part of OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions