Skip to content

Commit

Permalink
Remove signal listener as it made the executor unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Aug 1, 2024
1 parent 42da732 commit 55d92c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,21 @@ def _start_pool_worker(fn, predictions, max_workers, results, errors):

def _pool_worker(*, fn, predictions, max_workers, results, errors):
terminating_child_processes = False
executor_shutting_down = False

with ProcessPoolExecutor(max_workers=max_workers) as executor:
try:

def handle_error(error, prediction="Unknown"):
def handle_error(error, prediction):
nonlocal terminating_child_processes
if terminating_child_processes:
return

nonlocal executor_shutting_down
if not executor_shutting_down:
executor_shutting_down = True
executor.shutdown(wait=False, cancel_futures=True)
executor.shutdown(wait=False, cancel_futures=True)
errors.append((prediction, error))

terminating_child_processes = True
_terminate_child_processes()

def sigchld_handler(*_, **__):
if not terminating_child_processes:
handle_error(
RuntimeError(
"Child process was terminated unexpectedly"
)
)

# Register the SIGCHLD handler
signal.signal(signal.SIGCHLD, sigchld_handler)

# Submit the processing tasks of the predictions
futures = [
executor.submit(fn, prediction) for prediction in predictions
Expand Down
30 changes: 11 additions & 19 deletions tests/test_evaluate_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Some of the test below, if things go wrong, can potentially deadlock.
# So we set a maximum runtime
pytestmark = pytest.mark.timeout(5)
pytestmark = pytest.mark.timeout(4)


def working_process(p):
Expand Down Expand Up @@ -55,26 +55,18 @@ def forever_process(*_):
time.sleep(1)


def send_signals_to_process(process, signal_to_send, interval):
while True:
try:
os.kill(process.pid, signal_to_send)
except ProcessLookupError:
# Race conditions sometimes have this try and send a signal even though
# the process is already terminated
pass
time.sleep(interval)


def stop_children(process, interval):
while True:
stopped = False
while not stopped:
process = psutil.Process(process.pid)
children = process.children(recursive=True)
for child in children:
try:
child.kill()
except psutil.NoSuchProcess:
pass # Not a problem
if children:
for child in children:
try:
child.kill()
except psutil.NoSuchProcess:
pass # Not a problem
stopped = True
time.sleep(interval)


Expand Down Expand Up @@ -135,6 +127,6 @@ def add_child_terminator(*args, **kwargs):
if child_stopper:
child_stopper.terminate()

assert "Child process was terminated unexpectedly" in str(
assert "A process in the process pool was terminated abruptly" in str(
excinfo.value.error
)

0 comments on commit 55d92c6

Please sign in to comment.