Skip to content

Commit 8c9c5e1

Browse files
committed
Don't barrier() when we're unwinding an exception
See libMesh/libmesh#4351 for more context.
1 parent 06cd520 commit 8c9c5e1

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/utilities/src/timpi_init.C

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "timpi/communicator.h"
2626
#include "timpi/timpi_assert.h"
2727

28+
#ifdef TIMPI_ENABLE_EXCEPTIONS
29+
#include <exception> // For std::uncaught_exceptions
30+
#endif
2831

2932
#ifdef TIMPI_HAVE_MPI
3033
void TIMPI_MPI_Handler (MPI_Comm *, int *, ...)
@@ -119,14 +122,21 @@ TIMPIInit::TIMPIInit (int /* argc */, const char * const * /* argv */,
119122
TIMPIInit::~TIMPIInit()
120123
{
121124
// Every processor had better be ready to exit at the same time.
122-
// This would be a timpi_parallel_only() function, except that
123-
// timpi_parallel_only() uses timpi_assert() which throws an
124-
// exception which causes compilers to scream about exceptions
125-
// inside destructors.
126-
127125
// Even if we're not doing parallel_only debugging, we don't want
128126
// one processor to try to exit until all others are done working.
129-
this->comm().barrier();
127+
128+
// We could be destructing here because we're unwinding the stack
129+
// due to a thrown exception, though. It's possible that an
130+
// application is catching exceptions outside of the LibMeshInit
131+
// scope, or that we're using a C++ compiler that does unwinding
132+
// for uncaught exceptions (the standard says whether to go straight
133+
// to terminate() or unwind first is "implementation-defined"). If
134+
// *that* is the case then we can't safely communicate with other
135+
// processors that might not all be unwinding too.
136+
#ifdef TIMPI_ENABLE_EXCEPTIONS
137+
if (!std::uncaught_exceptions())
138+
#endif
139+
this->comm().barrier();
130140

131141
// Trigger any SemiPermanent cleanup before potentially finalizing MPI
132142
_ref.reset();

0 commit comments

Comments
 (0)