Skip to content

Commit

Permalink
fix #94 (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbyrohl authored Sep 14, 2023
1 parent 29e0c73 commit 29d1b3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/scida/io/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,16 @@ def create_cachefile(
hf.attrs["_cachingcomplete"] = True # mark that caching complete

def load_cachefile(self, location, token="", chunksize="auto", **kwargs):
self.file = h5py.File(location, "r")
try:
self.file = h5py.File(location, "r")
except (
OSError
) as e: # file potentially corrupted, raise InvalidCacheError and potentially recover
raise InvalidCacheError(
"""Cache file '%s' might be invalid. An OSError '%s' was raised.
Delete file and try again."""
% (location, str(e))
)
hf = self.file
cache_valid = False
if "_cachingcomplete" in hf.attrs:
Expand Down
20 changes: 19 additions & 1 deletion tests/test_convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def signal_handler(signum, frame):
with time_limit(dt_int):
# signal just accepts integer seconds... so we work around it with sleep before
time.sleep(dt_int - dt)

load(testdatapath, **loadkwargs)

# count total files by walking folders without counting those
Expand Down Expand Up @@ -191,3 +190,22 @@ def signal_handler(signum, frame):
# attempt load and recover.
load(testdatapath, **loadkwargs)
assert "Invalid cache file, attempting to create new one." in caplog.text


@require_testdata_path("interface", only=["TNG50-4_snapshot"])
def test_load_cachefail_oserror(cachedir, testdatapath, caplog):
"""Test recovery from failure during cache creation. Do so by keeping the file is kept open."""

# explicitly disable catalog discovery
loadkwargs = dict(catalog="none")
ds = load(testdatapath, **loadkwargs)
filename = ds.file.filename
ds.file.close()

# Let's destroy the hdf5 header with random data
with open(filename, "r+b") as f:
f.seek(0)
f.write(b"destroyheader")

ds = load(testdatapath, **loadkwargs)
print(ds.info())

0 comments on commit 29d1b3a

Please sign in to comment.