Skip to content

fix(modpoll): never wait for the exit event's lock in the signal handler - #148

Merged
gavinying merged 1 commit into
v2from
fix-signal-deadlock
Nov 14, 2026
Merged

gavinying merged 1 commit into
v2from
fix-signal-deadlock

Conversation

@gavinying

Copy link
Copy Markdown
Collaborator

Summary

modpoll occasionally ignored SIGINT or SIGTERM and had to be killed. This is the cause of the intermittent test_process_lifecycle timeout (twice during TOOL-01) and of the -9 exit in test_csv_tm_td_mqtt_equivalence_and_local_write_commands on 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(), inside threading.Event.wait(), which holds the event's internal, non-reentrant Lock for a few bytecodes on every wakeup. A signal landing there ran Event.set() on the same thread, which waited for that Lock forever.

Fix. _signal_handler hands the exit (log line plus set_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's loop.add_signal_handler.

Verification

  • Minimal reproduction (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.
  • 160 real modpoll --daemon --interval 0 processes signalled while polling: the old handler hung 2 times, the fixed one 0 times.
  • New test_signal_handler_does_not_wait_for_the_exit_event_lock holds 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 -q in packages/modpoll: 249 passed, 51 skipped. ruff check passes.

🤖 Generated with Claude Code

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>
@gavinying
gavinying merged commit a204f76 into v2 Nov 14, 2026
16 checks passed
@gavinying
gavinying deleted the fix-signal-deadlock branch November 14, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant