Skip to content

Improved conditions for closing nwb when using hdf5 backend#3150

Merged
alejoe91 merged 2 commits intoSpikeInterface:mainfrom
h-mayorquin:close_nwb
Jul 8, 2024
Merged

Improved conditions for closing nwb when using hdf5 backend#3150
alejoe91 merged 2 commits intoSpikeInterface:mainfrom
h-mayorquin:close_nwb

Conversation

@h-mayorquin
Copy link
Copy Markdown
Collaborator

So we have been battling this for a while. Our access pattern of nwb when using the hdf5 backend directly (instead of the pynwb API) leaves files references dangling.

When looking a this, I see that we are keeping references to some groups and datasets even after closing the main reference. I am not sure if it is the weak reference or the way we access some properties were we don't copy at read.

At some point I have to look deeply into this but meanwhile this should alleviate some of the problems. This uses the low level API of h5py to access all the references to a particular id and tries to close them when calling del. It did solve my issue that I was facing but I am too skeptical to think this is the final nail on the coffin.

@h-mayorquin h-mayorquin added the extractors Related to extractors module label Jul 4, 2024
@h-mayorquin h-mayorquin self-assigned this Jul 4, 2024
@h-mayorquin h-mayorquin marked this pull request as ready for review July 4, 2024 22:18
Comment on lines +629 to +643
def _close_hdf5_file(self):
has_hdf5_backend = hasattr(self, "_file")
if has_hdf5_backend:
import h5py

main_file_id = self._file.id
open_object_ids_main = h5py.h5f.get_obj_ids(main_file_id, types=h5py.h5f.OBJ_ALL)
for object_id in open_object_ids_main:
object_name = h5py.h5i.get_name(object_id).decode("utf-8")
try:
object_id.close()
except:
import warnings

warnings.warn(f"Error closing object {object_name}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is the same function as the Sorting object, why not making a separate unique function?

def _close_hdf5_file(extraxctor):
    has_hdf5_backend = hasattr(extraxctor, "_file")
    ....

then you would just call it in the __del__ with _close_hdf5_file(self)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right that this repetition is unecessary but I wanted to use self, check out the latest commit. I implemented a mixing class that should hold the methods that need the state of the nwb extractor and are common to both recording and sorting.

@alejoe91 alejoe91 added this to the 0.101.0 milestone Jul 5, 2024
Copy link
Copy Markdown
Member

@alejoe91 alejoe91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Ramon, see comment

@h-mayorquin h-mayorquin changed the title Improved better conditions for closing nwb when using hdf5 backend Improved conditions for closing nwb when using hdf5 backend Jul 5, 2024
@alejoe91 alejoe91 merged commit 52372a6 into SpikeInterface:main Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extractors Related to extractors module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants