conftest.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import gc
  2. import multiprocessing as mp
  3. from contextlib import suppress
  4. import psutil
  5. import pytest
  6. from hivemind.utils import get_logger
  7. from hivemind.utils.mpfuture import MPFuture
  8. logger = get_logger(__name__)
  9. @pytest.fixture(autouse=True, scope="session")
  10. def cleanup_children():
  11. yield
  12. gc.collect() # Call .__del__() for removed objects
  13. children = psutil.Process().children(recursive=True)
  14. if children:
  15. logger.info(f"Cleaning up {len(children)} leftover child processes")
  16. for child in children:
  17. with suppress(psutil.NoSuchProcess):
  18. child.terminate()
  19. psutil.wait_procs(children, timeout=1)
  20. for child in children:
  21. with suppress(psutil.NoSuchProcess):
  22. child.kill()
  23. # Killing child processes may leave the global locks in MPFuture acquired
  24. # or the global state broken, so we reset them
  25. MPFuture._initialization_lock = mp.Lock()
  26. MPFuture._update_lock = mp.Lock()
  27. MPFuture._active_pid = None # This will force to reset the global state