Skip to content

Conversation

dschwoerer
Copy link
Contributor

@dschwoerer dschwoerer commented Sep 24, 2025

It did run the test 100 times without issues. Before that every ~1 out of 3 runs failed

@dschwoerer
Copy link
Contributor Author

Note that this only a partial fix, for the netCDF4 backend, the issue still remains, but with this fix at least we can switch to h5netcdf to have a working solution.

Comment on lines 234 to 238
# Check for string so we do not have to import it
if str(type(file)) == "<class 'netCDF4._netCDF4.Dataset'>":
with NETCDF4_PYTHON_LOCK:
file.close()
return
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a parameter to FileManager (and also probably PickleableFileManager) in a way that allows this logic to stay only in the backend class?

I'm imagining adding a parameter closer, which is used to close files like closer(file).

Copy link
Member

Choose a reason for hiding this comment

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

Could also call this close but elsewhere in file_manager this is used for functions with a signature like close(), which could be a bit ambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed when can just store the lock in the FileManager, then that does not require significant changes, including making it self-contained, without needing a closer function

@dschwoerer dschwoerer force-pushed the netcdf4-locking-closing branch from edcbfc2 to e7c84a7 Compare September 26, 2025 09:06
self._args = args
self._mode = "a" if mode == "w" else mode
self._kwargs = kwargs
self._lock = lock
Copy link
Member

Choose a reason for hiding this comment

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

The rest of PickleableFileManager needs updates to use lock, at least in the close and __getstate__/__setstate__ methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure about __getstate__ and __setstate__ - for __getitem__ and __setitem__ the netCDF4 backend is doing the locking.
Are you sure we need to lock for them?

If we would switch to RLocks, we would need to be less strict about these checks ...

I will change it to use a lock in close 👍

Copy link
Member

Choose a reason for hiding this comment

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

__getstate__/__setstate__ are methods for pickling the file manager object. We definitely need to add lock there, otherwise the lock will be dropped when the file manager is pickled/unpickled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

segfault while closing netcdf files
2 participants