Skip to content

Commit 62ece9c

Browse files
authored
[flang][runtime] Add a critical section for LookUpOrCreateAnonymous. (#74468)
In parallel regions `LookUpOrCreateAnonymous` may return an anonymous unit that has not yet been opened, which may cause a runtime error. This commit ensures that the returned anonymous unit has been opened. For details see: #68856 (comment) Fixes #68856
1 parent 9fa3455 commit 62ece9c

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

flang/runtime/unit.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Fortran::runtime::io {
2020
// The per-unit data structures are created on demand so that Fortran I/O
2121
// should work without a Fortran main program.
2222
static Lock unitMapLock;
23+
static Lock createOpenLock;
2324
static UnitMap *unitMap{nullptr};
2425
static ExternalFileUnit *defaultInput{nullptr}; // unit 5
2526
static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
@@ -52,6 +53,9 @@ ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
5253
ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
5354
Direction dir, std::optional<bool> isUnformatted,
5455
const Terminator &terminator) {
56+
// Make sure that the returned anonymous unit has been opened
57+
// not just created in the unitMap.
58+
CriticalSection critical{createOpenLock};
5559
bool exists{false};
5660
ExternalFileUnit *result{
5761
GetUnitMap().LookUpOrCreate(unit, terminator, exists)};

0 commit comments

Comments
 (0)