Skip to content

Stokes.set_active_region refuses instead of ending in a segfault (#1154) - #1202

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/stokes-active-region-1154
Nov 16, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/stokes-active-region-1154

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #1154 by refusing (the issue's option 2).

The defect. SNES_Stokes_SaddlePt.set_active_region(name, value) is a public method. It configured a region discrete system (DMSetRegionDS) so that only one region's cells would be assembled. On #1154's case, a box with an internal boundary and essential conditions on the walls, the first residual evaluation segfaulted inside PETSc while inserting the essential boundary values (ISGetIndices in DMProjectLocal_Generic_Plex). Two more defects, by reading:

  • the region value was stored and never read, and PETSc takes a labelled system's cells from the label's stratum at value 1, which a region label does not carry;
  • with no label, the default system would still be assembled on the cells meant to be inactive.

The fix. The method now raises NotImplementedError on every rank, from its arguments alone, naming mesh.extract_region. That is the route the design record took (docs/developer/design/submesh-solver-architecture.md, 2026-04-05: "PETSc Region DS segfaults during assembly ... Dead end for now"). A user can no longer reach a segmentation fault through a documented method.

Removed (H1's review): _setup_region_ds and the check in the solver set-up that called it, now unreachable, with their entries in the docstring inventory and review queue. That is the issue's option 1 for the dead code. The method itself stays, refusing.

The example docs/examples/submesh_investigation/test_region_ds_phase3.py calls the method. It now stops with the refusal instead of segfaulting; it is one line of #944.

Tests.

  • tests/parallel/test_1154_stokes_active_region_refused_parallel.py, tier B: the issue's case refuses with the same message on every rank, and the same solver without the call converges. Development does not refuse, and its solve segfaults. It passes at np 1 and 2, and runs in batch 3 and under --p N.
  • test_0856's case for a missing region label now expects the refusal at the call.

Measured on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH).

Checklist (UW3 Style Charter — docs/developer/UW3_STYLE_CHARTER.md)

  • I have read the Style Charter and followed it over the surrounding code (S2)
  • No drive-by refactors, renames, or "while I was here" cleanups (S9)
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers (S8)
  • No hedging names (maybe_ / try_ / do_) and no commented-out code (S3, S4)
  • Every exception swallow states its sanctioned failure mode in a comment (S4)
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7)
  • Parallel safety considered (S11): the refusal depends on nothing rank-local; np 1 and 2 checked
  • No mesh entity or rank identified by coordinates; any remaining tolerance is named, relative and says what it bounds (S12)
  • No pixi.toml / pixi.lock or other dependency changes riding in an unrelated PR
  • AI-assisted work carries the attribution line below

Underworld development team with AI support from Claude Code

The method configured a region discrete system (DMSetRegionDS). The first residual
evaluation then segfaulted inside PETSc while inserting the essential boundary
values, the region value was never used (PETSc takes a labelled system's cells from
the stratum at value 1), and the default system would still have been assembled on
the inactive cells. It now raises NotImplementedError on every rank, naming
mesh.extract_region, the route the design record took (2026-04-05).

tests/parallel/test_1154_stokes_active_region_refused_parallel.py: the issue's case
refuses with the same message on every rank, and the solve without it converges;
development does not refuse (and the solve segfaults). test_0856's missing-label
case now expects the refusal at the call.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 16, 2026 00:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Verdict on head 532dbfe: ready to merge on green from this review (a comment, not a GitHub approval). The segfault reproduces on development and is gone on this head. The refusal is raised on every rank with the same message, including a rank that holds no cells. No working caller is broken. The two notes below are non-blocking.

Merge base: fafbecb (current development).

Findings

There are no blocking findings.

1. Minor: one investigation script still calls set_active_region

docs/examples/submesh_investigation/test_region_ds_phase3.py (line 126) calls stokes.set_active_region("Outer", mesh.regions.Outer.value).

  • On development that script would segfault at its first solve, so nothing that worked is broken.
  • It now stops at that line with the new NotImplementedError, whose message points to mesh.extract_region.
  • Pytest does not collect docs/examples, so no test fails.

Two follow-ups would keep readers from copying a dead route: a one-line comment at the top of the script, or moving it next to the other region-DS prototypes as historical.

docs/docstrings/review_queue.md and docs/docstrings/inventory.json still list set_active_region and _setup_region_ds under their old descriptions. This is cosmetic.

2. Nit: dead code is left in place

_setup_region_ds and the _build branch that calls it (if hasattr(self, '_inactive_region_label') ...) are now unreachable, as the PR says. Keeping them is reasonable while the region-DS route might come back. A comment at the _build branch pointing to #1154 would save the next reader a search.

Confirmed correct

The segfault reproduces on development and is gone here. I used my own script, not the PR's test: a Stokes problem with walls pinned, then set_active_region("Outer", 102), then solve().

development (176958a; it differs from fafbecb only in Python files the solver does not use) this head
BoxInternalBoundary, np 1 SEGV in the first residual evaluation refused; solve converges (reason 2)
BoxInternalBoundary, np 2 SEGV in the first residual evaluation refused; solve converges (reason 2)
AnnulusInternalBoundary (the layout from the phase-3 investigation script), np 1 SEGV in the first residual evaluation refused; solve converges (reason 2)
AnnulusInternalBoundary, np 2 not run refused; solve converges (reason 2)
  • Every rank refuses together, including an empty rank. The method raises from its arguments alone, before any PETSc or MPI call, so no rank can take a different path.
    • I checked this with an allgather of the messages at np 1 and 2 (default partition), and at np 3 and 4 with a shell partition that leaves the last rank with no cells (cells per rank: 38/38/0 and 27/22/27/0).
    • All messages were identical, and the same solver then solved on every rank.
  • No legitimate caller is broken. I searched src, tests, docs and the examples. The only caller outside the PR's own tests is the investigation script in finding 1. The rewritten test_0856 case asserts the refusal at the call, which is where the missing-label path now ends.

What was run

  • Serial (pytest -n 8): 124 passed.
    • The PR's tests: test_1154 and test_0856.
    • Stokes: test_1010–1014, 1017, 0786, 1066 and 1145.
    • Regions and submeshes: test_0771, 0772, 0856_remove_embedded, 0073, test_1154_a_deleted_solver_is_freed and test_1175.
  • Parallel, under the MPI supervisor: test_1154, test_0856, test_0786 (Stokes on a rank without cells), test_1175, test_0770 and test_1085.
    • np 2: 36 passed and 1 skipped on each rank. The skip is test_0786's own np 2 case: its one-cell triangle has no free velocity node.
    • np 4: 37 passed on each rank.
  • Reproduction script: 3 runs on development (box at np 1 and 2, annulus at np 1). 6 runs on this head (box and annulus at np 1 and 2, and the box at np 3 and 4 with an empty rank). Logs are kept by the reviewer.

Not run:

  • the full scripts/test.sh.
  • the investigation script itself. Its behaviour follows from the line it calls.

Underworld development team with AI support from Claude Code

…nreachable region-DS setup (H1's review)

set_active_region refuses, so _setup_region_ds and the check in the Stokes set-up
that called it could not be reached: removed, with their entries in the docstring
inventory and review queue. No behaviour changes.

Underworld development team with AI support from Claude Code
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.

3 participants