h5f_get_obj_count_f Extremely Buggy?

I’m trying to determine all open objects using h5fget_obj_count_f (HDF5 1.12.1). Within HDF5 source (H5_ff.f90) this is accomplished as CALL h5fget_obj_count_f(INT(H5F_OBJ_ALL_F,HID_T), H5F_OBJ_ALL_F, H5OPEN_NUM_OBJ_LOC, error).

The problem is that the returned value H5OPEN_NUM_OBJ_LOC is a function of how many times h5open_f is called. If you call h5open_f once, then call h5fget_obj_count_f, you’ll get a different H5OPEN_NUM_OBJ_LOC than if you call h5open_f two, three, or four times (regardless of whether or not h5close_f is called).

Additionally, any other flag besides H5F_OBJ_ALL_F in h5fget_obj_count_f (e.g. CALL h5fget_obj_count_f(INT(H5F_OBJ_ALL_F,HID_T), H5F_OBJ_GROUP_F, H5OPEN_NUM_OBJ_LOC, error) will return nonsense, regardless of how many times h5open_f is called.

Is h5fget_obj_count_f not the correct way of finding all open objects in an HDF5 under Fortran, or is h5fget_obj_count_f just completely broken?

It looks like a bug where H5close is not resetting the number of open objects, among other issues. I’ll investigate further. Thanks for the report.

https://jira.hdfgroup.org/browse/HDFFV-11306

Also, keep in mind that h5open_f differs from h5open because h5open_f is used to initialize parameters from the C HDF5 library, and it does this by creating various objects, which get closed later when calling h5close_f. Therefore, if you call h5open_f repeatedly, without calling h5close_f, then those objects created will compound with each call. Currently, there is no iteration in h5close_f over open objects if you do this, h5close_f is meant to close only one h5open_f.
Just out of curiosity, why are you calling h5open_f more than once?

The opened objects due to h5open_f is not meant to be included in the open object count.

According to the doc here: https://support.hdfgroup.org/HDF5/doc/RM/RM_H5.html#Library-Open (apparently deprecated, but I don’t see other Fortran documentation) h5open_f is a required call. I assume, as you note, that at a minimum that’s because it’s setting global constants.

It also says that “there are no damaging side-effects in calling it more than once”, which is apparently incorrect?

My current reason for calling it multiple times is library-based; an application might use HDF5 files and then use library that might manipulate its own HDF5 files, and library has no control over the caller’s HDF5 state. Absent a library, I could also imagine it being valuable to not need some kind of global HDF5_OPEN_WAS_CALLED parameter that every routine has to check before calling h5open_f (or alternatively querying some of the global constants that h5open_f is setting, but that seems like a fragile and dangerous workaround).

If calling h5open_f multiple times is not safe, that seems to be a pretty severe limitation in Fortran applications. Having to look through an entire program and all of its dependencies to verify that h5open_f can only ever be called once sounds awful.

That statement applies to the C-APIs H5open and is correct, i.e.,

H5open();

and

H5open();
...
H5open();

behave the same way.

G.

I made a new PR, https://github.com/HDFGroup/hdf5/pull/1657, which fixes it so both h5open_f and h5close_f can be called multiple times. It also fixes an issue with open objects remaining after h5close_f was called.
You can give it a try and see if that works for you.

1 Like

Looks great, thank you!