Skip to content

debug_utils: async buffer sanity inspector + --inspect-buffers-on-shutdown#683

Open
DLWoodruff wants to merge 10 commits into
Pyomo:mainfrom
DLWoodruff:async-buffer-sanity-design
Open

debug_utils: async buffer sanity inspector + --inspect-buffers-on-shutdown#683
DLWoodruff wants to merge 10 commits into
Pyomo:mainfrom
DLWoodruff:async-buffer-sanity-design

Conversation

@DLWoodruff
Copy link
Copy Markdown
Collaborator

@DLWoodruff DLWoodruff commented May 11, 2026

Summary

Two things: 0. adds a test for mpi itself failing to do an LOR and

  1. Adds a new package mpisppy.debug_utils containing a passive content-check utility for the MPI-RMA send and receive buffers used by the hub-and-spoke system, plus an opt-in CLI flag that runs the check at cylinder shutdown.

Motivation. An xhat spoke was observed receiving a SHUTDOWN signal that Hub.send_terminate had not produced — suggesting that the SHUTDOWN buffer region was being written to from somewhere unexpected. mpi-sppy had no runtime sanity check on buffer contents; _validate_recv_field only checks layout at registration time. This PR adds the missing layer.

What's in it

  • mpisppy/debug_utils/buffer_inspect.py — the inspector. Single entry point inspect_buffer(buf, fld, ctx, *, send, verbose). InspectContext is duck-typed (accepts SPBase via getattr); explicit fields take precedence.
  • Generic checks (every field): trailing write_id slot is finite / non-negative / integer-valued; send-side id matches buf.id(); recv-side id does not regress below buf.id() or optional ctx.last_write_id; no inf in data; no NaN once write_id >= 1; padding region (between logical_len and padded_len) stays NaN.
  • Per-Field checks: SHUTDOWN, NONANT, NONANT_LOWER_BOUNDS, NONANT_UPPER_BOUNDS, OBJECTIVE_INNER_BOUND, OBJECTIVE_OUTER_BOUND, BEST_XHAT. Adding a new checker is one function and one dict entry.
  • --inspect-buffers-on-shutdown CLI flag in popular_args, propagated through cfg_vanilla.shared_options. _BoundSpoke.got_kill_signal reads it and runs the inspector only when shutdown is detected (the diagnostic moment). Off by default; no overhead when unset.
  • Producers are NOT touched. The inspector is purely a consumer concern.

Tests (34 new)

mpisppy/tests/test_buffer_inspect.py covers: every generic check, every per-field check, SPBase fallback for nonant length, verbose dump, severity ladder, and the config-flag wiring (default false, propagation through shared_options). Wired into the existing unit-tests CI job so codecov picks it up.

Documentation

  • doc/src/debug_utils.rst — user-facing page under For Developers and Contributors.
  • doc/designs/async_buffer_sanity_design.md — internal design doc with invariants, check tables, and explicit non-goals (cross-cylinder consensus, module-level history state).

Draft because

Opened as a draft so the design can be reviewed before any further hot-path integrations (e.g., wiring the inspector into update_nonants or sync_bounds) are added. The current scope is intentionally narrow: prove out the SHUTDOWN check, then expand.

Test plan

  • python -m pytest mpisppy/tests/test_config.py mpisppy/tests/test_buffer_inspect.py — 112 passed
  • ruff check clean on all touched files
  • Manual smoke: stomped recv buffer (data=1.0, write_id=0) produces the expected SHUTDOWN-specific finding
  • Manual smoke: padding-region overrun produces a padding finding
  • Manual smoke: --inspect-buffers-on-shutdown propagates through shared_options for hub and spoke; unset default propagates as False
  • Real reproduction of the motivating spurious-SHUTDOWN incident with the flag enabled

🤖 Generated with Claude Code

DLWoodruff and others added 2 commits May 11, 2026 16:25
mpisppy/debug_utils/buffer_inspect.py is a passive content checker for
SendArray / RecvArray that detects the SHUTDOWN-stomp signature plus a
small set of per-Field invariants. Producers are not touched; callers
pass an optional InspectContext (or an SPBase via duck-typing).

_BoundSpoke.got_kill_signal gets an env-gated hook
(MPISPPY_INSPECT_BUFFERS=1) that runs the inspector on each shutdown
poll and prints findings only when the report is non-OK.

See doc/designs/async_buffer_sanity_design.md for invariants leaned on
(NaN-padding init from communicator_array, buf.id() as recv baseline),
the check tables, and the wiring rationale.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces the prior MPISPPY_INSPECT_BUFFERS env-var gate with a Config
flag --inspect-buffers-on-shutdown (popular_args). The flag is
propagated through cfg_vanilla.shared_options into opt.options and
read by _BoundSpoke.got_kill_signal only when a shutdown is detected;
when the report is non-OK the spoke prints rank info plus the
verbose Report dump. The check fires at the moment of detection
because a spurious shutdown's buffer state has not yet been overwritten
by later activity.

Public surface: mpisppy.debug_utils now re-exports inspect_buffer,
InspectContext, and Report.

Adds mpisppy/tests/test_buffer_inspect.py (34 cases covering generic
checks, all per-field checkers, SPBase fallback, verbose dump, and
the config-flag wiring). Wired into the unit-tests CI job.

User-facing RST page (doc/src/debug_utils.rst) added under For
Developers and Contributors; design doc updated to reflect the
CLI-flag wiring.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 97.24771% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.45%. Comparing base (0248c97) to head (e1382e8).

Files with missing lines Patch % Lines
mpisppy/debug_utils/buffer_inspect.py 96.57% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #683      +/-   ##
==========================================
+ Coverage   70.14%   70.45%   +0.31%     
==========================================
  Files         154      157       +3     
  Lines       19257    19474     +217     
==========================================
+ Hits        13507    13721     +214     
- Misses       5750     5753       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

DLWoodruff and others added 8 commits May 12, 2026 11:07
Switches the inspector's failure path at the shutdown call site from
print() to warnings.warn(..., RuntimeWarning). This makes the signal
filterable, capturable in tests, and escalatable to a hard error via
python -W. We don't raise at this site because got_kill_signal runs
during the collective shutdown path; aborting one rank would deadlock
the others on the next barrier.

Tests:
- Unit: TestSpokeGotKillSignalWarning drives Spoke.got_kill_signal on a
  hand-stomped buffer (asserts the warning fires), on a legit shutdown
  (asserts silence), and with the flag off (asserts the inspector is
  skipped entirely).
- Smoke: the existing multi-stage Aircond cylinder invocation in
  straight_tests.py now also passes --inspect-buffers-on-shutdown and
  -W error::RuntimeWarning:mpisppy.cylinders.spoke, so a healthy run
  doubles as a no-false-positives check; a regression that produces a
  buffer warning will fail the smoke.

Design doc updated to reflect the warn/no-raise rationale and the new
coverage layers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
CI runs straight_tests.py with python_args="-m coverage run ..." prepended.
With -W placed after python_args, the resulting command was
"python -m coverage run ... -W error::... -m mpi4py ...", which made
coverage parse -W as one of its own flags and fail with
"no such option: -W". Moving -W before python_args yields
"python -W ... -m coverage run ... -m mpi4py ...", which Python parses
first; coverage then runs the target in-process under the installed
filter.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Previously the SHUTDOWN call site only inspected the SHUTDOWN recv
buffer, so six of seven per-field checkers (_check_nonant,
_check_lower_bounds, _check_upper_bounds, _check_objective_scalar,
_check_best_xhat) saw only synthetic buffers in unit tests. A false
positive in any of them would not have been caught by CI.

Spoke.got_kill_signal now delegates to _inspect_buffers_on_shutdown,
which inspects every registered receive buffer and every send buffer
through inspect_buffer. SHUTDOWN goes first and verbose; the rest are
non-verbose. InspectContext(spbase=self.opt) is threaded so checkers
that need nonant length pick it up via the spbase fallback. One
RuntimeWarning per bad buffer, tagged with field name and direction.

With the existing -W escalation in the straight_tests Aircond smoke,
this turns that smoke into a real-buffer no-false-positives guard for
every checker whose field is used by the run (SHUTDOWN, NONANT,
OBJECTIVE_INNER_BOUND, OBJECTIVE_OUTER_BOUND, BEST_XHAT). The bounds
checkers (NONANT_LOWER_BOUNDS / NONANT_UPPER_BOUNDS) stay uncovered by
smoke because Aircond doesn't run a spoke that produces those.

Tests:
- Two new unit tests: multi-buffer sweep (stomped OBJECTIVE_INNER_BOUND
  among healthy buffers -> exactly one warning) and the healthy-only
  sweep (no warnings under warnings.simplefilter("error", RuntimeWarning)).
- Existing tests adjusted: stub now exposes send_buffers, _split_key,
  optional spbase nonant_length, and binds the new helper methods.

Design doc updated with the sweep description, what smoke covers, and
what remains uncovered.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The CI sweep on multi-stage Aircond surfaced a real false positive in
_check_nonant: it compared len(data) against ctx.get_nonant_count(), but
NONANT buffers are sized to the *publisher's* sum of per-scenario
nonants across that publisher's local scenarios -- not per-scenario.
With 24 leaf scenarios on the hub and 6 nonants per scenario, the
buffer is 144 wide while spbase.nonant_length is 6, so the check
spuriously fired.

The recv buffer's logical length is already validated at registration
in _validate_recv_field, so the hard-equality check in _check_nonant
was redundant on the recv side. Relax it to "len(data) must be a
positive multiple of nonant_count" -- still catches a torn or
truncated buffer, no false positives in the multi-scenario case.

Tests:
- New: multi-scenario buffer (24 = 4*6) passes.
- New: non-multiple length (10 not divisible by 6) is still caught.
- Existing length-mismatch tests still pass; they use 5 vs 7, and
  5 % 7 != 0 still trips the relaxed check.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…init)

The previous check accepted {0.0, 1.0}, but Hub.send_terminate (the only
producer) only ever writes 1.0. So 0.0 -- and any other non-1.0 value --
can only appear via a cross-field stomp, an RMA race, or a producer bug.
Accepting 0.0 was a silent false negative on exactly the kind of stomp
signature the inspector is meant to catch.

The check now rejects anything that isn't (1.0 with write_id>=1) or
(NaN with write_id==0, the initial state from communicator_array).

Tests:
- New test_shutdown_zero_is_rejected: data[0]=0.0 is now flagged.
- Existing test_shutdown_value_not_in_set updated to match the new
  error message; 0.5 is still flagged.
- Existing test_initial_state_passes (NaN + write_id==0) still passes.

Design doc updated to lead with the general rule.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Under numpy 2.x, repr(np.float64(0.0)) is 'np.float64(0.0)', not '0.0',
so the substring check on "data[0]=0.0" failed in CI (which runs
numpy 2.4.4) even though the rejection itself was working. Switch the
match to the stable message text "only 1.0 is ever published".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
A small barrier-style sanity check on an MPI comm: each rank Allreduces a
uint8 0 with MPI.LOR, rank 0 announces START/STOP with the comm name, and
any rank seeing a non-zero reduction prints the value, comm name, and a
stack trace. The non-zero branch is exercised via a fake comm in the test
so the failure-print path is covered without adding a test-only flag.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@DLWoodruff DLWoodruff marked this pull request as ready for review May 15, 2026 21:40
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