From c1ddbdc24d043c3dee1d1023d4b53d7442ae5129 Mon Sep 17 00:00:00 2001 From: lmoresi Date: Mon, 2 Nov 2026 18:58:28 +1100 Subject: [PATCH 1/3] Test that a serial reference child receives the user's MPI component selections serial_reference spawns each test's np=1 diagnostic with every OMPI_* variable scrubbed, which also drops OMPI_MCA_io=^ompio: on kaiju every HDF5 error 76 under load was in such a child (#922). test_1138 checks the scrub (the user's io/mtl/btl/pml selections kept, the launcher's job-identity variables dropped) and spawns a child through _run_child that reports what it received. On development the child sees OMPI_MCA_io=None. Underworld development team with AI support from Claude Code --- .../test_1138_serial_reference_child_env.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/parallel/test_1138_serial_reference_child_env.py diff --git a/tests/parallel/test_1138_serial_reference_child_env.py b/tests/parallel/test_1138_serial_reference_child_env.py new file mode 100644 index 000000000..9c9d64ddf --- /dev/null +++ b/tests/parallel/test_1138_serial_reference_child_env.py @@ -0,0 +1,62 @@ +"""The serial reference child keeps the user's MPI component selections (#922). + +``serial_reference`` runs a test module's np=1 diagnostic in a spawned Python, and +scrubs the launcher's variables from the child's environment so that the child +runs as a singleton instead of trying to join the parent's job. The scrub dropped +every ``OMPI_*`` variable, which also dropped the user's own Open MPI component +selections. On kaiju, jobs set ``OMPI_MCA_io=^ompio`` to keep HDF5 off OMPIO, and +under load every HDF5 error 76 was in a spawned reference run that never saw it. + +The child now keeps ``OMPI_MCA_io``, ``OMPI_MCA_mtl``, ``OMPI_MCA_btl`` and +``OMPI_MCA_pml``, and still drops the job-identity variables. The first test +checks the scrub; the second spawns a child through the same launch path and asks +it what it received. + +This file carries no ``mpi(min_size=2)`` mark: under mpirun the launcher's own +variables are set too, which is the case the scrub exists for. +""" +import json + +import pytest + +from serial_reference import _run_child + +pytestmark = [pytest.mark.level_1] + +USER_SELECTIONS = {"OMPI_MCA_io": "^ompio", "OMPI_MCA_mtl": "^ofi", + "OMPI_MCA_btl": "self,vader", "OMPI_MCA_pml": "ob1"} +JOB_IDENTITY = {"OMPI_COMM_WORLD_RANK": "0", "OMPI_MCA_ess_base_jobid": "123", + "OMPI_MCA_orte_hnp_uri": "x", "PMIX_RANK": "0", "PMI_RANK": "0", + "SLURM_JOB_ID": "9", "PRTE_MCA_x": "1", "OPAL_PREFIX": "/o", + "HYDRA_CONTROL_FD": "3", "I_MPI_PIN": "1", "MPI_LOCALRANKID": "0"} + + +@pytest.mark.tier_b +def test_the_child_env_keeps_selections_and_drops_job_identity(): + from serial_reference import _child_env + env = _child_env({**USER_SELECTIONS, **JOB_IDENTITY, "PATH": "/bin", "HOME": "/h"}) + for name, value in USER_SELECTIONS.items(): + assert env.get(name) == value, f"{name} was not passed to the child" + leaked = sorted(set(JOB_IDENTITY) & set(env)) + assert not leaked, f"launcher variables passed to the child: {leaked}" + assert env["PATH"] == "/bin" and env["HOME"] == "/h" + + +CHILD = ''' +import json, os, sys +print("SERIALREF " + json.dumps({name: os.environ.get(name) for name in sys.argv[1].split(",")})) +''' + + +@pytest.mark.tier_b +def test_a_spawned_child_receives_the_parents_io_selection(tmp_path, monkeypatch): + for name, value in {**USER_SELECTIONS, "PMIX_RANK": "0"}.items(): + monkeypatch.setenv(name, value) + child = tmp_path / "child.py" + child.write_text(CHILD) + names = ",".join([*USER_SELECTIONS, "PMIX_RANK"]) + payload = _run_child(str(child), names, timeout=60) + assert isinstance(payload, dict), payload + for name, value in USER_SELECTIONS.items(): + assert payload[name] == value, f"the child saw {name}={payload[name]!r}" + assert payload["PMIX_RANK"] is None, json.dumps(payload) From ae9a45c4dccbc2b1e44c57149c54a30b07ecadf5 Mon Sep 17 00:00:00 2001 From: lmoresi Date: Mon, 2 Nov 2026 18:58:58 +1100 Subject: [PATCH 2/3] serial_reference: the child keeps the user's MPI component selections (#922) The scrub of the spawned np=1 child dropped every OMPI_* variable, the user's MCA component selections with the launcher's job identity. A job that set OMPI_MCA_io=^ompio to keep HDF5 off OMPIO had all its reference runs on OMPIO anyway, and those runs carried every HDF5 error 76 on kaiju under load. _child_env keeps OMPI_MCA_io, OMPI_MCA_mtl, OMPI_MCA_btl and OMPI_MCA_pml, and drops everything else under the launcher prefixes; test_1138 passes and is in serial batch 4. Underworld development team with AI support from Claude Code --- scripts/test.sh | 2 ++ tests/parallel/serial_reference.py | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/test.sh b/scripts/test.sh index 2c71ad75b..c6ddf955d 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -327,6 +327,8 @@ if [ $PARALLEL_ONLY -eq 0 ]; then batch 4 tests/parallel/test_1088_rotated_interface_operator_parallel.py # So is the band patch, the same rows at any rank count (#896). batch 4 tests/parallel/test_1106_fac_cover_partition_parallel.py + # So is the serial reference child's environment (#922). + batch 4 tests/parallel/test_1138_serial_reference_child_env.py # test_106*/test_107* were held back pending "a triage/deselect decision". That # decision is made: the band is not the unit of exclusion. Of the 243 tests in # the deferred bands only 26 are level_3 or tier_c; 55 are level_1 AND tier_a — diff --git a/tests/parallel/serial_reference.py b/tests/parallel/serial_reference.py index 90c366a1b..4f2638b19 100644 --- a/tests/parallel/serial_reference.py +++ b/tests/parallel/serial_reference.py @@ -23,7 +23,8 @@ which prints one ``SERIALREF `` line; the payload is broadcast to every rank. The child's environment is scrubbed of the launcher's MPI variables — inherited ``OMPI_*`` / ``PMIX_*`` make the child believe it is a member of the parent's job, and it then hangs -or aborts instead of running as a singleton. +or aborts instead of running as a singleton. The user's Open MPI component selections +(``OMPI_MCA_io``, ``_mtl``, ``_btl``, ``_pml``) are kept. The mesh is read from the same gmsh cache the parent uses, so parent and child are the same triangulation by construction; the fingerprint is carried through anyway and @@ -49,6 +50,10 @@ # Open MPI 5's runtime, which is a separate family again from OMPI_. _MPI_ENV_PREFIXES = ("OMPI_", "PMIX_", "PMI_", "MPI_", "HYDRA_", "I_MPI_", "SLURM_", "PRTE_", "PRTERUN_", "OPAL_") +# The user's Open MPI component selections are not job identity, and a singleton +# runs with them: the child keeps them. A job that sets OMPI_MCA_io=^ompio to keep +# HDF5 off OMPIO otherwise has its reference runs on OMPIO regardless (#922). +_MPI_ENV_KEPT = ("OMPI_MCA_io", "OMPI_MCA_mtl", "OMPI_MCA_btl", "OMPI_MCA_pml") _CACHE = {} @@ -106,16 +111,22 @@ def serial_reference(module_file, kind, timeout=600): return payload +def _child_env(environ): + """``environ`` without the launcher's variables, keeping the user's MPI + component selections (``_MPI_ENV_KEPT``).""" + return {k: v for k, v in environ.items() + if k in _MPI_ENV_KEPT or not k.startswith(_MPI_ENV_PREFIXES)} + + def _run_child(module_file, kind, timeout): """Never raises — see :func:`serial_reference`. Returns the payload dict, or a string describing what went wrong.""" name = os.path.basename(module_file) try: - env = {k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES)} proc = subprocess.run( [sys.executable, "-u", os.path.abspath(module_file), kind], - env=env, capture_output=True, text=True, timeout=timeout) + env=_child_env(os.environ), capture_output=True, text=True, + timeout=timeout) except subprocess.TimeoutExpired: return f"serial reference for {name}:{kind} timed out after {timeout}s" except Exception as exc: # noqa: BLE001 - see the docstring From a79d52365dee1beb6af5487d8c1fa888dd0a580d Mon Sep 17 00:00:00 2001 From: lmoresi Date: Mon, 2 Nov 2026 19:12:07 +1100 Subject: [PATCH 3/3] serial_reference review: a selection travels with its parameters; one scrub everywhere From the adversarial review of ae9a45c4: - HIGH: keeping a framework's selection without its parameters can abort the child's MPI_Init where the parent's succeeds (measured on Open MPI 5.0.10: pml=cm with mtl=ofi and mtl_ofi_provider_include=tcp gives rc 1 under the narrow keep, rc 0 on development and in the parent). _child_env now keeps OMPI_MCA_ and OMPI_MCA__* for io and its sub-frameworks (fcoll, fs, fbtl, sharedfp), mtl, btl, pml and opal_common_ucx, and still drops every other launcher-prefixed variable; the docstring says what is still dropped (osc, coll, PMIX_MCA_*, PRTE_MCA_*, OMPI_CC). - test_0855 and test_1079 spawned children with their own copy of the old scrub, so they still ran on OMPIO under a job's io=^ompio; both call _child_env now. - test_1138's child initialises MPI and must be a singleton (size 1), and every launcher-prefixed variable it receives must be a kept framework setting, which under mpirun covers the launcher's real variables. The selections include a parameter (mtl_ofi_provider_include). Settings that are not launcher identity (OPAL_PREFIX, I_MPI_PIN, PRTE_MCA_*) are no longer listed as variables the scrub must drop. Underworld development team with AI support from Claude Code --- tests/parallel/serial_reference.py | 32 +++++++++---- .../test_0855_mesh_smoothing_parallel.py | 9 ++-- .../test_1138_serial_reference_child_env.py | 46 +++++++++++-------- tests/test_1079_diffusion_disk_restart.py | 5 +- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/tests/parallel/serial_reference.py b/tests/parallel/serial_reference.py index 4f2638b19..d88ab8a4e 100644 --- a/tests/parallel/serial_reference.py +++ b/tests/parallel/serial_reference.py @@ -23,8 +23,8 @@ which prints one ``SERIALREF `` line; the payload is broadcast to every rank. The child's environment is scrubbed of the launcher's MPI variables — inherited ``OMPI_*`` / ``PMIX_*`` make the child believe it is a member of the parent's job, and it then hangs -or aborts instead of running as a singleton. The user's Open MPI component selections -(``OMPI_MCA_io``, ``_mtl``, ``_btl``, ``_pml``) are kept. +or aborts instead of running as a singleton. The user's Open MPI selections and +parameters for io (and its sub-frameworks), mtl, btl and pml are kept (:func:`_child_env`). The mesh is read from the same gmsh cache the parent uses, so parent and child are the same triangulation by construction; the fingerprint is carried through anyway and @@ -50,10 +50,15 @@ # Open MPI 5's runtime, which is a separate family again from OMPI_. _MPI_ENV_PREFIXES = ("OMPI_", "PMIX_", "PMI_", "MPI_", "HYDRA_", "I_MPI_", "SLURM_", "PRTE_", "PRTERUN_", "OPAL_") -# The user's Open MPI component selections are not job identity, and a singleton -# runs with them: the child keeps them. A job that sets OMPI_MCA_io=^ompio to keep -# HDF5 off OMPIO otherwise has its reference runs on OMPIO regardless (#922). -_MPI_ENV_KEPT = ("OMPI_MCA_io", "OMPI_MCA_mtl", "OMPI_MCA_btl", "OMPI_MCA_pml") +# The user's Open MPI component selections, with their parameters, are not job +# identity, and a singleton runs with them: the child keeps them. A job that sets +# OMPI_MCA_io=^ompio to keep HDF5 off OMPIO otherwise has its reference runs on +# OMPIO regardless (#922). A selection travels with its framework's parameters: +# pml=cm needs its mtl's provider settings, pml=ucx its UCX settings (synonyms under +# opal_common_ucx), and io's sub-frameworks configure OMPIO and ROMIO. Without them +# the child's MPI_Init can abort where the parent's succeeded. +_MPI_ENV_KEPT_FRAMEWORKS = ("io", "fcoll", "fs", "fbtl", "sharedfp", "mtl", "btl", + "pml", "opal_common_ucx") _CACHE = {} @@ -111,11 +116,20 @@ def serial_reference(module_file, kind, timeout=600): return payload +def _kept_mpi_setting(name): + """Is ``name`` one of the user's kept Open MPI framework settings + (``OMPI_MCA_`` or ``OMPI_MCA__``)?""" + return any(name == f"OMPI_MCA_{fw}" or name.startswith(f"OMPI_MCA_{fw}_") + for fw in _MPI_ENV_KEPT_FRAMEWORKS) + + def _child_env(environ): - """``environ`` without the launcher's variables, keeping the user's MPI - component selections (``_MPI_ENV_KEPT``).""" + """``environ`` for a spawned singleton: the launcher's variables removed, the + user's Open MPI framework selections and parameters kept + (``_MPI_ENV_KEPT_FRAMEWORKS``). Other MCA settings (``osc``, ``coll``, + ``PMIX_MCA_*``, ``PRTE_MCA_*``) and ``OMPI_CC`` are still dropped.""" return {k: v for k, v in environ.items() - if k in _MPI_ENV_KEPT or not k.startswith(_MPI_ENV_PREFIXES)} + if _kept_mpi_setting(k) or not k.startswith(_MPI_ENV_PREFIXES)} def _run_child(module_file, kind, timeout): diff --git a/tests/parallel/test_0855_mesh_smoothing_parallel.py b/tests/parallel/test_0855_mesh_smoothing_parallel.py index 2f11ca5cf..447dc7d71 100644 --- a/tests/parallel/test_0855_mesh_smoothing_parallel.py +++ b/tests/parallel/test_0855_mesh_smoothing_parallel.py @@ -36,7 +36,7 @@ import underworld3 as uw from underworld3.meshing import smooth_mesh_interior -from serial_reference import _MPI_ENV_PREFIXES +from serial_reference import _child_env pytestmark = [pytest.mark.mpi(min_size=2), pytest.mark.timeout(120)] @@ -254,7 +254,7 @@ def test_parallel_matches_serial_bit_identical(): # launcher's variables are stripped first, or the child's MPI_Init tries to # join THIS mpirun's job and aborts on a descriptor it does not own. # - # The prefix list is shared with serial_reference rather than restated here: + # The scrub is serial_reference's (_child_env) rather than restated here: # a local copy that named only the Open MPI family passed on Open MPI and # failed under MPICH, whose variables are PMI_* (#675). One list, so a # launcher that is missing from it is missing everywhere and gets noticed. @@ -262,10 +262,7 @@ def test_parallel_matches_serial_bit_identical(): if rank == 0: tmpdir = tempfile.mkdtemp(prefix="winslow_ref_") ref_path = os.path.join(tmpdir, "ref.npz") - clean_env = { - k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES) - } + clean_env = _child_env(os.environ) proc = subprocess.run( [sys.executable, "-c", _SERIAL_REFERENCE_SCRIPT, ref_path], diff --git a/tests/parallel/test_1138_serial_reference_child_env.py b/tests/parallel/test_1138_serial_reference_child_env.py index 9c9d64ddf..25fcaf5c5 100644 --- a/tests/parallel/test_1138_serial_reference_child_env.py +++ b/tests/parallel/test_1138_serial_reference_child_env.py @@ -7,44 +7,50 @@ selections. On kaiju, jobs set ``OMPI_MCA_io=^ompio`` to keep HDF5 off OMPIO, and under load every HDF5 error 76 was in a spawned reference run that never saw it. -The child now keeps ``OMPI_MCA_io``, ``OMPI_MCA_mtl``, ``OMPI_MCA_btl`` and -``OMPI_MCA_pml``, and still drops the job-identity variables. The first test -checks the scrub; the second spawns a child through the same launch path and asks -it what it received. +The child now keeps the user's selections and parameters for io (and its +sub-frameworks), mtl, btl and pml, and still drops the launcher's variables. A +selection is kept with its parameters: ``pml=cm`` without its mtl's provider +settings, or ``pml=ucx`` without its UCX settings, can make the child's MPI_Init +abort where the parent's succeeded. The first test checks the scrub. The second +spawns a child through the same launch path; the child initialises MPI, reports +its world size and every launcher-prefixed variable it received. This file carries no ``mpi(min_size=2)`` mark: under mpirun the launcher's own variables are set too, which is the case the scrub exists for. """ -import json - import pytest -from serial_reference import _run_child +from serial_reference import _MPI_ENV_PREFIXES, _kept_mpi_setting, _run_child pytestmark = [pytest.mark.level_1] USER_SELECTIONS = {"OMPI_MCA_io": "^ompio", "OMPI_MCA_mtl": "^ofi", + "OMPI_MCA_mtl_ofi_provider_include": "tcp", "OMPI_MCA_btl": "self,vader", "OMPI_MCA_pml": "ob1"} -JOB_IDENTITY = {"OMPI_COMM_WORLD_RANK": "0", "OMPI_MCA_ess_base_jobid": "123", - "OMPI_MCA_orte_hnp_uri": "x", "PMIX_RANK": "0", "PMI_RANK": "0", - "SLURM_JOB_ID": "9", "PRTE_MCA_x": "1", "OPAL_PREFIX": "/o", - "HYDRA_CONTROL_FD": "3", "I_MPI_PIN": "1", "MPI_LOCALRANKID": "0"} +LAUNCHER_VARIABLES = {"OMPI_COMM_WORLD_RANK": "0", "OMPI_MCA_ess_base_jobid": "123", + "OMPI_MCA_orte_hnp_uri": "x", "PMIX_RANK": "0", "PMI_RANK": "0", + "SLURM_JOB_ID": "9", "HYDRA_CONTROL_FD": "3", + "MPI_LOCALRANKID": "0"} @pytest.mark.tier_b def test_the_child_env_keeps_selections_and_drops_job_identity(): from serial_reference import _child_env - env = _child_env({**USER_SELECTIONS, **JOB_IDENTITY, "PATH": "/bin", "HOME": "/h"}) + env = _child_env({**USER_SELECTIONS, **LAUNCHER_VARIABLES, "PATH": "/bin", "HOME": "/h"}) for name, value in USER_SELECTIONS.items(): assert env.get(name) == value, f"{name} was not passed to the child" - leaked = sorted(set(JOB_IDENTITY) & set(env)) + leaked = sorted(set(LAUNCHER_VARIABLES) & set(env)) assert not leaked, f"launcher variables passed to the child: {leaked}" assert env["PATH"] == "/bin" and env["HOME"] == "/h" CHILD = ''' import json, os, sys -print("SERIALREF " + json.dumps({name: os.environ.get(name) for name in sys.argv[1].split(",")})) +from mpi4py import MPI +prefixes = tuple(sys.argv[1].split(",")) +print("SERIALREF " + json.dumps({ + "size": MPI.COMM_WORLD.Get_size(), + "mpi_env": {k: v for k, v in os.environ.items() if k.startswith(prefixes)}})) ''' @@ -54,9 +60,13 @@ def test_a_spawned_child_receives_the_parents_io_selection(tmp_path, monkeypatch monkeypatch.setenv(name, value) child = tmp_path / "child.py" child.write_text(CHILD) - names = ",".join([*USER_SELECTIONS, "PMIX_RANK"]) - payload = _run_child(str(child), names, timeout=60) + payload = _run_child(str(child), ",".join(_MPI_ENV_PREFIXES), timeout=120) assert isinstance(payload, dict), payload + assert payload["size"] == 1, f"the child is not a singleton: {payload}" + received = payload["mpi_env"] for name, value in USER_SELECTIONS.items(): - assert payload[name] == value, f"the child saw {name}={payload[name]!r}" - assert payload["PMIX_RANK"] is None, json.dumps(payload) + assert received.get(name) == value, f"the child saw {name}={received.get(name)!r}" + # under mpirun the parent carries the launcher's own variables too: none of + # them may reach the child, only the kept framework settings + unexpected = sorted(k for k in received if not _kept_mpi_setting(k)) + assert not unexpected, f"launcher variables reached the child: {unexpected}" diff --git a/tests/test_1079_diffusion_disk_restart.py b/tests/test_1079_diffusion_disk_restart.py index aa2afb1e7..b7fd4d377 100644 --- a/tests/test_1079_diffusion_disk_restart.py +++ b/tests/test_1079_diffusion_disk_restart.py @@ -10,7 +10,7 @@ import pytest import sympy import underworld3 as uw -from parallel.serial_reference import _MPI_ENV_PREFIXES +from parallel.serial_reference import _child_env pytestmark = [pytest.mark.tier_b] @@ -87,8 +87,7 @@ def test_symbolic_diffusion_fresh_process_disk_restart(tmp_path): ranks = int(os.environ.get("UW_DIFFUSION_TEST_RANKS", "1")) root = Path(__file__).resolve().parents[1] worker = root / "tests/parallel/ptest_1079_diffusion_disk_restart.py" - env = {k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES)} + env = _child_env(os.environ) launcher = [] if ranks == 1 else [str(Path(sys.executable).with_name("mpirun")), "-np", str(ranks)] for phase in ("write", "resume"): command = [sys.executable, str(root / "scripts/mpi_supervisor.py"),