Skip to content

Commit 3d3894a

Browse files
committed
Fix muted exceptions in fm_dispatch due to self SIGKILL
This commit fixes the issue where fm_dispatch would call SIGKILL on itself before reraising the exception that caused it to fail.
1 parent 9c06e6d commit 3d3894a

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/_ert/forward_model_runner/fm_dispatch.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ def _stop_reporters(
144144
reporter.stop(exited_event=exited_event)
145145

146146

147-
def sigterm_handler(_signo, _stack_frame):
148-
signal.signal(signal.SIGTERM, signal.SIG_DFL)
149-
os.kill(0, signal.SIGTERM)
150-
151-
152147
def fm_dispatch(args):
153148
parser = argparse.ArgumentParser(
154149
description=(
@@ -207,13 +202,14 @@ def sigterm_handler(_signo, _stack_frame):
207202

208203
def main():
209204
os.nice(19)
210-
signal.signal(signal.SIGTERM, sigterm_handler)
211205
try:
212206
fm_dispatch(sys.argv)
213-
except Exception as e:
207+
except Exception as exc:
208+
print(f"fm_dispatch failed with {exc=}")
214209
pgid = os.getpgid(os.getpid())
215-
os.killpg(pgid, signal.SIGTERM)
216-
raise e
210+
os.killpg(
211+
pgid, signal.SIGTERM
212+
) # This will trigger the sigterm_handler, which shuts down the reporters and SIGKILLS any remaining processes.
217213

218214

219215
if __name__ == "__main__":

tests/ert/unit_tests/forward_model_runner/test_fm_dispatch.py

+17
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,20 @@ async def wait_for_msg(msg_type):
484484
event_from_json(zmq_server.messages[-1]).error_msg
485485
== FORWARD_MODEL_TERMINATED_MSG
486486
)
487+
488+
489+
async def test_fm_dispatch_main_signals_sigterm_on_exception(capsys):
490+
def mock_fm_dispatch_raises(*args):
491+
raise RuntimeError("forward model critical error")
492+
493+
with (
494+
patch("_ert.forward_model_runner.fm_dispatch.os.killpg") as mock_killpg,
495+
patch("_ert.forward_model_runner.fm_dispatch.os.getpgid") as mock_getpgid,
496+
patch("_ert.forward_model_runner.fm_dispatch.fm_dispatch") as mock_fm_dispatch,
497+
):
498+
mock_getpgid.return_value = 17
499+
mock_fm_dispatch.side_effect = mock_fm_dispatch_raises
500+
_ert.forward_model_runner.fm_dispatch.main()
501+
assert "forward model critical error" in capsys.readouterr().out
502+
503+
mock_killpg.assert_called_with(17, signal.SIGTERM)

0 commit comments

Comments
 (0)