Repository navigation
fix(modpoll): never wait for the exit event's lock in the signal handler - #148
Merged
Merged
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
modpoll occasionally ignored SIGINT or SIGTERM and had to be killed. This is the cause of the intermittent
test_process_lifecycletimeout (twice during TOOL-01) and of the-9exit intest_csv_tm_td_mqtt_equivalence_and_local_write_commandson the merge of #147 (run).Cause. Python runs a signal handler between two bytecodes of the main thread. The main loop spends most of its time in
delay_thread(), insidethreading.Event.wait(), which holds the event's internal, non-reentrantLockfor a few bytecodes on every wakeup. A signal landing there ranEvent.set()on the same thread, which waited for thatLockforever.Fix.
_signal_handlerhands the exit (log line plusset_threading_event()) to a short-lived daemon thread, which sets the event as soon as the main thread releases the lock. modsim is not affected: it uses asyncio'sloop.add_signal_handler.Verification
Event.wait(0.0001)loop, SIGTERM at a random moment): setting the event in the handler hung 8 times in 200 runs; setting it from a thread hung 0 times in 200.modpoll --daemon --interval 0processes signalled while polling: the old handler hung 2 times, the fixed one 0 times.test_signal_handler_does_not_wait_for_the_exit_event_lockholds the event's lock and calls the handler in a subprocess. It fails (10 s timeout) on the old handler and passes on the new one.pytest -qinpackages/modpoll: 249 passed, 51 skipped.ruff checkpasses.🤖 Generated with Claude Code