Skip to content
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 #62344

Closed
ClaudioFreire mannequin opened this issue Jun 5, 2013 · 5 comments
Closed

FD leak in urllib2 #62344

ClaudioFreire mannequin opened this issue Jun 5, 2013 · 5 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ClaudioFreire
Copy link
Mannequin

ClaudioFreire mannequin commented Jun 5, 2013

BPO 18144
Nosy @pitrou, @vadmium, @serhiy-storchaka
Superseder
  • bpo-12692: test_urllib2net is triggering a ResourceWarning
  • Files
  • bogus.py: Tornado server associated with the snippet in the description
  • 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:

    assignee = None
    closed_at = <Date 2014-07-16.19:30:16.268>
    created_at = <Date 2013-06-05.19:10:23.594>
    labels = ['type-bug', 'library']
    title = 'FD leak in urllib2'
    updated_at = <Date 2014-07-16.19:30:16.268>
    user = 'https://bugs.python.org/ClaudioFreire'

    bugs.python.org fields:

    activity = <Date 2014-07-16.19:30:16.268>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-07-16.19:30:16.268>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2013-06-05.19:10:23.594>
    creator = 'Claudio.Freire'
    dependencies = []
    files = ['30477']
    hgrepos = []
    issue_num = 18144
    keywords = []
    message_count = 5.0
    messages = ['190687', '205370', '214001', '214021', '214028']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'javawizard', 'martin.panter', 'serhiy.storchaka', 'Claudio.Freire']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '12692'
    type = 'behavior'
    url = 'https://bugs.python.org/issue18144'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

    @ClaudioFreire
    Copy link
    Mannequin Author

    ClaudioFreire mannequin commented Jun 5, 2013

    While other issues already exist about this problem, this particular case is unlike other issues, and I didn't think it a good idea to merge with those.

    Under some very specific circumstances (sending a POST request with more data than an unknown threshold), at least one socket remains unclosed after calling close() on urllib2.urlopen's returned file object.

    While I marked the only versions I could confirm exhibit the issue, I believe this is an issue on all versions.

    This started in pypy[0], although it applies to CPython as well (albeit the reference counting GC is less likely to delay closing of the FD as much as in pypy).

    I'm attaching the same server used to trigger this issue in pypy, works the same with CPython.

    To trigger the leak, open an interpreter and do this (copypaste from pypy, CPython does not cause the leak because decref immediately closes the leak, but it will issue a wraning if ran with -Wall). See pypy's issue tracker[0] for detilas.

    >>>> import os, urllib2
    >>>> req = """{"imp": [{"h": 50, "battr": ["9", "10", "12"], "api": 3, "w": 320,
    "instl": 0, "impid": "5d6dedf3-17bb-11e2-b5c0-1040f38b83e0"}]""" * 10
    >>>> r = urllib2.Request("http://localhost:8000/bogus?src=1", req)
    >>>> u = urllib2.urlopen(r)
    >>>> v = u.read()
    >>>> u.close()
    >>>> os.system("ls -alh /proc/%d/fd/*" % os.getpid())
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/0 -> /dev/pts/5
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/1 -> /dev/pts/5
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/2 -> /dev/pts/5
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/3 ->
    socket:[2086998]
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/5 -> /dev/pts/5
    lrwx------ 1 claudiofreire users 64 Jun  4 15:08 /proc/26203/fd/6 -> /dev/pts/5
    0
    >>>> 

    [0] https://bugs.pypy.org/issue867

    @ClaudioFreire ClaudioFreire mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 5, 2013
    @vadmium
    Copy link
    Member

    vadmium commented Dec 6, 2013

    Confirmed that this happens when the server sends a chunked response, or sends a Content-Length header, but not when the server just sends “Connection: close”. So this looks like the same as bpo-19524, and my patch for that seems to fix the issue here.

    Python 3 version of the demo code:

    import os, urllib.request
    data = b"""{"imp": [{"h": 50, "battr": ["9", "10", "12"], "api": 3, "w": 320,
    "instl": 0, "impid": "5d6dedf3-17bb-11e2-b5c0-1040f38b83e0"}]""" * 10
    req = urllib.request.Request("http://localhost:8000/bogus?src=1", data)
    resp = urllib.request.urlopen(req)
    v = resp.read()
    resp.close()
    os.system("ls -alh /proc/%d/fd/*" % os.getpid())

    @ClaudioFreire
    Copy link
    Mannequin Author

    ClaudioFreire mannequin commented Mar 18, 2014

    I can confirm the issue is in urllib's open: it fails to close() the HTTP connection, leaving it to the GC to do it.

    If addinfourl (and friends) is altered to carry a reference to the HTTP connection and close it on close(), the leak is fixed.

    I have a patch but it is incomplete (just a POC), it only handles the common case I use.

    @vadmium
    Copy link
    Member

    vadmium commented Mar 18, 2014

    Does the fix for bpo-12692 work for you? Namely this revision <http://hg.python.org/cpython/rev/92656b5df2f2\>. It was backported to C Python 3.3.4 as I understand.

    @ClaudioFreire
    Copy link
    Mannequin Author

    ClaudioFreire mannequin commented Mar 18, 2014

    Yes, seems it does.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants