Skip to content

Commit 4a54359

Browse files
committed
fix(dataset): ensure proper handling of LMDB read environment and prevent access errors
Signed-off-by: R. Garcia-Dias <rafaelagd@gmail.com>
1 parent b5b1eff commit 4a54359

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

monai/data/dataset.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def __init__(
609609
# the cache is created without multi-threading
610610
self._read_env: Any | None = None
611611
# this runs on the primary thread/process
612-
self._fill_cache_start_reader(show_progress=self.progress)
612+
self._read_env = self._fill_cache_start_reader(show_progress=self.progress)
613613
print(f"Accessing lmdb file: {self.db_file.absolute()}.")
614614

615615
def set_data(self, data: Sequence):
@@ -637,6 +637,12 @@ def _fill_cache_start_reader(self, show_progress=True):
637637
Args:
638638
show_progress: whether to show the progress bar if possible.
639639
"""
640+
# Close any open read environment before attempting write-mode access
641+
# to prevent "environment already open" errors when multiple LMDBDataset
642+
# instances target the same db file
643+
if self._read_env is not None:
644+
self._read_env.close()
645+
self._read_env = None
640646
# create cache
641647
self.lmdb_kwargs["readonly"] = False
642648
env = lmdb.open(path=f"{self.db_file}", subdir=False, **self.lmdb_kwargs)
@@ -664,7 +670,7 @@ def _fill_cache_start_reader(self, show_progress=True):
664670
size = env.info()["map_size"]
665671
new_size = size * 2
666672
warnings.warn(
667-
f"Resizing the cache database from {int(size) >> 20}MB" f" to {int(new_size) >> 20}MB."
673+
f"Resizing the cache database from {int(size) >> 20}MB to {int(new_size) >> 20}MB."
668674
)
669675
env.set_mapsize(new_size)
670676
except lmdb.MapResizedError:

tests/transforms/test_spacing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def _template_5_expected_output(device: torch.device) -> torch.Tensor:
3636
return _TEMPLATE_5_COMPILED
3737
return _TEMPLATE_5_NATIVE
3838

39+
3940
all_template_parts = [
4041
[
4142
{"pixdim": (1.0, 1.5), "padding_mode": "zeros", "dtype": float},

versioneer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
429429
return stdout, process.returncode
430430

431431

432-
LONG_VERSION_PY[
433-
"git"
434-
] = r'''
432+
LONG_VERSION_PY["git"] = r'''
435433
# This file helps to compute a version number in source trees obtained from
436434
# git-archive tarball (such as those provided by githubs download-from-tag
437435
# feature). Distribution tarballs (built by setup.py sdist) and build

0 commit comments

Comments
 (0)