Skip to content

Unclosed socket on connection failure #1126

Closed

Description

@geryogam

With Python 3.6.3 and Paramiko 2.4.0, connecting with an invalid host or port causes a ResourceWarning (noticeable when the interpreter warnings are activated with the -Wall flag) as the underlying socket is not closed in such a case:

Input (example with an invalid port):

$ python3 -Wall <<EOF
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
with client.connect(hostname="localhost", port=0): pass
EOF

Output:

/home/master/.local/lib/python3.6/site-packages/paramiko/client.py:332: ResourceWarning: unclosed <socket.socket fd=4, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=0, laddr=('::', 49626, 0, 0)>
sock = socket.socket(af, socket.SOCK_STREAM)
/usr/lib/python3/dist-packages/apport/report.py:13: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import fnmatch, glob, traceback, errno, sys, atexit, locale, imp
Traceback (most recent call last):
File "", line 4, in
File "/home/master/.local/lib/python3.6/site-packages/paramiko/client.py", line 357, in connect
raise NoValidConnectionsError(errors)
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 0 on 127.0.0.1 or ::1
sys:1: ResourceWarning: unclosed <socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 58766)>

Activity

  1. pkittenis commented on Nov 30, 2017

    @pkittenis

    Not just on invalid host/port, sockets are not closed in general. #1127

  2. maludwig commented on Dec 31, 2018

    @maludwig

    I just spent a bunch of time hunting down this error, sad that it's in Paramiko itself.

  3. andyleejordan commented on Oct 12, 2020

    @andyleejordan

    As of today, this is still the case.

  4. added a commit that references this issue on Mar 4, 2021
    fa470df
  5. bitprophet commented on Jun 3, 2022

    @bitprophet
    Member

    Rolling into #1822

  6. added a commit that references this issue on Jun 3, 2022
    7b8bb92
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