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
asyncore.file_wrapper should implement __del__ and call close there to prevent resource leaks and behave like socket.socket does. #55662
Comments
asyncore.file_wrapper duplicates file descriptor of given file and closes it in it's close method. But unlike socket.socket class it does not automatically call close when object is garbage collected. Users of regular sockets and asyncore.dispatcher do not experience resource leaks when they forget to call self.close() in handle_close(). But people using file_dispatcher do loose file descriptor every time file_wrapper object is garbage collected without calling self.close() first. |
Adding a patch that adds an __exit__ function much like the one that socket.socket implements. Passes the test_asyncore & also doesn't raise a resource warning when I explicitly comment out some close() calls on file wrapper objects in the test. |
Adding __exit__ will not make asyncore.file_wrapper close file descriptor when garbage collected. Here is clone of socket.py solution for the same problem. def close(self):
if self.fd:
os.close(self.fd)
self.fd = None # or maybe self.fd = 0 will be better
def __del__(self):
try:
self.close()
except:
# close() may fail if __init__ didn't complete
pass |
New changeset ae12a926e680 by Victor Stinner in branch '3.4': |
I fixed the issue in Python 3.4 and 3.5, thanks for the report. In Python 3.4+, it's safe to add a destructor (del method): even if the object is part of a reference cycle, it will be destroyed. It's not the case in Python 2.7. I prefer to leave Python 2.7 unchanged to limit the risk of regression. |
New changeset 7c9335d97628 by Victor Stinner in branch 'default': |
New changeset 379aad232000 by Victor Stinner in branch '3.4': New changeset 0ced2d2325fb by Victor Stinner in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: