Skip to content

Commit 6536e81

Browse files
klauslerjeanPerier
authored andcommitted
[flang] Don't close stderr in runtime (fixes STOP output)
STOP statement output was sometimes failing to appear because the runtime flushes and shuts down open Fortran units beforehand. But when file descriptor 2 was closed, the STOP statement output was suppressed. The fix is to not actually close file descriptors 0-2 if they are connected to Fortran units being closed. This was already the policy when an OPEN statement was (re-)opening such a unit, so that logic has been pulled out into a member function and shared with CLOSE processing. Differential Revision: https://reviews.llvm.org/D114897
1 parent 93bd239 commit 6536e81

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

flang/runtime/file.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,7 @@ void OpenFile::Open(OpenStatus status, std::optional<Action> action,
6666
(status == OpenStatus::Old || status == OpenStatus::Unknown)) {
6767
return;
6868
}
69-
if (fd_ >= 0) {
70-
if (fd_ <= 2) {
71-
// don't actually close a standard file descriptor, we might need it
72-
} else {
73-
if (::close(fd_) != 0) {
74-
handler.SignalErrno();
75-
}
76-
}
77-
fd_ = -1;
78-
}
69+
CloseFd(handler);
7970
if (status == OpenStatus::Scratch) {
8071
if (path_.get()) {
8172
handler.SignalError("FILE= must not appear with STATUS='SCRATCH'");
@@ -179,12 +170,7 @@ void OpenFile::Close(CloseStatus status, IoErrorHandler &handler) {
179170
break;
180171
}
181172
path_.reset();
182-
if (fd_ >= 0) {
183-
if (::close(fd_) != 0) {
184-
handler.SignalErrno();
185-
}
186-
fd_ = -1;
187-
}
173+
CloseFd(handler);
188174
}
189175

190176
std::size_t OpenFile::Read(FileOffset at, char *buffer, std::size_t minBytes,
@@ -410,6 +396,19 @@ int OpenFile::PendingResult(const Terminator &terminator, int iostat) {
410396
return id;
411397
}
412398

399+
void OpenFile::CloseFd(IoErrorHandler &handler) {
400+
if (fd_ >= 0) {
401+
if (fd_ <= 2) {
402+
// don't actually close a standard file descriptor, we might need it
403+
} else {
404+
if (::close(fd_) != 0) {
405+
handler.SignalErrno();
406+
}
407+
}
408+
fd_ = -1;
409+
}
410+
}
411+
413412
bool IsATerminal(int fd) { return ::isatty(fd); }
414413

415414
#ifdef WIN32

flang/runtime/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class OpenFile {
8585
position_ = pos;
8686
openPosition_.reset();
8787
}
88+
void CloseFd(IoErrorHandler &);
8889

8990
int fd_{-1};
9091
OwningPtr<char> path_;

0 commit comments

Comments
 (0)