Run resource tracker.
(fd)
| 340 | |
| 341 | |
| 342 | def main(fd): |
| 343 | '''Run resource tracker.''' |
| 344 | # protect the process from ^C and "killall python" etc |
| 345 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 346 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
| 347 | if _HAVE_SIGMASK: |
| 348 | signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS) |
| 349 | |
| 350 | for f in (sys.stdin, sys.stdout): |
| 351 | try: |
| 352 | f.close() |
| 353 | except Exception: |
| 354 | pass |
| 355 | |
| 356 | cache = {rtype: set() for rtype in _CLEANUP_FUNCS.keys()} |
| 357 | exit_code = 0 |
| 358 | |
| 359 | try: |
| 360 | # keep track of registered/unregistered resources |
| 361 | with open(fd, 'rb') as f: |
| 362 | for line in f: |
| 363 | try: |
| 364 | cmd, rtype, name = _decode_message(line) |
| 365 | cleanup_func = _CLEANUP_FUNCS.get(rtype, None) |
| 366 | if cleanup_func is None: |
| 367 | raise ValueError( |
| 368 | f'Cannot register {name} for automatic cleanup: ' |
| 369 | f'unknown resource type {rtype}') |
| 370 | |
| 371 | if cmd == 'REGISTER': |
| 372 | cache[rtype].add(name) |
| 373 | elif cmd == 'UNREGISTER': |
| 374 | cache[rtype].remove(name) |
| 375 | elif cmd == 'PROBE': |
| 376 | pass |
| 377 | else: |
| 378 | raise RuntimeError('unrecognized command %r' % cmd) |
| 379 | except Exception: |
| 380 | exit_code = 3 |
| 381 | try: |
| 382 | sys.excepthook(*sys.exc_info()) |
| 383 | except: |
| 384 | pass |
| 385 | finally: |
| 386 | # all processes have terminated; cleanup any remaining resources |
| 387 | for rtype, rtype_cache in cache.items(): |
| 388 | if rtype_cache: |
| 389 | try: |
| 390 | exit_code = 1 |
| 391 | if rtype == 'dummy': |
| 392 | # The test 'dummy' resource is expected to leak. |
| 393 | # We skip the warning (and *only* the warning) for it. |
| 394 | pass |
| 395 | else: |
| 396 | warnings.warn( |
| 397 | f'resource_tracker: There appear to be ' |
| 398 | f'{len(rtype_cache)} leaked {rtype} objects to ' |
| 399 | f'clean up at shutdown: {rtype_cache}' |
nothing calls this directly
no test coverage detected
searching dependent graphs…