Skip to content

Commit 1e623c4

Browse files
vchuravysimonbyrne
andauthored
only finalize in the case that jl_current_exception is unset (#390)
* only finalize in the case that jl_current_exception is unset * add tests Co-authored-by: Simon Byrne <[email protected]>
1 parent 7eaf42d commit 1e623c4

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/environment.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ For more details, see [Finalizers](@ref).
4949
function refcount_dec()
5050
# refcount zero, all objects finalized, now finalize MPI
5151
if Threads.atomic_sub!(REFCOUNT, 1) == 1
52-
!Finalized() && _Finalize()
52+
if !Finalized()
53+
# MPI can now be finalized, but MPI_Finalize is a collective and can act
54+
# like a barrier (this may be implementation specific), if we are terminating
55+
# due to a Julia exception, we should calling MPI_Finalize. We thus peek at the
56+
# current exception, and only if that field is nothing do we terminate.
57+
if ccall(:jl_current_exception, Any, ()) === nothing
58+
_Finalize()
59+
end
60+
end
5361
end
5462
end
5563

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function runtests()
2222

2323
nfail = 0
2424
printstyled("Running MPI.jl tests\n"; color=:white)
25-
25+
2626
for f in testfiles
2727
mpiexec() do cmd
2828
cmd = `$cmd $args`
@@ -32,6 +32,9 @@ function runtests()
3232
withenv("JULIA_NUM_THREAD" => "4") do
3333
run(`$cmd -n $nprocs $(Base.julia_cmd()) $(joinpath(testdir, f))`)
3434
end
35+
elseif f == "test_error.jl"
36+
r = run(ignorestatus(`$cmd -n $nprocs $(Base.julia_cmd()) $(joinpath(testdir, f))`))
37+
@test !success(r)
3538
else
3639
run(`$cmd -n $nprocs $(Base.julia_cmd()) $(joinpath(testdir, f))`)
3740
end

test/test_error.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using MPI
2+
function main()
3+
MPI.Init()
4+
comm = MPI.COMM_WORLD
5+
rank = MPI.Comm_rank(comm)
6+
if rank == 1
7+
error("Goodbye")
8+
end
9+
MPI.Barrier(comm)
10+
end
11+
main()

0 commit comments

Comments
 (0)