Bug #88375 Unclosed socket on connection failure
Submitted: 6 Nov 2017 19:35 Modified: 9 May 2022 22:22
Reporter: Géry Ogam Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:2.1.6, 2.2.3 OS:Any
Assigned to: CPU Architecture:Any
Tags: socket, warning

[6 Nov 2017 19:35] Géry Ogam
Description:
With [Python 3.6.3](https://www.python.org/downloads/) and both [MySQL Connector 2.1.6](https://pypi.python.org/pypi/mysql-connector/2.1.6) and [2.2.3](https://stackoverflow.com/a/45450183/2326961) packages, connecting to an invalid host 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.

How to repeat:
Input:

    python3 -Wall <<EOF
    import mysql.connector
    with mysql.connector.connect(host = "0.0.0.0") as session: pass
    EOF

Output:

    Traceback (most recent call last):
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/network.py", line 509, in open_connection
        self.sock.connect(sockaddr)
    ConnectionRefusedError: [Errno 111] Connection refused

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/__init__.py", line 179, in connect
        return MySQLConnection(*args, **kwargs)
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/connection.py", line 95, in __init__
        self.connect(**kwargs)
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 722, in connect
        self._open_connection()
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/connection.py", line 208, in _open_connection
        self._socket.open_connection()
      File "/home/master/.local/lib/python3.6/site-packages/mysql/connector/network.py", line 512, in open_connection
        errno=2003, values=(self.get_address(), _strioerror(err)))
    mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '0.0.0.0:3306' (111 Connection refused)
    sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 52836)>

It looks very similar to [this](https://bugs.mysql.com/bug.php?id=82324) MySQL Connector bug which has been [resolved](https://dev.mysql.com/doc/relnotes/connector-python/en/news-2-1-7.html) in MySQL Connector 2.1.7 released on 8 August 2017. The two versions that I have tested (2.1.6 released on 18 April 2017 and 2.2.3 released on 22 March 2017) are prior to the 2.1.7 version so I patched them myself with the [patch](https://bugs.mysql.com/file.php?id=24258&bug_id=82324) given by Vilnis Termanis to see if the bugs are related, but the patched versions still give me the same warning.
[7 Nov 2017 11:17] Chiranjeevi Battula
Hello Géry Ogam,

Thank you for the bug report.
Verified as described with the help of dev's.

Thanks,
Chiranjeevi.
[4 May 2022 14:11] Nuno Mariz
Posted by developer:
 
Fixed by BUG#24342757 in 2.1.7 release. Thank you for your report.
[9 May 2022 22:22] Philip Olson
This was fixed via MySQL Bug #82324 -- setting the status to closed. Thanks for submitting bugs!