Closed kurtmckee closed 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.
You're welcome! Thanks for your work on pg8000!
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 raisingself.error
will resolve this problem.This is related to kvesteri/sqlalchemy-utils#590.