Ifort 2021.7 reusing file units despite newunit specifier

Hi all,

I have a large program that’s been converted to using the newunit specifier for most file I/O applications. The program runs fine compiled with ifort 16, ifort 19, and gfortran 12. It crashes while running when compiled with ifort 2021.7, however. It seems to be reusing units, with 2 files pointing to -129 and 2 pointing to unit -131.

Has anyone else encountered this? Is this a known compiler bug, or am I doing something wrong?

You may perhaps want to ask here also: https://community.intel.com/t5/Intel-Fortran-Compiler/bd-p/fortran-compiler

And consider searching discussion threads there for newunit and reviewing them, there have been some discussions in the recent past but dunno where things are now with Intel Fortran.

Do you have a simple reproducer? It is not clear if the file was closed after being opened, and subsequently the number was used, for example. That other compilers worked points towards a bug, but not conclusively.

I couldn’t get a good enough handle on what was going on to be able to write a simple reproducer unfortunately. For now, I seem to have gotten around the problem by replacing a bit of code that was essentially this:

close (unit1)
close (unit2)
open(newunit =unit1,...)
open(newunit =unit2,...)

With an inquire to see if the file is already open, and if so rewind and use the existing unit rather than have any subsequent calls to newunit.

Is there any chance you are trying to open two units with the same file name? That is not allowed, but I don’t think the compiler is required to detect that, so it is the programmer’s responsibility.

I’m reasonably sure that’s not happening, but it’s not completely out of the question. The reason I’m somewhat leaning toward compiler bug is that unit -129 is successfully opened and written to for a while before the program crashes, and -129 is a file that’s never closed during the life of the program. But it seems like a different file (different name) is later re-opened using the already connected unit, and then that file is closed.

That said, it’s so rarely a compiler bug that it’s hard to imagine it was. But I know there were recent changes to newunit in ifort based on comments on the Intel forum.

I’ll have to do a bit more digging, but for now I’m planning to just wrap my open statements with the inquire/rewind.

It is sometimes alright to open() a file more than once, depending on what exactly you do with the open statement. For example, you can change the file position this way. But there are other attributes that you cannot change without first closing the file. I always have to look up what is and isn’t allowed. I think newunit is one of the things that you can’t do this way.

@bwe,

Perhaps this recent comment at the Intel Fortran forum may be of some help to you?

If not, you may want to look into a small reproducer like that other user and check if you can get closer to the root of the problem.