[issue42076] urllib ResourceWarning in case of usage of FTP

Serhiy Ivanov report at bugs.python.org
Sun Oct 18 19:00:52 EDT 2020


New submission from Serhiy Ivanov <icegood1980 at gmail.com>:

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 <report at bugs.python.org>
<https://bugs.python.org/issue42076>
_______________________________________


More information about the Python-bugs-list mailing list