Skip to content

Conversation

klausler
Copy link
Contributor

When an OPEN statement fails, a unit that was created for the OPEN needs to be removed from the unit map. The code that tried to do this was incorrect -- it needs to re-acquire the unit via LookUpForClose as a CLOSE statement does. (The failure to do this completely was leaving a zombie unit active that could break a later OPEN on the same unit number.)

@klausler klausler requested a review from jeanPerier September 17, 2024 15:37
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Sep 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2024

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

When an OPEN statement fails, a unit that was created for the OPEN needs to be removed from the unit map. The code that tried to do this was incorrect -- it needs to re-acquire the unit via LookUpForClose as a CLOSE statement does. (The failure to do this completely was leaving a zombie unit active that could break a later OPEN on the same unit number.)


Full diff: https://github.com/llvm/llvm-project/pull/108994.diff

1 Files Affected:

  • (modified) flang/runtime/io-stmt.cpp (+5-2)
diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 265bd0dc9d9499..cd7a196335d31e 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -329,8 +329,11 @@ void OpenStatementState::CompleteOperation() {
   }
   if (!wasExtant_ && InError()) {
     // Release the new unit on failure
-    unit().CloseUnit(CloseStatus::Delete, *this);
-    unit().DestroyClosed();
+    if (ExternalFileUnit *
+        toClose{unit().LookUpForClose(unit().unitNumber())}) {
+      toClose->Close(CloseStatus::Delete, *this);
+      toClose->DestroyClosed();
+    }
   }
   IoStatementBase::CompleteOperation();
 }

When an OPEN statement fails, a unit that was created for the OPEN
needs to be removed from the unit map.  The code that tried to do
this was incorrect -- it needs to re-acquire the unit via LookUpForClose
as a CLOSE statement does.  (The failure to do this completely was
leaving a zombie unit active that could break a later OPEN on the
same unit number.)
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@klausler klausler merged commit 5f11d38 into llvm:main Sep 18, 2024
8 checks passed
@klausler klausler deleted the bug36355 branch September 18, 2024 19:19
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
When an OPEN statement fails, a unit that was created for the OPEN needs
to be removed from the unit map. The code that tried to do this was
incorrect -- it needs to re-acquire the unit via LookUpForClose as a
CLOSE statement does. (The failure to do this completely was leaving a
zombie unit active that could break a later OPEN on the same unit
number.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants