Skip to content

Parallel job hangs when one MPI process fails if PETSc is used #635

Description

@yguclu

Summary

PR #624 introduced fixed #621, but did not consider the case where petsc4py was used in the MPI-parallel function. This deserves a dedicated investigation.

Reproducer

Originally posted by @FrederikSchnack in #624 (comment)

I reviewed this PR with Claude and it came up with an issue: The job still hangs if PETSc is used. The following script should replicate it as is described in the doc strings. Is this a relevant problem?

"""
Demonstrate that the abort-at-exit of PR #624 hangs once PETSc is in use.

One process raises an exception in `main`, while the others wait in a
barrier. `parallel_run_from_cli` should then terminate all the processes.

    # Expected: traceback, then MPI_Abort and exit code 1
    timeout 60 mpirun -n 2 python demo_abort_petsc_hang.py; echo "exit=$?"

    # Same, after converting a vector to PETSc: the job hangs (exit=124)
    timeout 60 mpirun -n 2 python demo_abort_petsc_hang.py --petsc; echo "exit=$?"

The failing process cannot call MPI_Abort, because `PetscFinalize` (which
runs at interpreter exit, before mpi4py's abort handler) waits for the other
processes, which in turn wait for the failing one in the barrier.
"""
def parse_input_arguments():
    import argparse

    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=__doc__.split('\n\n')[0],
    )
    parser.add_argument('--petsc', action='store_true',
                        help='Convert a PSYDAC vector to PETSc before failing')
    parser.add_argument('--fail-rank', type=int, default=1, dest='fail_rank',
                        help='MPI rank of the process which raises an exception')
    return parser.parse_args()


def main(*, petsc, fail_rank, mpi_comm):

    import numpy as np

    from psydac.ddm.cart    import DomainDecomposition
    from psydac.fem.splines import SplineSpace
    from psydac.fem.tensor  import TensorFemSpace

    # Distributed 2D spline space, as in a typical PSYDAC simulation
    ncells = [8, 8]
    ddm    = DomainDecomposition(ncells, [False, False], comm=mpi_comm)
    spaces = [SplineSpace(2, grid=np.linspace(0., 1., n + 1)) for n in ncells]
    V      = TensorFemSpace(ddm, *spaces)
    x      = V.coeff_space.zeros()

    # Convert to PETSc using PSYDAC's own interface (imports petsc4py)
    if petsc:
        x_petsc = x.topetsc()
        print(f'rank {mpi_comm.rank}: PETSc vector of size {x_petsc.getSize()}', flush=True)

    # One process fails, the others wait for it
    if mpi_comm.rank == fail_rank:
        raise RuntimeError(f'failure on rank {fail_rank}')

    mpi_comm.Barrier()

    return locals()


if __name__ == '__main__':

    from psydac.utilities.parallel_utils import parallel_run_from_cli

    namespace = parallel_run_from_cli(parse_input_arguments, main)

I use Arch Linux (kernel 7.2.11-arch1-1, x86_64), PETSc 3.25.0, petsc4py 3.25.0, Open MPI 5.0.11, mpi4py 4.1.2, Python 3.14.8.

If I run the script as
mpirun -n 2 python demo_abort_petsc_hang.py --petsc,

I get the following output:

Running function 'main' in parallel with 2 MPI processes, and arguments:
{'petsc': True,
 'fail_rank': 1,
 'mpi_comm': <mpi4py.MPI.Intracomm object at 0x7f0d1aa84720>}

rank 1: PETSc vector of size 100
rank 0: PETSc vector of size 100
Traceback (most recent call last):
  File "/home/schnack/PhD/pyccel/psydac_codex/psydac/demo_abort_petsc_hang.py", line 71, in <module>
    namespace = parallel_run_from_cli(parse_input_arguments, main)
  File "/home/schnack/PhD/pyccel/psydac_codex/psydac/psydac/utilities/parallel_utils.py", line 95, in parallel_run_from_cli
    namespace.update(main(**args_dict))
                     ~~~~^^^^^^^^^^^^^
  File "/home/schnack/PhD/pyccel/psydac_codex/psydac/demo_abort_petsc_hang.py", line 60, in main
    raise RuntimeError(f'failure on rank {fail_rank}')
RuntimeError: failure on rank 1

And the terminal hangs indefinitely.

Activity

  1. added
    MPI
    CLI
    on Oct 27, 2026
  2. added theissue type on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIMPI

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions