NCAR / ParallelIO

A high-level Parallel I/O Library for structured grid applications
Apache License 2.0
134 stars 52 forks source link

PIO_openfile mode argument can get the wrong default value #1943

Closed billsacks closed 1 year ago

billsacks commented 1 year ago

In PIO_openfile, if mode isn't supplied it uses the default imode. imode is declared like this:

https://github.com/NCAR/ParallelIO/blob/df1c069fad563408a5ffb9104cbe9ee87cb8bb64/src/flib/piolib_mod.F90#L1426

and then set like:

https://github.com/NCAR/ParallelIO/blob/df1c069fad563408a5ffb9104cbe9ee87cb8bb64/src/flib/piolib_mod.F90#L1431

The intent appears to be that it should use a default mode of 0 if mode isn't supplied. However, this initialization of imode in the declaration implicitly gives imode the "save" attribute. This means that, if PIO_openfile is called with (for example) mode = 1, then I believe that future calls to PIO_openfile that do not specify the mode argument will use a default mode of 1 instead of 0.

I believe this problem is responsible for an issue I have been helping with: under some circumstances, the model is crashing with an access error while trying to open a stream file. I think this is because the PIO_openfile call from dshr_stream_mod doesn't specify a mode:

https://github.com/ESCOMP/CDEPS/blob/b7de279948c81812ce6cae7eb45bd49ed405e8fd/streams/dshr_stream_mod.F90#L1515

so the file seems to be opened using whatever mode was last explicitly specified. In this case, the mode is set to 1, which is problematic for this file that does not have world-write permissions.

I have a fix on a branch and will submit a PR shortly.