Skip to content

"ieof packet referred to nonexistent channel" cause by channel.shutdown_write() (have script for reproduce ) #1705

Open

Description

@markhuangext359

Problem

  • Random raise Exception("paramiko.ssh_exception.SSHException: Unable to open channel.") when execute_command

Env

Client: SSH-2.0-paramiko_2.7.1
Server: SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3

Python traceback

Traceback (most recent call last):
  File "ssh_test.py", line 25, in <module>
    ret = ssh.exec_command(cmd)
  File "/testenv/local/lib/python2.7/site-packages/paramiko/client.py", line 508, in exec_command
    chan = self._transport.open_session(timeout=timeout)
  File "/testenv/local/lib/python2.7/site-packages/paramiko/transport.py", line 879, in open_session
    timeout=timeout,
  File "/testenv/local/lib/python2.7/site-packages/paramiko/transport.py", line 1006, in open_channel
    raise e
paramiko.ssh_exception.SSHException: Unable to open channel.

Client site log

2020-06-09 14:11:09,682 - paramiko.transport - DEBUG - [chan 9562] Max packet in: 32768 bytes
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - [chan 9562] remote_chanid: 1
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - [chan 9562] Max packet out: 32768 bytes
2020-06-09 14:11:09,722 - paramiko.transport - DEBUG - Secsh channel 9562 opened.
2020-06-09 14:11:09,747 - paramiko.transport - INFO - [chan 9561] 9561: __del__ called
2020-06-09 14:11:09,748 - paramiko.transport - DEBUG - [chan 9562] Sesch channel 9562 request ok
2020-06-09 14:11:09,751 - paramiko.transport - DEBUG - [chan 9562] EOF received (9562)
2020-06-09 14:11:09,752 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n  USER=root\n  LOGNAME=root\n  HOME=/root\n  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n  MAIL=/var/mail/root\n  SHELL=/bin/bash\n  TZ=Asia/Taipei\n  SSH_CLIENT=127.0.0.1 42875 1234\n  SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9563
2020-06-09 14:11:09,752 - paramiko.transport - INFO - [chan 9562] shutdown called, how: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] Init EOF sending (9562), remote_chanid: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] EOF sent (9562), remote_chanid: 1
2020-06-09 14:11:09,753 - paramiko.transport - DEBUG - [chan 9562] Skip EOF sending (9562), remote_chanid: 1
echo haha
2020-06-09 14:11:09,754 - paramiko.transport - DEBUG - [chan 9563] Max packet in: 32768 bytes
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - [chan 9563] remote_chanid: 0
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - [chan 9563] Max packet out: 32768 bytes
2020-06-09 14:11:09,794 - paramiko.transport - DEBUG - Secsh channel 9563 opened.
2020-06-09 14:11:09,820 - paramiko.transport - INFO - [chan 9562] 9562: __del__ called
2020-06-09 14:11:09,821 - paramiko.transport - DEBUG - [chan 9563] Sesch channel 9563 request ok
2020-06-09 14:11:09,824 - paramiko.transport - DEBUG - [chan 9563] EOF received (9563)
2020-06-09 14:11:09,824 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n  USER=root\n  LOGNAME=root\n  HOME=/root\n  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n  MAIL=/var/mail/root\n  SHELL=/bin/bash\n  TZ=Asia/Taipei\n  SSH_CLIENT=127.0.0.1 42875 1234\n  SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9564
2020-06-09 14:11:09,825 - paramiko.transport - INFO - [chan 9563] shutdown called, how: 1
2020-06-09 14:11:09,825 - paramiko.transport - DEBUG - [chan 9563] Init EOF sending (9563), remote_chanid: 0
2020-06-09 14:11:09,825 - paramiko.transport - DEBUG - [chan 9563] EOF sent (9563), remote_chanid: 0
2020-06-09 14:11:09,826 - paramiko.transport - DEBUG - [chan 9563] Skip EOF sending (9563), remote_chanid: 0
echo haha
2020-06-09 14:11:09,827 - paramiko.transport - DEBUG - [chan 9564] Max packet in: 32768 bytes
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - [chan 9564] remote_chanid: 1
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - [chan 9564] Max packet out: 32768 bytes
2020-06-09 14:11:09,866 - paramiko.transport - DEBUG - Secsh channel 9564 opened.
2020-06-09 14:11:09,892 - paramiko.transport - INFO - [chan 9563] 9563: __del__ called
2020-06-09 14:11:09,893 - paramiko.transport - DEBUG - [chan 9564] Sesch channel 9564 request ok
2020-06-09 14:11:09,896 - paramiko.transport - DEBUG - [chan 9564] EOF received (9564)
2020-06-09 14:11:09,896 - root - DEBUG - ('haha\n', 'debug1: SELinux support disabled\r\ndebug1: PAM: reinitializing credentials\r\ndebug1: permanently_set_uid: 0/0\r\nEnvironment:\n  USER=root\n  LOGNAME=root\n  HOME=/root\n  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games\n  MAIL=/var/mail/root\n  SHELL=/bin/bash\n  TZ=Asia/Taipei\n  SSH_CLIENT=127.0.0.1 42875 1234\n  SSH_CONNECTION=127.0.0.1 42875 127.0.0.1 1234\n')
9565
2020-06-09 14:11:09,897 - paramiko.transport - INFO - [chan 9564] shutdown called, how: 1
2020-06-09 14:11:09,897 - paramiko.transport - DEBUG - [chan 9564] Init EOF sending (9564), remote_chanid: 1
2020-06-09 14:11:09,897 - paramiko.transport - DEBUG - [chan 9564] EOF sent (9564), remote_chanid: 1
2020-06-09 14:11:09,898 - paramiko.transport - DEBUG - [chan 9564] Skip EOF sending (9564), remote_chanid: 1
echo haha
2020-06-09 14:11:09,899 - paramiko.transport - DEBUG - [chan 9565] Max packet in: 32768 bytes
2020-06-09 14:11:09,939 - paramiko.transport - INFO - Disconnect (code 2): ieof packet referred to nonexistent channel 1
2020-06-09 14:11:09,939 - paramiko.transport - INFO - [chan 9564] 9564: __del__ called
2020-06-09 14:11:09,981 - paramiko.transport - INFO - [chan 9565] 9565: __del__ called

Server site error

debug1: channel 1: new [server-session]
debug1: session_new: session 1
debug1: session_open: channel 1
debug1: session_open: session 1: link with channel 1
debug1: server_input_channel_open: confirm session
debug1: session_by_channel: session 0 channel 0
debug1: session_close_by_channel: channel 0 child 0
Close session: user root from 127.0.0.1 port 53213 id 0
debug1: channel 0: free: server-session, nchannels 2
debug1: server_input_channel_req: channel 1 request exec reply 1
debug1: session_by_channel: session 1 channel 1
debug1: session_input_channel_req: session 1 req exec
Starting session: command for root from 127.0.0.1 port 53213 id 1
debug1: Received SIGCHLD.
debug1: session_by_pid: pid 13605
debug1: session_exit_message: session 1 channel 1 pid 13605
debug1: session_exit_message: release channel 1
debug1: session_by_channel: session 1 channel 1
debug1: session_close_by_channel: channel 1 child 0
Close session: user root from 127.0.0.1 port 53213 id 1
debug1: channel 1: free: server-session, nchannels 1
channel_by_id: 1: bad id: channel free
Disconnecting user root 127.0.0.1 port 53213: ieof packet referred to nonexistent channel 1

Script for reproduce

Usege

  • python ssh_test.py [IP] [port] [usr] [pwd]
import sys
import paramiko
print paramiko.__version__
print paramiko


if len(sys.argv) != 5:
    print('{__name__} ip port usr passwd'.format(**locals()))
    print(sys.argv)
    sys.exit(1)
print sys.argv
ip = sys.argv[1]
port = sys.argv[2]
usr = sys.argv[3]
passwd = sys.argv[4]

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=port,
            username=usr, password=passwd)
while True:
    cmd = 'echo haha'
    print cmd
    ret = ssh.exec_command(cmd)
    _in, _out, _err = ret
    o = _out.read()
    e = _err.read()

    # follow line make bug
    del o, e, _in, _out, _err, ret
    # or replase by _in.close()

ssh.close()

Reproduce frequency

  • abount 1~5 min(s)

Possible reason

self.channel.shutdown_write()

  • comment out this code can workaround this problem

Related bug

#1584

Activity

  1. added 3 commits that reference this issue on Jun 12, 2020
    744bdb9
    73151b4
    2608d90
  2. added a commit that references this issue on Jul 29, 2020
    413ec61
  3. ploxiln commented on Jul 29, 2020

    @ploxiln
    Contributor

    I can't seem to reproduce this using your script, perhaps the timing of my systems is not quite the same ...

    Does this alternate fix work for you?

    --- a/paramiko/channel.py
    +++ b/paramiko/channel.py
    @@ -958,6 +958,7 @@ class Channel(ClosingContextManager):
                 # feign "read" shutdown
                 self.eof_received = 1
             if (how == 1) or (how == 2):
    +            m = None
                 self.lock.acquire()
                 try:
                     m = self._send_eof()
    @@ -1239,7 +1240,7 @@ class Channel(ClosingContextManager):
     
         def _send_eof(self):
             # you are holding the lock.
    -        if self.eof_sent:
    +        if self.eof_sent or self.closed:
                 return None
             m = Message()
             m.add_byte(cMSG_CHANNEL_EOF)

    EDIT: I tested running the script on my laptop with a wifi connection, against two servers: one in the same room running OpenSSH_7.9p1 Debian-10+deb10u2, and one on the opposite US coast running OpenSSH_8.2p1 Ubuntu-4ubuntu0.1. I also ran it under both python-2.7 and python-3.7, and modified the script in what really should be trivial ways, just to print() fully compatibly with python-3 and slightly more readable: https://gist.github.com/ploxiln/86dbd713dc959d87130e2066a0124a5e

  4. ploxiln commented on Aug 7, 2020

    @ploxiln
    Contributor

    another option that may be convenient to test this fix - try using the latest release of my fork, paramiko-ng, something like this:

    pip uninstall paramiko && pip install paramiko-ng==2.8.4
    

    (for more details: https://github.com/ploxiln/paramiko-ng/#installation https://github.com/ploxiln/paramiko-ng/releases)

  5. markhuang3310 commented on Aug 17, 2020

    @markhuang3310

    paramiko version=2.8.4
    python version=2.7.17

    The commit still not work

    debug1: sshd version OpenSSH_7.6, OpenSSL 1.0.2n  7 Dec 2017
    debug1: private host key #0: ssh-rsa SHA256:j6cndSCp07+2SSzzuYo4Ow1IR4QZ/Bpuh+SEg3mleRo
    debug1: private host key #1: ecdsa-sha2-nistp256 SHA256:sNzGwZnQ6GTK0JkHBetBgkWglt5xY1PPG3qUnooQ+sk
    debug1: private host key #2: ssh-ed25519 SHA256:MzPfFtTErImTBwHtv1ftfNBNSFp4P63odpBZ6lgqjQ8
    debug1: rexec_argv[0]='/usr/sbin/sshd'
    debug1: rexec_argv[1]='-p'
    debug1: rexec_argv[2]='1234'
    debug1: rexec_argv[3]='-d'
    debug1: Set /proc/self/oom_score_adj from 0 to -1000
    debug1: Bind to port 1234 on 0.0.0.0.
    Server listening on 0.0.0.0 port 1234.
    debug1: Bind to port 1234 on ::.
    Server listening on :: port 1234.
    ...(start testing)
    debug1: session_close_by_channel: channel 0 child 0
    Close session: user root from 127.0.0.1 port 49771 id 0
    debug1: channel 0: free: server-session, nchannels 1
    .channel_by_id: 0: bad id: channel free
    Disconnecting user root 127.0.0.1 port 49771: ieof packet referred to nonexistent channel 0
    debug1: do_cleanup
    debug1: PAM: cleanup
    debug1: PAM: closing session
    debug1: PAM: deleting credentials
    debug1: audit_event: unhandled event 12
    Traceback (most recent call last):
      File "reporduce2.py", line 28, in <module>
        ret = ssh.exec_command("echo haha")
      File "/venv/lib/python2.7/site-packages/paramiko/client.py", line 433, in exec_command
        chan = self._transport.open_session(timeout=timeout)
      File "/venv/lib/python2.7/site-packages/paramiko/transport.py", line 772, in open_session
        timeout=timeout)
      File "/venv/lib/python2.7/site-packages/paramiko/transport.py", line 896, in open_channel
        raise e
    paramiko.ssh_exception.SSHException: Unable to open channel.
    

    You can try to reproduce with the following command:

    /usr/sbin/sshd -p 1234 -d&
    python ssh_test.py 127.0.0.1 1234 usr pwd
    
  6. ploxiln commented on Aug 18, 2020

    @ploxiln
    Contributor

    OK, I can reproduce with localhost sshd, thanks.

  7. rusek commented on Sep 14, 2020

    @rusek
    Contributor

    This looks like a duplicate of #1115.

  8. ploxiln commented on Nov 27, 2021

    @ploxiln
    Contributor

    For what it's worth ... I've finally released paramiko-ng-2.8.6 with what I believe is a proper fix for this issue. Thanks for your script and instructions for reproducing this issue reliably (I also had to use python-2.7, rather than porting the script to python3, to reproduce it reliably). I also found that the #1115 was a great explanation of the cause and #1137 was a working model for how to fix it (though I took the liberty of modifying/minimizing it).

  9. harixxxx commented on May 12, 2022

    @harixxxx

    Is this bug specific to paramiko 2.7.1 or does it exist in previous versions also (e.g: 2.4.1)?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions