From fede45b0e663eb171b980ff4c105a02263e99e14 Mon Sep 17 00:00:00 2001 From: Ishan Dutta Date: Mon, 20 Nov 2023 08:02:39 +0530 Subject: [PATCH] :hammer: [dataset] Handle corrupted cached file in PersistentDataset (#7244) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #5723 ### Description Corrupted cached files in the PersistentDataset cause the exception: `RuntimeError: Invalid magic number; corrupt file?` With this PR we handle that case in the try-except block and continue the usual functionality if the cached file was absent. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [x] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Ishan Dutta Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Signed-off-by: Juan Pablo de la Cruz GutiƩrrez --- monai/data/dataset.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/monai/data/dataset.py b/monai/data/dataset.py index 5e403d6fdb..eba850225d 100644 --- a/monai/data/dataset.py +++ b/monai/data/dataset.py @@ -387,6 +387,12 @@ def _cachecheck(self, item_transformed): except PermissionError as e: if sys.platform != "win32": raise e + except RuntimeError as e: + if "Invalid magic number; corrupt file" in str(e): + warnings.warn(f"Corrupt cache file detected: {hashfile}. Deleting and recomputing.") + hashfile.unlink() + else: + raise e _item_transformed = self._pre_transform(deepcopy(item_transformed)) # keep the original hashed if hashfile is None: