tlocke / pg8000

A Pure-Python PostgreSQL Driver
BSD 3-Clause "New" or "Revised" License
515 stars 46 forks source link

It's possible for a socket to be left open #100

Closed kurtmckee closed 2 years ago

kurtmckee commented 2 years ago

After configuring sqlalchemy-utils' unit tests to escalate warnings to errors, it was revealed that the root cause of a ResourceWarning was a socket that is opened in pg8000.core.CoreConnection.__init__() but is not closed when an error is detected:

https://github.com/tlocke/pg8000/blob/121f754c5d423ea4785c8ed35ee72f5586a56a26/pg8000/core.py#L341-L342

Python's garbage collector will eventually collect the socket object and throw a ResourceWarning. I have tested and found that calling self.close() immediately prior to raising self.error will resolve this problem.

This is related to kvesteri/sqlalchemy-utils#590.

tlocke commented 2 years ago

Thanks for this bug report and patch @kurtmckee. I've used your PR and done a new release (1.24.2). I hadn't thought about the fact that if there's an exception in a constructor, there's no handle on the object and so resources associated with it can't be freed.

kurtmckee commented 2 years ago

You're welcome! Thanks for your work on pg8000!