Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unclosed socket on constructor (connection) failure #22

Closed
wants to merge 1 commit into from

Conversation

vtermanis
Copy link

Creating a Connection instance (with use_pure=True) and e.g. specifying an invalid password causes a ResourceWarning (if running python 3 with -b flag) as the underlying socket is not closed in such a case. This patch addresses this problem.

@mysql-oca-bot
Copy link

Hi, thank you for your contribution. Please confirm this code is submitted under the terms of the OCA (Oracle's Contribution Agreement) you have previously signed by cutting and pasting the following text as a comment:
"I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it."
Thanks

@vtermanis
Copy link
Author

I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

@mysql-oca-bot
Copy link

Hi, thank you for your contribution. Your code has been assigned to an internal queue. Please follow
bug http://bugs.mysql.com/bug.php?id=82324 for updates.
Thanks

@vtermanis vtermanis deleted the conn_leak_fix branch July 25, 2016 15:02
@nmariz
Copy link
Contributor

nmariz commented Jun 30, 2017

Hi Vilnis,
Can you provide more details about your setup? I've tested with Python 3.5 and MySQL 5.7.18 and I could not replicate the issue.

@vtermanis
Copy link
Author

vtermanis commented Jun 30, 2017

Testing against 10141444881c7ae583 (current head of master branch) I can reproduce the issue with the following code (where either the user is unknown or password is invalid):

from contextlib import closing
from mysql.connector import connect

def main():
    with closing(connect(
        host='localhost',
        user='unkown',
        password='invalid',
        use_pure=True
    )) as conn:
        # Doesn't get here obviously due to "Access denied"
        pass


if __name__ == '__main__':
    main()

.. and then running it using (I tested with Python v3.5.2 & MySQL 5.7.18 under Ubuntu):
python3 -Wall -b example.py

You should see below the access denied exception something like:

sys:1: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 36970), raddr=('127.0.0.1', 3306)>

@nmariz
Copy link
Contributor

nmariz commented Jun 30, 2017

Thanks Vilnis, I just verified it using Python 3.5.2.
Python 3.5.0 does not emit this warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants