-
Notifications
You must be signed in to change notification settings - Fork 297
Protect NetCDF saving from bad Python-vs-HDF file lock timing #6760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trexfeathers
wants to merge
3
commits into
SciTools:main
Choose a base branch
from
trexfeathers:hdf_protection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−2
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
lib/iris/tests/unit/fileformats/netcdf/_thread_safe_nc/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the BSD license. | ||
| # See LICENSE in the root of the repository for full licensing details. | ||
| """Unit tests for the :mod:`iris.fileformats.netcdf._thread_safe_nc` module. | ||
| Not required for a private module, but useful for specific checks. | ||
| """ |
76 changes: 76 additions & 0 deletions
76
lib/iris/tests/unit/fileformats/netcdf/_thread_safe_nc/test_NetCDFWriteProxy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the BSD license. | ||
| # See LICENSE in the root of the repository for full licensing details. | ||
| """Unit tests for :class:`iris.fileformats.netcdf._thread_safe_nc.NetCDFWriteProxy`.""" | ||
|
|
||
| from threading import Lock | ||
|
|
||
| import netCDF4 as nc | ||
| from netCDF4 import Dataset as DatasetOriginal | ||
| import pytest | ||
|
|
||
| from iris.fileformats.netcdf._thread_safe_nc import DatasetWrapper, NetCDFWriteProxy | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dataset_path(tmp_path): | ||
| return tmp_path / "test.nc" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def netcdf_variable(dataset_path): | ||
| dataset = DatasetWrapper(dataset_path, "w") | ||
| _ = dataset.createDimension("dim1", 1) | ||
| variable = dataset.createVariable( | ||
| "test_var", | ||
| "f4", | ||
| ("dim1",), | ||
| ) | ||
| return variable | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def write_proxy(netcdf_variable) -> NetCDFWriteProxy: | ||
| dataset = netcdf_variable.group() | ||
| proxy = NetCDFWriteProxy( | ||
| filepath=dataset.filepath(), | ||
| cf_var=netcdf_variable, | ||
| file_write_lock=Lock(), | ||
| ) | ||
| dataset.close() | ||
| return proxy | ||
|
|
||
|
|
||
| class UnreliableDatasetMaker: | ||
| """A mock operation that returns a Dataset, but fails the first time it is called. | ||
| This simulates non-deterministic HDF locking errors which are difficult to | ||
| debug at the Python layer - pending further investigation. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self.call_count = 0 | ||
|
|
||
| def __call__(self, *args, **kwargs) -> nc.Dataset: | ||
| self.call_count += 1 | ||
| if self.call_count < 2: | ||
| raise OSError("Simulated non-deterministic HDF locking error") | ||
| else: | ||
| return DatasetOriginal(*args, **kwargs) | ||
|
|
||
|
|
||
| def test_handle_hdf_locking_error(dataset_path, monkeypatch, write_proxy): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should probably also have test coverage showing that Iris will bail after 5 attempts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| """Test that NetCDFWriteProxy can handle non-deterministic HDF locking errors.""" | ||
| monkeypatch.setattr(nc, "Dataset", UnreliableDatasetMaker()) | ||
| with pytest.raises(OSError, match="Simulated non-deterministic HDF locking error"): | ||
| dataset = nc.Dataset(write_proxy.path, "r+") | ||
| var = dataset.variables[write_proxy.varname] | ||
| var[0] = 1.0 | ||
|
|
||
| # Reset. | ||
| monkeypatch.setattr(nc, "Dataset", UnreliableDatasetMaker()) | ||
| try: | ||
| write_proxy[0] = 1.0 | ||
| except OSError: | ||
| pytest.fail("NetCDFWriteProxy failed to handle HDF locking error") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.