New submission from Serhiy Ivanov <[email protected]>:
In case when FTP url is successfully connected via default FTPHandler in
FTPHandler.connect_ftp then release of this stuff becomes total responsibility
of user i.e. socket remains open, which leads to ResourceWarning in the garbage
collector, in case if the user does nothing.
Something like this solves issue in user area:
class SafeFTPHandler(urllib.request.FTPHandler):
ftp_object = None
def __init__(self):
super().__init__()
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
self.ftp_object = super().connect_ftp(user, passwd, host, port,
dirs, timeout)
return self.ftp_object
def ftp_response(self, req, response):
self._close_ftp()
return response
def ftp_open(self, req):
try:
return super().ftp_open(req)
except:
self._close_ftp()
raise
def _close_ftp(self):
if self.ftp_object:
self.ftp_object.close()
and further usage in OpenerDirector...
In general case FTPHandler should be able to close immediately (in case if it
raises as well) while re-using of the socket should be implemented only within
CacheFTPHandler
----------
components: Library (Lib)
messages: 378911
nosy: icegood
priority: normal
severity: normal
status: open
title: urllib ResourceWarning in case of usage of FTP
type: resource usage
versions: Python 3.10, Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42076>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com