Skip to content

A failed JIT build raises on every rank, with the compiler's output (#842) - #1217

Merged
lmoresi merged 3 commits into
developmentfrom
bugfix/jit-build-failure-collective-842
Nov 16, 2026
Merged

lmoresi merged 3 commits into
developmentfrom
bugfix/jit-build-failure-collective-842

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member

Fixes #842.

The defect. getext (utilities/_jitextension.py), under MPI with the disk cache on, compiles on rank 0 inside if rank == 0, and the other ranks wait for it at a barrier.

  • The hang: a failed build raised on rank 0 alone, and the other ranks waited at the barrier until a wall-clock limit. H1 measured it: rank 0 raised in 1.4 s, rank 1 was still waiting at 240 s.
  • The blind error: it named the build directory but not what the compiler said, unless verbose=True.

The fix.

  • Disk-cache path: rank 0's build failure is held until the barrier, broadcast, and raised on every rank as "JIT build of ... failed on rank 0, so it fails on every rank", with rank 0's error text.
  • Per-rank path (no shared disk cache, each rank compiles its own copy): a failure on some ranks is gathered and raised on all. This path is only collective under MPI, as the cache path already was.
  • The error from compile_and_load now ends with the build's exit code and the last 40 lines (at most 4000 characters) of its output, whatever verbose is set to.

A successful build runs exactly as before.

Test. tests/parallel/test_0842_jit_build_failure_collective_parallel.py, tier B. It solves a fresh expression with an invalid UW3_JIT_CFLAGS (-fthis-flag-does-not-exist-0842), and asserts that every rank raises and that each message carries the compiler's complaint about the flag.

  • On development: it hangs at np 2 (supervisor exit 124), and the serial error has no compiler output.
  • Here: every rank raises in about 3 s at np 1, 2 and 4, with "x86_64-conda-linux-gnu-cc: error: unrecognized command-line option '-fthis-flag-does-not-exist-0842'".
  • Where it runs: batch 3 and under --p N.
  • Also run: the JIT and cache test files serially (71), ptest_jit_cache.py with the new test at np 2 and 4 (6 each), and the coverage gate.

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

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): the build's exception is held only until every rank can raise it
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7)
  • Parallel safety considered (S11): the failure is agreed through the existing barrier (a broadcast) or an allgather on the per-rank path; np 1, 2, 4 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

…842)

Under MPI with the disk cache, rank 0 builds and the others wait for it at a barrier.
A failed build raised on rank 0 alone and the other ranks waited until a wall-clock
limit. Rank 0's failure is now held until the barrier, broadcast, and raised on every
rank. Where each rank builds its own copy (no shared disk cache), a failure on some
ranks is gathered and raised on all. The error now carries the last lines of the
build's output, whatever `verbose` is set to.

tests/parallel/test_0842_jit_build_failure_collective_parallel.py: an invalid
UW3_JIT_CFLAGS on a fresh expression; development hangs at np 2 (supervisor exit 124)
and its serial error has no compiler output. Every rank raises, with gcc's
"unrecognized command-line option", in about 3 s at np 1, 2 and 4.

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

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 6ff805a: changes requested. The main path is fixed. When rank 0's build fails, every rank now raises the same error together, with the compiler's message and exit code. One branch of the same barrier block still raises on a single rank and hangs the others. The fix is small (see 1).

Findings

1. Medium: the fallback compile on a non-zero rank is not agreed, so it can still hang

In the disk-cache path, after the barrier, a rank whose _jc.load_module returns None compiles locally (the "Defensive: rank-0 publish failed" fallback). That compile_and_load is outside the new agreement. If it fails, the error is raised on that rank alone, and rank 0 and the other ranks carry on into the solve.

This can happen whenever a rank cannot see rank 0's published module: a node-local or per-node cache directory, a write that store_module swallows (its OSError returns), or a stale entry. The build can then fail on that rank only: the compiler is OOM-killed on one node, /tmp is full on one node, or the toolchain differs per node.

Failure case, reproduced at np 2:

  • Rank 0's cache directory already holds the module and rank 1's is empty, so needs_compile is true and rank 0 does not build.
  • Rank 1 loads nothing and falls back to a local compile.
  • That compile fails (the compiler is pointed at a missing binary on rank 1 only).
  • Rank 1 raised RuntimeError after 1.0 s. Rank 0 never returned and the job was killed at the 120 s limit. This is the JIT: a failed rank-0 build hangs the other MPI ranks at the cache barrier #842 symptom, from the other side.

Suggested fix: wrap the fallback in the same try/except and agree on its outcome before anyone continues. For example:

fallback_error = None
if module is None:
    try:
        module, _tmpdir = compile_and_load(real_modname, codeguys_final, verbose=verbose)
    except Exception as error:
        fallback_error = f"{type(error).__name__}: {error}"
failed = underworld3.mpi.comm.allgather(fallback_error)
if any(e is not None for e in failed):
    raise RuntimeError(...)   # same wording as the disk-disabled branch

This adds one allgather per cold build and nothing to the warm path. A test can force the case without a second node by giving each rank its own UW_JIT_CACHE_DIR, with rank 0's pre-warmed.

2. Low: a cache-write failure on rank 0 is reported as a build failure

_jc.store_module is inside the new try, so any exception it raises is reported as "JIT build of ... failed on rank 0". That includes the PermissionError from creating the lock file in an unwritable cache directory, which store_module does not catch. In that case the compile succeeded and rank 0 holds a working module.

Measured with a read-only UW_JIT_CACHE_DIR and a healthy compiler:

  • At np 2, both ranks raise "JIT build ... failed on rank 0 ... PermissionError: [Errno 13] Permission denied: '.../..lock'".
  • On development the same run hangs: rank 0 raises PermissionError and rank 1 waits at the barrier until the timeout. So the PR still improves on it.
  • At np 1 it raises PermissionError on both development and the PR, so the failure being fatal is older than this PR.

The wording is the issue here. Either keep store_module out of the try and agree its error under its own message, or (better, and separate) make a failed cache write a warning. Then the job runs from the in-memory module, and the other ranks fall back to compiling locally, which finding 1 makes safe.

3. Nit: the serial and single-rank error loses its type and its chain

  • With one rank (or no disk cache), build_error is a string and the code raises RuntimeError(build_error). The message then starts "RuntimeError: The Underworld extension module ...", and the original traceback is dropped.
  • At np 1, re-raising the caught exception unchanged would keep the old serial behaviour exactly.
  • In the multi-rank branches, raise ... from error on the rank that failed would keep the original traceback there.

4. Nit: the Barrier before the bcast is redundant

bcast from root 0 already synchronises the waiting ranks, so the Barrier() can go. It is harmless.

Confirmed correct

  • The claim, at np 2 and 4 (cold, empty cache, UW3_JIT_CFLAGS="-fno-such-flag-842"):
    • Every rank raised the same RuntimeError, naming the module, "failed on rank 0, so it fails on every rank", "The build exited with code 1", and the compiler's line "unrecognized command-line option '-fno-such-flag-842'".
    • Time to the raise was the same on every rank: 1.19 s on both ranks at np 2 and 1.05 s on all four at np 4, with 1.5 s and 1.4 s wall, then a clean exit. No rank waits past the failure.
    • On development (merge base aa3c49d) at np 2, rank 0 raised at 1.21 s with no compiler output, and rank 1 never returned (killed at 120 s).
  • Other failures, all at np 2, all ranks raising together with the cause in the message:
    • A C syntax error injected into the generated cy_ext.h: "cy_ext.h:59:35: error: expected expression before ';' token", exit code 1, raised at 1.58 s on both ranks.
    • A missing compiler (CC pointing at a path that does not exist): "error: command '/nonexistent/cc-842' failed: No such file or directory", exit code 1.
    • A build that exits 137 with a single line of output: exit code 137 and that line reported.
    • With the disk cache off (UW_JIT_CACHE=0), a build failing on rank 1 only. Both ranks raise "failed on rank(s) [1]", and rank 0's message carries rank 1's compiler output.
  • Collectives:
    • Rank 0's outcome reaches the other ranks by bcast after the barrier in the disk path, and by allgather in the disk-disabled path.
    • needs_compile and disk_enabled are already global reductions, so every rank enters the same branch.
    • Rank 0 hit with rank 1 miss (separate cache directories, healthy compiler): solves.
    • Rank 0 miss with rank 1 hit: solves.
    • Rank 0 miss with a failing build while rank 1 hits: both ranks raise together.
    • Only the fallback in finding 1 escapes.
  • The warm cache still skips the build: a second run on a warm cache, with the compiler made unusable on every rank, solves in 0.24 s against 2.5 s cold. The in-memory tier is untouched by the diff.
  • Healthy cold-cache build is unaffected:
    • Fresh empty cache, two repeats each. Every rank loaded the module and solved, and each cache held one module and its manifest afterwards.

    • Solve times:

      ranks development (s) PR (s)
      2 2.04, 2.01 2.02, 2.03
      4 1.68, 1.64 1.63, 1.66
    • ptest_jit_cache Test 5 still shows a single compile on rank 0 (per-rank cc invocations [1, 0] at np 2 and [1, 0, 0, 0] at np 4).

  • The output tail is bounded (last 40 lines, at most 4000 characters), uses stderr and falls back to stdout, and is appended whatever verbose says.

What was run

The merge base aa3c49d differs from the last development build in a compiled source (_function.pyx), so it was built fresh. The PR's one changed .py file was laid over a copy of that install. underworld3.__file__ was checked to resolve to the overlay. Tests came from a worktree at 6ff805a.

  • JIT tests serially:

    • Files: test_0004_pointwise_fns, test_0021, test_0024 to test_0027, test_0103, test_0105, test_jit_cache, test_jit_deterministic_ordering, the new test, and test_0022_jit_rank_source_agreement.
    • Result: 49 passed, 3 skipped. The skips are test_0022's, which need two ranks.
  • ptest_jit_cache.py: all 5 tests pass at np 2 and at np 4.

  • The new test with test_0022:

    • np 1: 1 passed, 3 skipped.
    • np 2: 4 passed.
    • np 4: 4 passed.
  • The new test on development at np 2: it hangs, with no result before the 280 s limit, and was killed.

  • Probes:

    Logs kept by the reviewer.

  • Not run:

    • A real multi-node job with node-local caches. Finding 1 was reproduced on one node with a cache directory per rank.
    • Compilers other than the conda gcc.
    • The full test suite.

Underworld development team with AI support from Claude Code

…842)

A rank that cannot load rank 0's published module compiles its own copy;
that build was outside the agreement, so a failure there raised on that
rank alone and the others hung in the solve. Its outcome is now gathered
before any rank goes on. A failed cache write on rank 0 is reported as
such, not as a failed build. At one rank the original exception is
re-raised unchanged, and the failing rank chains it. The barrier before
the bcast is gone.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Answers to H1's review (H1 is the session that reviews pull requests) are in ae5c152. #1218 has not merged, so the agreement is a plain allgather and not first_verdict.

  • 1, the fallback build:
    • Fix: a rank that cannot load rank 0's module still compiles its own copy, but inside a try. Every rank then joins one allgather of the outcome (_raise_if_any_rank_failed, which the disk-disabled branch now uses too), and all raise "failed on rank(s) [k]" with the failing rank's message. That adds one allgather to a cold build and nothing to a warm one.
    • Test: test_a_failed_fallback_build_on_one_rank_raises_on_every_rank. Rank 0's cache is warmed, the in-memory tier is cleared, the other ranks get empty cache directories, and the last rank's build fails.
      • How it fails: the failure is simulated by patching compile_and_load on that rank. A bad UW3_JIT_CFLAGS doesn't reach the second build, because the flags are fixed into the generated setup.py when the code is first generated. A bad CC doesn't either, because _petsc_build_env sets PETSc's compiler.
      • Before: on the previous head the test hangs at np 2 (the supervisor's exit 124).
      • After: it passes at np 2 and 4.
  • 2, the cache write: store_module now has its own try. Its failure is reported as "JIT build of ... succeeded on rank 0 but storing it in the JIT cache failed, so it fails on every rank".
    • Test: test_a_failed_cache_write_says_so_on_every_rank, with a read-only cache directory. It fails on the previous head (the message said "build failed") and passes at np 2 and 4.
    • Not done here: making a failed write a warning and running from the in-memory module is the better behaviour, as you said, and a separate change. I've left it out of this PR.
  • 3, the exception type: at one rank, or with the disk cache off, the original exception is re-raised unchanged. In the multi-rank branches the failing rank's RuntimeError is raised from its own exception, so its traceback is kept there.
  • 4, the barrier: removed. The bcast from rank 0 waits for it.

Runs:

Underworld development team with AI support from Claude Code

@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.)

Recheck of head ae5c152: ready to merge on green from this review. All four findings from the first review are resolved. The merge base is still aa3c49d, so the same fresh build was used, with the head's _jitextension.py laid over it.

The findings from the first review

# Finding At ae5c152
1 The fallback compile on a non-zero rank was not agreed, so the other ranks hung Fixed. The fallback outcome goes through an allgather (_raise_if_any_rank_failed). Every rank raises together; see the rerun below.
2 A cache-write failure was reported as a build failure Fixed. The message now reads "JIT build of ... succeeded on rank 0 but storing it in the JIT cache failed, so it fails on every rank", followed by the PermissionError. The original exception is chained.
3 The error at np 1 lost its type and traceback Fixed. np 1 re-raises the original exception (see below). The multi-rank raises chain the original with from.
4 Redundant Barrier Removed. The bcast alone synchronises the ranks.

Reruns

Case np Result
Fallback: per-rank cache directories, rank 0 warm, build fails on rank 1 only 2 Both ranks raise "failed on rank(s) [1]" at 0.97 s, with the compiler's line. No hang. (At the previous head, rank 0 hung to the 120 s limit.)
Same 4 All four ranks raise "failed on rank(s) [1]" at 1.63 s. No hang.
#842 reproducer, -fno-such-flag-842, cold cache 2 Both ranks raise at 1.09 s with "exited with code 1" and "unrecognized command-line option '-fno-such-flag-842'".
Same 4 All four ranks raise at 1.00 s, same message.
Syntax error injected into generated cy_ext.h 2 Both raise at 1.19 s: "cy_ext.h:59:35: error: expected expression before ';' token".
Missing compiler 2 Both raise at 0.96 s: "command '/nonexistent/cc-842' failed: No such file or directory".
Build exits 137 2 Both raise at 0.26 s: "exited with code 137" and its one line of output.
Read-only cache directory (wording) 2 Both raise "succeeded on rank 0 but storing it in the JIT cache failed", followed by the PermissionError.
np 1 bad flag 1 Raises the original RuntimeError from compile_and_load, not a wrapper. The traceback runs getext to compile_and_load, and the message includes the compiler output. A read-only cache at np 1 raises the original PermissionError from _file_lock.

Healthy cold-cache build, then a warm rerun on the same cache, two repeats each. Every rank solved, and each cache held one module and its manifest.

np development: cold, warm (s) ae5c152: cold, warm (s)
2 1.91 / 1.99, 0.18 / 0.19 1.95 / 1.86, 0.18 / 0.17
4 1.62 / 1.60, 0.18 / 0.19 1.66 / 1.64, 0.19 / 0.19

Tests

Test np Result
JIT set serially: test_0004_pointwise_fns, 0021, 0024 to 0027, 0103, 0105, test_jit_cache, test_jit_deterministic_ordering, test_0022, the new test 1 49 passed, 5 skipped (3 in test_0022 and 2 in the new file, which need np > 1)
ptest_jit_cache.py 2 all 5 pass; per-rank cc invocations [1, 0]
ptest_jit_cache.py 4 all 5 pass; per-rank cc invocations [1, 0, 0, 0]
test_0842_jit_build_failure_collective_parallel.py 1 1 passed, 2 skipped (fallback and cache-write tests need np > 1)
Same 2 3 passed
Same 4 3 passed

Not run: a real multi-node job with node-local caches (reproduced on one node with a cache directory per rank), and the full test suite. Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit f4afddb into development Nov 16, 2026
10 checks passed
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