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
FD leak in urllib2 #47316
Comments
In urllib2.AbstractHTTPHandler.do_open, the following like creates a r.recv = r.read [r.read is a bound method, so it contains a reference to 'r'. Therefore, If the GC is disabled or doesn't run often, this creates a FD leak. How to reproduce: import gc
import urllib2
u = urllib2.urlopen("http://google.com")
s = [ u.fp._sock.fp._sock ]
u.close()
del u
print gc.get_referrers(s[0])
[<socket._fileobject object at 0xf7d42c34>, [<socket object, fd=4,
family=2, type=1, protocol=6>]] I would expect that only one reference to the socket would exist (the I can reproduce with 2.4; the problems seems to still exist in SVN HEAD. |
Since the socket object is added to a list, a reference to the object On the other hand, it should also be noted that in close method, the |
The list is not the problem. The problem is the other reference, from Also note that the workaround (u.fp.recv = None) removes the second This is fine (at least in CPython), because the socket is destroyed when the |
So if I add a: class _WrapForRecv:
def __init__(self, obj):
self.__obj = obj
def __getattr__(self, name):
if name == "recv": name = "read"
return getattr(self.__obj, name) ...and then change: r.recv = r.read ...into: r = _WrapForRecv(r) ...it stops the leak, and afaics nothing bad happens. |
Has (non-unittest) test and proposed (non-diff) patch inline. |
I can't reproduce in python 2.5.4, 2.6.2, or 2.7 trunk (though I can Quick bisection suggests that it was fixed in r53511 while solving Suggest closing as fixed. |
not reproducable in head as stated. |
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: