signal_handling.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import signal
  2. import sys
  3. from typing import Any, Callable, List, Union
  4. from ray_release.logger import logger
  5. _handling_setup = False
  6. _handler_functions: List[Callable[[int, Any], None]] = []
  7. _signals_to_handle = {
  8. sig
  9. for sig in (
  10. signal.SIGTERM,
  11. signal.SIGINT,
  12. signal.SIGQUIT,
  13. signal.SIGABRT,
  14. )
  15. if hasattr(signal, sig.name)
  16. }
  17. _original_handlers = {sig: signal.getsignal(sig) for sig in _signals_to_handle}
  18. def _terminate_handler(signum=None, frame=None):
  19. logger.info(f"Caught signal {signal.Signals(signum)}, using custom handling...")
  20. for fn in _handler_functions:
  21. fn(signum, frame)
  22. if signum is not None:
  23. logger.info(f"Exiting with return code {signum}.")
  24. sys.exit(signum)
  25. def register_handler(fn: Callable[[int, Any], None]):
  26. """Register a function to be used as a signal handler.
  27. The function will be placed on top of the stack."""
  28. assert _handling_setup
  29. _handler_functions.insert(0, fn)
  30. def unregister_handler(fn_or_index: Union[int, Callable[[int, Any], None]]):
  31. """Unregister a function by reference or index."""
  32. assert _handling_setup
  33. if isinstance(fn_or_index, int):
  34. _handler_functions.pop(fn_or_index)
  35. else:
  36. _handler_functions.remove(fn_or_index)
  37. def setup_signal_handling():
  38. """Setup custom signal handling.
  39. Will run all functions registered with ``register_handler`` in order from
  40. the most recently registered one.
  41. """
  42. global _handling_setup
  43. if not _handling_setup:
  44. for sig in _signals_to_handle:
  45. signal.signal(sig, _terminate_handler)
  46. _handling_setup = True
  47. def reset_signal_handling():
  48. """Reset custom signal handling back to default handlers."""
  49. global _handling_setup
  50. if _handling_setup:
  51. for sig, handler in _original_handlers.items():
  52. signal.signal(sig, handler)
  53. _handling_setup = False