INNER CODE UNIT · Python
_exit_when_parent_closes
pasteurlabs/tesseract-core · tesseract_core/runtime/cli.py:525
def _exit_when_parent_closes(fd: int) -> None:
"""Shut down orphaned Tesseract once the far end of `fd` is closed.
Parent processes are not guaranteed to clean up if ended prematurely.
However, reading a pipe the parent is meant to hold open is guaranteed to
return EOF if the parent is no longer live.
"""
def watch() -> None:
try:
while os.read(fd, 1):
# Nothing is expected to be written; a stray byte is not EOF.
pass
except OSError:
pass
# Ask uvicorn to stop rather than dying where we stand, so workers are
# shut down and the port is released.
os.kill(os.getpid(), signal.SIGTERM)