Skip to content

Commit

Permalink
add process watch thread to clean up process create by standalone
Browse files Browse the repository at this point in the history
Signed-off-by: weiwee <wbwmat@gmail.com>
  • Loading branch information
sagewe committed Jul 21, 2023
1 parent 0221c70 commit 1644be7
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion python/fate/arch/_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import os
import pickle as c_pickle
import shutil
import signal
import threading
import time
import uuid
from collections.abc import Iterable
Expand Down Expand Up @@ -60,6 +62,31 @@
LOGGER.debug(f"env STANDALONE_DATA_PATH is not set, using {_data_dir} as data dir")


def _watch_thread_react_to_parent_die(ppid):
"""
this function is used to watch parent process, if parent process is dead, then kill self
the trick is to use os.kill(ppid, 0) to check if parent process is alive periodically
and if parent process is dead, then kill self
Note: this trick is modified from the answer by aaron: https://stackoverflow.com/a/71369760/14697733
Args:
ppid: parent process id
"""
pid = os.getpid()

def f():
while True:
try:
os.kill(ppid, 0)
except OSError:
os.kill(pid, signal.SIGTERM)
time.sleep(1)

thread = threading.Thread(target=f, daemon=True)
thread.start()


# noinspection PyPep8Naming
class Table(object):
def __init__(
Expand Down Expand Up @@ -359,7 +386,9 @@ def delete(self, k):
class Session(object):
def __init__(self, session_id, max_workers=None):
self.session_id = session_id
self._pool = Executor(max_workers=max_workers)
self._pool = Executor(
max_workers=max_workers, initializer=_watch_thread_react_to_parent_die, initargs=(os.getpid(),)
)

def __getstate__(self):
# session won't be pickled
Expand Down

0 comments on commit 1644be7

Please sign in to comment.