(fd)
Run resource tracker.
Run resource tracker.
Run resource tracker .
def main(fd):
    '''Run resource tracker.'''
    # protect the process from ^C and "killall python" etc
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, signal.SIG_IGN)
    if _HAVE_SIGMASK:
        signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS)

    for f in (sys.stdin, sys.stdout):
        try:
            f.close()
        except Exception:
            pass

    cache = {rtype: set() for rtype in _CLEANUP_FUNCS.keys()}
    try:
        # keep track of registered/unregistered resources
        with open(fd, 'rb') as f:
            for line in f:
                try:
                    cmd, name, rtype = line.strip().decode('ascii').split(':')
                    cleanup_func = _CLEANUP_FUNCS.get(rtype, None)
                    if cleanup_func is None:
                        raise ValueError(
                            f'Cannot register {name} for automatic cleanup: '
                            f'unknown resource type {rtype}')

                    if cmd == 'REGISTER':
                        cache[rtype].add(name)
                    elif cmd == 'UNREGISTER':
                        cache[rtype].remove(name)
                    elif cmd == 'PROBE':
                        pass
                    else:
                        raise RuntimeError('unrecognized command %r' % cmd)
                except Exception:
                    try:
                        sys.excepthook(*sys.exc_info())
                    except:
                        pass
    finally:
        # all processes have terminated; cleanup any remaining resources
        for rtype, rtype_cache in cache.items():
            if rtype_cache:
                try:
                    warnings.warn('resource_tracker: There appear to be %d '
                                  'leaked %s objects to clean up at shutdown' %
                                  (len(rtype_cache), rtype))
                except Exception:
                    pass
            for name in rtype_cache:
                # For some reason the process which created and registered this
                # resource has failed to unregister it. Presumably it has
                # died.  We therefore unlink it.
                try:
                    try:
                        _CLEANUP_FUNCS[rtype](name)
                    except Exception as e:
                        warnings.warn('resource_tracker: %r: %s' % (name, e))
                finally:
                    pass
def main ( fd ) : # protect the process from ^C and "killall python" etc signal . signal ( signal . SIGINT , signal . SIG_IGN ) signal . signal ( signal . SIGTERM , signal . SIG_IGN ) if _HAVE_SIGMASK : signal . pthread_sigmask ( signal . SIG_UNBLOCK , _IGNORED_SIGNALS ) for f in ( sys . stdin , sys . stdout ) : try : f . close ( ) except Exception : pass cache = { rtype : set ( ) for rtype in _CLEANUP_FUNCS . keys ( ) } try : # keep track of registered/unregistered resources with open ( fd , 'rb' ) as f : for line in f : try : cmd , name , rtype = line . strip ( ) . decode ( 'ascii' ) . split ( ':' ) cleanup_func = _CLEANUP_FUNCS . get ( rtype , None ) if cleanup_func is None : raise ValueError ( f'Cannot register {name} for automatic cleanup: ' f'unknown resource type {rtype}' ) if cmd == 'REGISTER' : cache [ rtype ] . add ( name ) elif cmd == 'UNREGISTER' : cache [ rtype ] . remove ( name ) elif cmd == 'PROBE' : pass else : raise RuntimeError ( 'unrecognized command %r' % cmd ) except Exception : try : sys . excepthook ( * sys . exc_info ( ) ) except : pass finally : # all processes have terminated; cleanup any remaining resources for rtype , rtype_cache in cache . items ( ) : if rtype_cache : try : warnings . warn ( 'resource_tracker: There appear to be %d ' 'leaked %s objects to clean up at shutdown' % ( len ( rtype_cache ) , rtype ) ) except Exception : pass for name in rtype_cache : # For some reason the process which created and registered this # resource has failed to unregister it. Presumably it has # died.  We therefore unlink it. try : try : _CLEANUP_FUNCS [ rtype ] ( name ) except Exception as e : warnings . warn ( 'resource_tracker: %r: %s' % ( name , e ) ) finally : pass