<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/126264>126264</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Unexpected runtime I/O error related to finalization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:runtime
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DanielCChen
      </td>
    </tr>
</table>

<pre>
    Consider the following code
```
module m
    type base
        integer*4 :: id

 contains

        procedure :: makeArray => createBaseArray
 final :: finalizeBaseRank1
    end type

    type (base) :: b1_m

 contains

    function createBaseArray (b, n)
        class (base), intent(in) :: b
        integer*4, intent(in) :: n
        class (base), allocatable :: createBaseArray (:)

        allocate (createBaseArray(n))

        createBaseArray%id = b%id
    end function

    subroutine finalizeBaseRank1 (b)
        type(base) :: b(:)

        print *, 'finalizeBaseRank1'
    end subroutine

end module

program ftpbnd516a
use m
  print*, size (b1_m%makeArray (3))
end

```

Flang currently issues a runtime I/O error as:
```
> a.out

fatal Fortran runtime error(/home/cdchen/temp/t.f:28): Recursive I/O attempted on unit 6
 3IOT/Abort trap(coredump)
```

It seems the print statement in the main program got interfered with the print statement in the finalization routine.  
All ifort, gfortran and XLF executed the program successfully.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVVGPozYQ_jXOy-giYgIJDzxwu4e0UqWTTq3Ut2qAIXHP2Mge713u11c2ZDfd7LYoEjGZ-Wbm-74h6L06GaJaFJ9F8bjBwGfr6kc0ivTDw5nMprPDpX6wxquBHPCZYLRa2x_KnKC3A4msEWW2frJmskPQBJPIGgAAvswEHXpaz_FShulETshmDyJvRN6AGiJM1kBvDaMyfj2u1-xsT0NwdI2f8Ds1zuEFRP4o8i_QO0Kmz-iXxzF3VAb1NSEd1K8U8Q3N992KTmZIPb7WSx0LeUxNy-oK0O3-mj7scQymZ2XN2zYSjp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