[flang-commits] [PATCH] D127032: [flang][runtime] Fix deadlock in error recovery

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jun 3 16:10:01 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

When an external I/O statement is in a recoverable error
state before any data transfers take place (for example,
an unformatted transfer with ERR=/IOSTAT=/IOMSG= attempted on
a formatted unit), ensure that the unit's mutex is still
released at the end of the statement.


https://reviews.llvm.org/D127032

Files:
  flang/runtime/io-api.cpp
  flang/runtime/io-stmt.cpp
  flang/runtime/io-stmt.h


Index: flang/runtime/io-stmt.h
===================================================================
--- flang/runtime/io-stmt.h
+++ flang/runtime/io-stmt.h
@@ -711,9 +711,10 @@
 
 class ErroneousIoStatementState : public IoStatementBase {
 public:
-  explicit ErroneousIoStatementState(
-      Iostat iostat, const char *sourceFile = nullptr, int sourceLine = 0)
-      : IoStatementBase{sourceFile, sourceLine} {
+  explicit ErroneousIoStatementState(Iostat iostat,
+      ExternalFileUnit *unit = nullptr, const char *sourceFile = nullptr,
+      int sourceLine = 0)
+      : IoStatementBase{sourceFile, sourceLine}, unit_{unit} {
     SetPendingError(iostat);
   }
   int EndIoStatement();
@@ -722,6 +723,7 @@
 
 private:
   ConnectionState connection_;
+  ExternalFileUnit *unit_{nullptr};
 };
 
 extern template bool IoStatementState::EmitEncoded<char>(
Index: flang/runtime/io-stmt.cpp
===================================================================
--- flang/runtime/io-stmt.cpp
+++ flang/runtime/io-stmt.cpp
@@ -1518,6 +1518,9 @@
 
 int ErroneousIoStatementState::EndIoStatement() {
   SignalPendingError();
+  if (unit_) {
+    unit_->EndIoStatement();
+  }
   return IoStatementBase::EndIoStatement();
 }
 
Index: flang/runtime/io-api.cpp
===================================================================
--- flang/runtime/io-api.cpp
+++ flang/runtime/io-api.cpp
@@ -172,7 +172,7 @@
           *child, sourceFile, sourceLine);
     } else {
       return &child->BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, nullptr /* no unit */, sourceFile, sourceLine);
     }
   } else {
     if (iostat == IostatOk && unit.access == Access::Direct) {
@@ -186,7 +186,7 @@
           std::forward<A>(xs)..., unit, sourceFile, sourceLine);
     } else {
       return &unit.BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, &unit, sourceFile, sourceLine);
     }
   }
 }
@@ -228,7 +228,7 @@
           *child, format, formatLength, sourceFile, sourceLine);
     } else {
       return &child->BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, nullptr /* no unit */, sourceFile, sourceLine);
     }
   } else {
     if (iostat == IostatOk) {
@@ -239,7 +239,7 @@
           unit, format, formatLength, sourceFile, sourceLine);
     } else {
       return &unit.BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, &unit, sourceFile, sourceLine);
     }
   }
 }
@@ -280,7 +280,7 @@
           *child, sourceFile, sourceLine);
     } else {
       return &child->BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, nullptr /* no unit */, sourceFile, sourceLine);
     }
   } else {
     if (iostat == IostatOk) {
@@ -301,7 +301,7 @@
       return &io;
     } else {
       return &unit.BeginIoStatement<ErroneousIoStatementState>(
-          iostat, sourceFile, sourceLine);
+          iostat, &unit, sourceFile, sourceLine);
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127032.434192.patch
Type: text/x-patch
Size: 3127 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220603/9b3671a3/attachment.bin>


More information about the flang-commits mailing list