sassanp / pynetinfo

Python module for retrieving network information
http://pypi.python.org/pypi/pynetinfo/
GNU General Public License v2.0
11 stars 10 forks source link

Socket Leak #1

Open ZachGoldberg opened 12 years ago

ZachGoldberg commented 12 years ago

Everytime I call netinfo.list_active_devs() approximately 15 sockets are leaked. I call list_active_devs once every couple of seconds and therefore this causes my application to crash after a few minutes.

    devs = netinfo.list_active_devs()

Exception: (24, 'Too many open files') Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 59, in apport_excepthook ImportError: No module named fileutils

ZachGoldberg commented 12 years ago

Correction, other netinfo calls also leak sockets. get_broadcast() being the largest offender I've identified thus far.

ZachGoldberg commented 12 years ago

I wrote the following decorator for use around functions that call netinfo (and preferably only netinfo) to cleanup the leaked FDs.

def ensure_no_leaked_fds(function):
    def wrapper(*args, **kwargs):
        original_fds = [int(i) for i in os.listdir(
                "/proc/%s/fd/" % os.getpid()) if int(i) > 10]
        retval = function(*args, **kwargs)  
        post_fds = [int(i) for i in os.listdir(
                "/proc/%s/fd/" % os.getpid()) if int(i) > 10]
        new_fds = set(post_fds) - set(original_fds)
        for fd in new_fds:
            try:
                f = os.fdopen(fd)
                f.close()
            except:
                pass
        return retval
    return wrapper
sassanp commented 12 years ago

Hi Zach,

Thanks for your report, sorry it has taken so long for me to attend to it. I will hopefully have some time to deal with the issue over the next couple of days.

Thanks Sassan