Issue 243074
Summary [flang-rt] Fortran I/O from a C++ static destructor spins forever with the static runtime
Labels flang:runtime
Assignees
Reporter eugeneepshteyn
    A C++ object with static storage duration whose destructor calls a Fortran procedure that does external I/O (PRINT, WRITE to a unit, OPEN) never terminates when the program is linked with the static flang-rt (`-static-libflangrt`, which is the only flavour in a default flang-rt build). The process spins at 100% CPU after `main` has finished. With the shared flang-rt, and with gfortran (static or shared libgfortran), the same program works.

```cpp
// dtor_io.cpp
extern "C" void report_at_exit();
namespace {
struct Reporter {
  ~Reporter() { report_at_exit(); }
} reporter;
} // namespace
```

```fortran
! dtor_io.f90
subroutine report_at_exit() bind(c)
  print *, 'from static destructor'
end subroutine

program main
  print *, 'main done'
end program
```

```console
$ clang++ -c dtor_io.cpp
$ flang -static-libflangrt dtor_io.f90 dtor_io.o -lstdc++ -o t
$ timeout 20 ./t; echo rc=$?
 main done
rc=124
```

The same thing happens when the program has no Fortran I/O before exit, when it ends with STOP, with `WRITE (0,*)` or OPEN/WRITE/CLOSE in the destructor, and with a C++ `main` that calls into a Fortran library. Internal WRITE is not affected. Reproduced on x86_64 Linux (glibc 2.39) with flang built from 2f6c79128784 and from 68fab9c6089f; the relevant files are unchanged on main as of 563eb6b997c3.

Backtrace of the spinning process (the reproducer above, interrupted in gdb):

```text
#0  ___pthread_mutex_lock (mutex=0x555555621648 <Fortran::runtime::io::createOpenLock>) at ./nptl/pthread_mutex_lock.c:80
#1 Fortran::runtime::io::ExternalFileUnit::LookUpOrCreateAnonymous(int, Fortran::runtime::io::Direction, std::optional<bool>, Fortran::runtime::io::IoErrorHandler&) ()
#2 Fortran::runtime::io::IoStatementState* Fortran::runtime::io::BeginExternalListIO<(Fortran::runtime::io::Direction)0, Fortran::runtime::io::ExternalListIoStatementState>(int, char const*, int) ()
#3  report_at_exit ()
#4  (anonymous namespace)::Reporter::~Reporter() ()
#5  __run_exit_handlers (status=0, ...) at ./stdlib/exit.c:108
#6 __GI_exit (status=<optimized out>) at ./stdlib/exit.c:138
#7 Fortran::runtime::NormalExit(int) ()
#8  _FortranAProgramEndStatement ()
#9 main ()
```

An OPEN in the destructor spins the same way on `unitMapLock`, inside `ExternalFileUnit::LookUpOrCreate`.

Cause:

- `static Lock createOpenLock` and `static Lock unitMapLock` in `flang-rt/lib/runtime/external-unit.cpp` have a non-trivial destructor (`~Lock() { pthread_mutex_destroy(&mutex_); }` in `flang-rt/include/flang-rt/runtime/lock.h`).
- With the static runtime their constructors run after the user's static (link order), so their destructors run before the user's.
- The next external I/O statement takes one of them. `LookUpOrCreateAnonymous` takes `createOpenLock`. `GetUnitMap()` takes `unitMapLock`, because the END statement's `CloseAll()` has already set `unitMap` to null.
- On a destroyed mutex glibc's `pthread_mutex_lock` returns `EINVAL` (the mutex `__kind` is -1), and `Lock::Take()` retries forever: `while (pthread_mutex_lock(&mutex_)) {}`.

`rand_seed_lock`, `timef_lock` and the trampoline `poolLock` are static `Lock`s with the same lifetime.

Possible fixes:

- Make `Lock` constant-initialized (`PTHREAD_MUTEX_INITIALIZER`) with no destructor, or otherwise never destroy the static instances.
- Independently, have `Take()` report an error instead of looping when `pthread_mutex_lock` fails, so that a lifetime bug shows up as a crash rather than a hang.

Workaround for users: construct the object lazily (a function-local static or on first use), or link the shared flang-rt.

Related: 243040. With the shared runtime this destructor path stays alive past `CloseAll()`, and an OPEN from it reaches that crash when `FORT_CONVERT_UNIT` names the unit.

(This issue was filed with the help of AI)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to