Skip to content

SSH connection still alive on channel.close #1475

Open

Description

@yaroslavvb

The following doesn't seem to close connection on the server side.

    chan: paramiko.Channel = transport.open_session()
    chan.exec_command(long_running_command)
    chan.close()

IE, repeating this will eventually end up with paramiko.ssh_exception.ChannelException: (1, 'Administratively prohibited') as the server rejects additional SSH connections.

Looking at debug log, the number of channels stays constant while number of active SSH connections grows

 <paramiko.Transport at 0x151d4320 (cipher aes128-ctr, 128 bits) (active; 2 open channel(s))>]

My workaround has been to manually kill the command in addition to channel close
chan.exec_command('killall long_running_command')

paramiko version '2.4.2'

Activity

  1. mhaoli commented on Jul 19, 2022

    @mhaoli

    I'm also facing this problem.

    I used paramiko to ssh from a linux device to a Windows device, and ran a long running command. After calling the channel.close, the chanel was still alive. I checked that the channel object exists in ssh_client.get_transport()._channels._map and the pid of the shell was still alive on Windows device.

    Also it is interesting to note that after calling channel.close, the ssh connection is alive, the shell process is alive, and the long running process is killed.

    My paramiko version: 2.7.1

  2. azertyfun commented on Jun 21, 2023

    @azertyfun

    Just ran into this issue, but in my case it's because I had the following workaround for an unrelated issue:

    try:
        self.connection.connect(...)
    except paramiko.ssh_exception.AuthenticationException:
        self.connection.connect(..., disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})

    Turns out I had to close that connection even if it had failed:

    try:
        self.connection.connect(...)
    except paramiko.ssh_exception.AuthenticationException:
        self.connection.close()
        self.connection.connect(..., disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})
  3. jun66j5 commented on Jun 21, 2023

    @jun66j5
    Contributor

    I don't think Channel class should do job-control, because paramiko intents that direct use of paramiko itself is only intended for users who need advanced/low-level primitives.

    Instead, we could send a signal to terminate the command using signals feature (needed apply #1535):

    with transport.open_session() as channel:
        channel.exec_command('sleep 86400')  # long running command
        try:
            # Waiting for termination of the command...
        finally:
            channel.send_signal('TERM')
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions