Author icegood
Recipients icegood
Date 2020-10-18.23:00:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1603062052.42.0.758014022395.issue42076@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-10-18 23:00:52icegoodsetrecipients: + icegood
2020-10-18 23:00:52icegoodsetmessageid: <1603062052.42.0.758014022395.issue42076@roundup.psfhosted.org>
2020-10-18 23:00:52icegoodlinkissue42076 messages
2020-10-18 23:00:51icegoodcreate