From 8376b9fa6fdb4636a4ab504150123dddab4b3b80 Mon Sep 17 00:00:00 2001 From: Gavin Ying Date: Sat, 14 Nov 2026 13:29:53 +0800 Subject: [PATCH] fix(modpoll): never wait for the exit event's lock in the signal handler Python runs a signal handler between two bytecodes of the main thread. The main loop spends most of its time in delay_thread(), inside threading.Event.wait(), which holds the event's non-reentrant Lock for a few bytecodes on every wakeup. A SIGINT or SIGTERM landing there called Event.set() from the same thread, which waited for that Lock forever, so modpoll occasionally ignored the signal and had to be killed. This is the timeout seen in test_process_lifecycle and the -9 exit in the MQTT equivalence test. The handler now hands the exit to a short-lived thread, which sets the event as soon as the main thread releases the lock. With 160 real modpoll processes signalled while polling, the previous code hung twice and this one never; the new regression test holds the lock and fails on the old handler. Co-Authored-By: Claude Opus 5.5 --- packages/modpoll/modpoll/main.py | 13 ++++++++++++- packages/modpoll/tests/test_shutdown.py | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/modpoll/modpoll/main.py b/packages/modpoll/modpoll/main.py index e580d2f..1773c88 100644 --- a/packages/modpoll/modpoll/main.py +++ b/packages/modpoll/modpoll/main.py @@ -2,6 +2,7 @@ import logging import signal import sys +import threading import time from datetime import datetime, timezone from pathlib import Path @@ -298,12 +299,22 @@ def _upload(args, report, *, confirm=True): return True -def _signal_handler(signal_number, frame): +def _request_exit(): if logger: logger.info(f"Exiting {sys.argv[0]}") set_threading_event() +def _signal_handler(signal_number, frame): + # Python runs the handler between two bytecodes of the main thread, which may hold + # the exit event's internal (non-reentrant) lock inside delay_thread(); setting the + # event here would then wait for that lock forever. Another thread sets it as soon + # as the main thread lets the lock go. + thread = threading.Thread(target=_request_exit, name="modpoll-exit", daemon=True) + thread.start() + return thread + + def setup_logging(level, format): logging.basicConfig(level=level, format=format) diff --git a/packages/modpoll/tests/test_shutdown.py b/packages/modpoll/tests/test_shutdown.py index 0f36a5f..ffa9272 100644 --- a/packages/modpoll/tests/test_shutdown.py +++ b/packages/modpoll/tests/test_shutdown.py @@ -1,4 +1,5 @@ import signal +import subprocess import sys import pytest @@ -76,6 +77,21 @@ def test_signal_handler_tolerates_uninitialized_logger(monkeypatch): monkeypatch.setattr(main, "logger", None) monkeypatch.setattr(main, "set_threading_event", lambda: called.append(True)) - main._signal_handler(signal.SIGTERM, None) + main._signal_handler(signal.SIGTERM, None).join(timeout=5) assert called == [True] + + +def test_signal_handler_does_not_wait_for_the_exit_event_lock(): + # The signal can arrive while the main thread holds the event's lock in delay_thread(). + script = """ +from modpoll import main, utils +with utils._thread_event._cond: + thread = main._signal_handler(15, None) +thread.join(timeout=5) +assert utils.on_threading_event() +""" + result = subprocess.run( + [sys.executable, "-c", script], capture_output=True, text=True, timeout=10 + ) + assert result.returncode == 0, result.stderr