Issue 26620: Fix ResourceWarning warnings in test_urllib2_localnet (original) (raw)
Attached patch fixes 3 ResourceWarning warnings in Lib/test/test_urllib2_localnet.py.
See also issue #26612 (test_ssl).
Example of warning logged by "./python -X tracemalloc=25 -m test -v test_urllib2_localnet ":
/home/haypo/prog/python/default/Lib/test/support/init.py:1444: ResourceWarning: unclosed <socket.socket [closed] fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6> gc.collect() Object allocated at (most recent call first): File "/home/haypo/prog/python/default/Lib/socket.py", lineno 697 sock = socket(af, socktype, proto) File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 898 (self.host,self.port), self.timeout, self.source_address) File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 926 self.connect() File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 983 self.send(msg) File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 1151 self._send_output(message_body) File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 1200 self.endheaders(body) File "/home/haypo/prog/python/default/Lib/http/client.py", lineno 1155 self._send_request(method, url, body, headers) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 1303 h.request(req.get_method(), req.selector, req.data, headers) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 1331 return self.do_open(http.client.HTTPConnection, req) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 503 result = func(*args) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 543 '_open', req) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 525 response = self._open(req, data) File "/home/haypo/prog/python/default/Lib/urllib/request.py", lineno 222 return opener.open(url, data, timeout) File "/home/haypo/prog/python/default/Lib/test/test_urllib2_localnet.py", lineno 595 urllib.request.urlopen(req) (...)
Martin wrote:
This patch looks okay to me. I left one review suggestion.
Thanks for the review, I changed this code.
While testing one more time my patch with CTRL+c, I noticed that sometimes the servers are not stopped properly and os.environ is not restored.
I made some extra changes to try to restore os.environ and try to close the server in more cases.
It's not perfect. Since unittest.TestCase.doCleanups() doesn't handle KeyboardInterrupt, there is no warranty that cleanup functions are always executed.
Focussing on test_sending_headers(), the ResourceWarning seems to be only shown since revision 46329eec5515 (Issue 26590). In simpler cases, the warning would be bypassed due to Issue 19829. But in this cases it seems there is a garbage cycle which may be involved in the new warning.
It's possible that Python 3.5 failed to log ResourceWarning in some cases, Python 3.6 should be better on this part.