Repository navigation
The hang watchdog dumps one thread, and every reader of that dump knows it (#793) - #818
Conversation
…one thread test_0053 segfaulted inside CPython's faulthandler during full tier_a runs (#793). Two measured causes, both addressed here. The dump walked every thread state (`all_threads=True`) while the watchdog created and destroyed a `threading.Timer` on every `checkpoint()` call. A thread state torn down between the dump starting and that state being read gives `EXC_BAD_ACCESS` inside `_Py_DumpTracebackThreads`. Probed in isolation: thread churn 4/6 crash against 0/6 without it, and `all_threads=True` 2/8 against `all_threads=False` 0/8. The timer churn is now gone: one `uw-watchdog` thread waits on an event with a deadline that `arm()` moves, so a checkpoint updates a timestamp instead of replacing a thread. `cancel()` sets the event and joins. The registration asks for this thread only, which is the thread that is stuck in the collective and the only one the report was ever read for. Two alternatives were measured and rejected. A pure-Python watcher loop files only 1 of 5 reports on a rank blocked in a collective, because it needs the GIL the blocked thread is holding. `dump_traceback_later()` files 5 of 5 but `cancel_dump_traceback_later()` then hangs 3 of 3. Verified at 0/30 serial failures of test_0053. Underworld development team with AI support from Claude Code
…writer Registering faulthandler with all_threads=False changes the line it writes, and three readers were keyed to the old one. The fix for #793 was incomplete without them. A dump that walks every thread labels the signalled stack "Current thread 0x...". A dump that walks the signalled thread alone writes "Stack (most recent call first):" and nothing else. underworld3.utilities.hang_report split dumps on the first form only, so it found no dumps at all in the watchdog's own files and reported a hang with no evidence. It now accepts either, which it has to: the watchdog writes the second, and scripts/mpi_supervisor.py registers all_threads=True and writes the first. test_0054's _dump_count counted the first form, so both of its end-to-end tests sat out their full 600 s cap waiting for dumps that were arriving all along -- 20 of the 28 minutes of a level_1 tier_a run. It now counts either. Two tests are added, both of which fail with all_threads=True restored: tests/parallel/test_0778_hang_watchdog_mpi.py is the parallel half that test_0053's docstring has always named and that did not exist. A bounded planted block -- rank 0 is late to an allreduce by a fixed interval, then joins -- puts every other rank inside MPI with no Python of its own able to run. The blocked ranks file repeat dumps naming the frame they are stuck in, at np=2 and np=4. tests/test_0063_mpi_hang_supervisor.py is not a substitute: it covers scripts/mpi_supervisor.py, a separate SIGUSR1 mechanism with its own registration. The repeat is asserted on the faulthandler dumps rather than on the labelled report. The labelled report is formatted in Python from sys._current_frames(), needs the interpreter lock, and has no guarantee on a blocked rank: the pre-#793 threading.Timer filed none there, the persistent reporter thread files one, and neither is a contract. faulthandler filed every dump in both cases. Claiming otherwise is also what the watch() docstring did. It said other threads were deliberately not dumped, and the labelled report has always dumped every one of them. Corrected, along with the registration comment: the Python path holds the lock and cannot read a half-built thread state, so it was never the unsafe one. Verified: level_1 tier_a 1393 passed with no segfault; test_0053, test_0054 and test_0063 26 passed in 2 minutes against the 600 s stall; test_0778 passes on every rank at np=2 and np=4. Underworld development team with AI support from Claude Code
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Reporter cancellation can time out and permit stream use after unwatch(), including reviving an old reporter during nested watching.
Review effort: Balanced
Findings: 1
Open (4)
What changed in this PR
This PR hardens hang diagnostics by avoiding unsafe all-thread traceback walks and eliminating per-checkpoint thread churn.
Changes:
- Adds a persistent watchdog reporter thread and single-thread faulthandler dumps.
- Updates hang-report parsing for both traceback formats.
- Adds serial and MPI regression coverage.
| File | Description |
|---|---|
src/underworld3/mpi.py |
Revises watchdog reporting and lifecycle. |
src/underworld3/utilities/hang_report.py |
Parses both dump headers. |
tests/test_0053_hang_watchdog.py |
Adds serial regression coverage. |
tests/test_0054_hang_report.py |
Updates dump counting. |
tests/parallel/test_0778_hang_watchdog_mpi.py |
Adds blocked-rank MPI tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if reporter is not None and reporter is not _threading.current_thread(): | ||
| reporter.join(timeout=5.0) |
| first. It needs the interpreter lock, so it is the one that goes missing | ||
| on a rank blocked inside MPI. |
| pytestmark = [ | ||
| pytest.mark.mpi(min_size=2), | ||
| pytest.mark.timeout(120, method="thread"), | ||
| pytest.mark.level_1, | ||
| pytest.mark.tier_a, | ||
| ] |
| ) | ||
|
|
||
|
|
||
| def test_the_faulthandler_dump_walks_one_thread(report): |
A report in flight on the reporter thread can reach _rearm_timer after cancel() has cleared self._reporter and stopped waiting for the thread. It would then clear the stop event and start a replacement: a reporter nobody holds, writing into a stream the caller is about to close -- which is the descriptor hazard unwatch() exists to avoid. The `cancelled` test in report() does not close it, because that value is read before cancel() sets it and acted on afterwards. The pair (self._reporter, self.cancelled) is now read and written under one lock, and _rearm_timer refuses outright once cancelled. cancel() holds the lock only for the swap and joins outside it. arm(resume=True) clears the latch before re-arming, so restoring an outer watchdog after a nested `watching` block still starts its thread. Underworld development team with AI support from Claude Code
Adversarial reviewOurs, before asking for anyone else's. Findings and evidence; one is fixed in Fixed: a leaked reporter thread on the cancel/re-arm raceA The window is narrow and the old Capability lost, and not mitigated
Unexplained behaviour we are asserting around, not onA blocked rank files exactly one labelled report in a 3 s block at a 0.5 s Unverified claim in the new test
Races we are living with
The reporter is a daemon thread, so interpreter shutdown kills it where it RetractionsFour things we said during this investigation that were wrong:
What the first commit missedWorth stating plainly, because it is the shape of the mistake rather than a Underworld development team with AI support from Claude Code |
tests/test_0054_hang_report.py spawns processes designed never to exit, both busy-polling, both with start_new_session=True so that nothing but the killpg in each `finally` reaps them. A killed pytest never runs its finally, and interrupting pytest is exactly what one does during a hang investigation -- which is when this file is being worked on. Four ranks and their prterun, orphaned to init for 15 hours at ~100% CPU each, were running on this machine while the fix was written. The 15-minute load average went from 25.2 to 12.9 when they were killed. Each script now watches the TEST RUNNER's pid, injected at spawn the way DUMPS and READY already are, and exits when it goes. Watching os.getppid() instead looks equivalent and is not: under mpirun a rank's parent is prterun, so a SIGKILLed pytest leaves the rank's parent alive and the rank never notices. Measured -- with the getppid() version all four ranks and prterun survived the full 70 s of the probe at 100% CPU; with the runner-pid version every one of the five is gone 5 s after the kill. A 900 s cap sits behind it as a backstop against a recycled pid, not as a measurement. Teardown now sends SIGTERM to the group before SIGKILL. SIGKILL alone removes mpirun from under its own ranks, which is how they end up re-parented to init; SIGTERM on prterun alone took all four ranks down with it when the orphans above were cleared. 26 passed across test_0053, test_0054 and test_0063, with no leftover processes after a clean run. Closes #639. Underworld development team with AI support from Claude Code
Issues in this branch's territoryUnder the rule in
#639, which this branch nearly left openFour ranks and their We would have shipped the wrong fix. The first attempt had each child watch Watching the test runner's pid directly, injected at spawn the way Separately, the issue's suggestion (3) is right and we can show why: What this says about the other 131
Underworld development team with AI support from Claude Code |


test_0053_hang_watchdogsegfaulted inside CPython'sfaulthandlerduringfull
tier_aruns, state-dependently (#793). Two causes, both measured, andthen three readers that had to follow.
What crashed
The dump walked every thread state (
all_threads=True) while the watchdogcreated and destroyed a
threading.Timeron everycheckpoint()call. Athread state torn down between the dump starting and that state being read
gives
EXC_BAD_ACCESSinside_Py_DumpTracebackThreads. The native stackpointed at
dump_traceback + 496with a cursor part-way through a threadlist.
Probed in isolation:
all_threads=Trueall_threads=Trueall_threads=FalseThe timer churn is gone: one
uw-watchdogthread waits on an event with adeadline that
arm()moves, so a checkpoint updates a timestamp instead ofreplacing a thread.
cancel()sets the event and joins.Two alternatives were measured and rejected. A pure-Python watcher loop files
1 report in 5 on a rank blocked in a collective, because it needs the lock
that rank is holding.
dump_traceback_later()files 5 in 5, butcancel_dump_traceback_later()then hangs 3 times in 3.Why the first commit was not the fix
all_threads=Falsechanges the line faulthandler writes. A dump that walksevery thread labels the signalled stack
Current thread 0x...; a dump thatwalks the signalled thread alone writes
Stack (most recent call first):andnothing else. Three readers were keyed to the first form.
underworld3.utilities.hang_report— the analyser that reads a directory ofper-rank dumps and names the divergent rank — split dumps on
^Current thread 0x...only. After the writer change it found no dumps at all in thewatchdog's own files, and would have reported a hang with no evidence. It now
accepts either form, which it has to: the watchdog writes the second, and
scripts/mpi_supervisor.pyregistersall_threads=Trueand writes the first.test_0054's_dump_countcounted the same string, so both of its end-to-endtests sat out their full 600 s cap waiting for dumps that were arriving all
along. That is 16 of the 28 minutes a
level_1 and tier_arun took.Tests
tests/parallel/test_0778_hang_watchdog_mpi.pyis the parallel half thattest_0053's module docstring has always named and that did not exist. Abounded planted block — rank 0 is late to an
allreduceby a fixed interval,then joins, so the collective always completes — puts every other rank inside
MPI with no Python of its own able to run. The blocked ranks file repeat dumps
naming the frame they are stuck in, at np=2 and np=4.
test_0063_mpi_hang_supervisoris not a substitute for it. That coversscripts/mpi_supervisor.py, a separate SIGUSR1 mechanism with its ownregistration, and nothing in this change touches it.
Both new tests fail with
all_threads=Truerestored in the installed tree.The repeat is asserted on the faulthandler dumps rather than on the labelled
report, because the labelled report has no guarantee on a blocked rank: it is
formatted in Python from
sys._current_frames(), needs the interpreter lock,and
_Watchdog.reportsays so. The pre-#793threading.Timerfiled nonethere, the persistent reporter thread files one, and neither number is a
contract. faulthandler filed every dump in both cases.
A docstring that was wrong
watch()'s Warnings section said other threads are deliberately not dumped.The labelled report has always dumped every one of them. There are two report
paths, they carry different things, and the docstring now says which is which:
the Python path holds the interpreter lock and so cannot read a half-built
thread state, which is why it was never the unsafe one.
Verification
test_0053+test_0054, repeat runstest_0053+test_0054+test_0063test_0778level_1 and tier_abeforelevel_1 and tier_aafterCloses #793.
Underworld development team with AI support from Claude Code