https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128875

            Bug ID: 128875
           Summary: REWIND that fails to seek leaves the unit locked: next
                    I/O fails with "Recursive I/O not allowed" or hangs
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.perini at gmail dot com
  Target Milestone: ---

Created attachment 66222
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66222&action=edit
Patch

Reproducer:

  program p
    implicit none
    integer :: ios
    rewind (6, iostat=ios)
    print *, 'after REWIND, iostat =', ios
  end program p

  $ gfortran p.f90 && ./a.out | cat
  At line 5 of file p.f90 (unit = 6, file = 'stdout')
  Fortran runtime error: Recursive I/O not allowed

Also check on Compiler Explorer: https://godbolt.org/z/TrWrYjWEM

Expected: "after REWIND, iostat = 29" (ESPIPE), as with the unit
unlocked.  With "backspace (6, iostat=ios)" in place of the PRINT the
program hangs.  With standard output redirected to a regular file, the
REWIND succeeds and the program prints the line.

Seen with GCC 16.2.0 (Homebrew, aarch64-apple-darwin) and with
master (75dfd8b97da1, 2026-11-18) on x86_64-pc-linux-gnu.  With GCC
15.3.0 (x86_64-pc-linux-gnu) the PRINT hangs instead: the "Recursive
I/O not allowed" check comes from PR 119136, which is in GCC 16 and
reached the gcc-15 branch only after 15.3.

When the seek in st_rewind (libgfortran/io/file_pos.c) fails, st_rewind
calls generate_error and library_end and returns without unlock_unit (u):

          if (sseek (u->s, 0, SEEK_SET) < 0)
            {
              generate_error (&fpp->common, LIBERROR_OS, NULL);
              library_end ();
              return;
            }

Without IOSTAT= or ERR= the program stops there, so nothing else
happens.  With IOSTAT= or ERR= it goes on, and the unit stays locked:

- the next data transfer statement on the unit fails with
  "Fortran runtime error: Recursive I/O not allowed", because
  check_for_recursive finds the unit held by the same thread;
- the next file positioning statement on the unit (REWIND, BACKSPACE,
  ENDFILE) waits forever for the lock in find_unit.

The seek fails on every unit that is neither buffered nor seekable.
fd_to_stream buffers only regular files, so this includes a
preconnected unit on a pipe or a terminal.

Also in st_rewind, needs_unlock starts as true, where st_backspace,
st_endfile and st_flush start it as false.  On a direct-access unit
with asynchronous I/O, the "Cannot REWIND a file opened for DIRECT
access" path therefore unlocks u->au->io_lock without having taken it.

Found while testing the fix for PR 128486: with that fix, the
preconnected units of a Windows process started without standard
handles fail REWIND with EBADF and reach this path.  The bug itself
does not depend on that fix, or on Windows: it shows up whenever a
REWIND cannot seek, as in the reproducer above, which fails with
GCC 16.2.0 without either patch.

A patch follows.

Thank you, 
Federico

Assisted by: Claude Opus 5.5

Reply via email to