Bug#518945: Leaks sockets under Python 2.5

Chris Lamb lamby at debian.org
Mon Mar 9 12:20:43 UTC 2009


Package: python-mechanize
Version: 0.1.7b-3

python-mechanize 0.1.7 is a total pain to use under Python 2.5 as it
doesn't clean up after itself without a lot of convincing, leading to
"out of file descriptor" errors in long-running processes, etc.

My testcase is:

    import os, subprocess
    from mechanize import Browser
    subprocess.call(['/bin/ls', '-l', '/proc/%d/fd' % os.getpid()])
    br = Browser()
    br.open('http://google.co.uk/')
    br.close()
    subprocess.call(['/bin/ls', '-l', '/proc/%d/fd' % os.getpid()])

which prints:

    total 0
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 0 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 1 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 2 -> /dev/pts/9
    lr-x------ 1 lamby lamby 64 2009-03-09 12:09 3 -> /proc/12035/fd
    total 0
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 0 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 1 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 2 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 3 -> socket:[44930] <--
    lrwx------ 1 lamby lamby 64 2009-03-09 12:09 4 -> socket:[44942] <--
    lr-x------ 1 lamby lamby 64 2009-03-09 12:09 5 -> pipe:[44943]

Doing a bit of "manual" GC helps:

    import os, gc, subprocess
    from mechanize import Browser
    subprocess.call(['/bin/ls', '-l', '/proc/%d/fd' % os.getpid()])
    br = Browser()
    br.open('http://google.co.uk/')
    br.close()
    del br
    gc.collect()
    subprocess.call(['/bin/ls', '-l', '/proc/%d/fd' % os.getpid()])

    total 0
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 0 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 1 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 2 -> /dev/pts/9
    lr-x------ 1 lamby lamby 64 2009-03-09 12:12 3 -> /proc/12086/fd
    total 0
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 0 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 1 -> /dev/pts/9
    lrwx------ 1 lamby lamby 64 2009-03-09 12:12 2 -> /dev/pts/9
    lr-x------ 1 lamby lamby 64 2009-03-09 12:12 3 -> pipe:[45407]

.. but this is, of course, extremely backwards. As an aside, it is
difficult to reliably reproduce this latter behaviour in my application,
where scoping is non-trivial (but certainly not complicated).

From the upstream changelog:

> 2008-09-13 John J Lee <jjl at pobox.com>
> 	* 0.1.8 release.
> 	* Close sockets.  This only affects Python 2.5 (and later) -
> 	  earlier versions of Python were unaffected.  See
> 	  http://bugs.python.org/issue1627441
[..]

If it helps, the fix was in r58103 in the mechanize SVN.


Regards,

-- 
      ,''`.
     : :'  :     Chris Lamb
     `. `'`      lamby at debian.org
       `-
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
Url : http://lists.alioth.debian.org/pipermail/pkg-zope-developers/attachments/20090309/2ec3ed5e/attachment.pgp 


More information about the pkg-zope-developers mailing list