-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(ZarrAvgMerger): use separate tmpdir attrs to prevent value_store tmpdir GC on zarr v3 #8816
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
Zeesejo
wants to merge
1
commit into
Project-MONAI:dev
Choose a base branch
from
Zeesejo:fix/zarr-avg-merger-tmpdir-overwrite
base: dev
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,32 +286,33 @@ def __init__( | |
| self.value_dtype = value_dtype | ||
| self.count_dtype = count_dtype | ||
| self.store = store | ||
| self.tmpdir: TemporaryDirectory | None | ||
| # Use separate tmpdir attributes so both TemporaryDirectory objects remain alive | ||
| # for the full lifetime of this instance. Previously a single `self.tmpdir` was | ||
| # overwritten when both value_store and count_store were None, causing the first | ||
| # TemporaryDirectory to be garbage-collected immediately. | ||
| self.value_tmpdir: TemporaryDirectory | None = None | ||
| self.count_tmpdir: TemporaryDirectory | None = None | ||
|
|
||
| # Handle zarr v3 vs older versions | ||
| is_zarr_v3 = version_geq(get_package_version("zarr"), "3.0.0") | ||
|
|
||
| if is_zarr_v3: | ||
| if value_store is None: | ||
| self.tmpdir = TemporaryDirectory() | ||
| self.value_store = zarr.storage.LocalStore(self.tmpdir.name) # type: ignore | ||
| self.value_tmpdir = TemporaryDirectory() | ||
| self.value_store = zarr.storage.LocalStore(self.value_tmpdir.name) # type: ignore | ||
| else: | ||
| self.value_store = value_store # type: ignore | ||
| if count_store is None: | ||
| self.tmpdir = TemporaryDirectory() | ||
| self.count_store = zarr.storage.LocalStore(self.tmpdir.name) # type: ignore | ||
| self.count_tmpdir = TemporaryDirectory() | ||
| self.count_store = zarr.storage.LocalStore(self.count_tmpdir.name) # type: ignore | ||
| else: | ||
| self.count_store = count_store # type: ignore | ||
| else: | ||
| self.tmpdir = None | ||
| self.value_store = zarr.storage.TempStore() if value_store is None else value_store # type: ignore | ||
| self.count_store = zarr.storage.TempStore() if count_store is None else count_store # type: ignore | ||
|
|
||
| self.chunks = chunks | ||
|
|
||
| # Handle compressor/codecs based on zarr version | ||
| is_zarr_v3 = version_geq(get_package_version("zarr"), "3.0.0") | ||
|
|
||
| # Initialize codecs/compressor attributes with proper types | ||
| self.codecs: list | None = None | ||
| self.value_codecs: list | None = None | ||
|
|
@@ -322,50 +323,28 @@ def __init__( | |
| if codecs is not None: | ||
| self.codecs = codecs | ||
| elif compressor is not None: | ||
| # Convert compressor to codec format | ||
| if isinstance(compressor, (list, tuple)): | ||
| self.codecs = compressor | ||
| else: | ||
| self.codecs = [compressor] | ||
| self.codecs = compressor if isinstance(compressor, (list, tuple)) else [compressor] | ||
| else: | ||
| self.codecs = None | ||
|
|
||
| if value_codecs is not None: | ||
| self.value_codecs = value_codecs | ||
| elif value_compressor is not None: | ||
| if isinstance(value_compressor, (list, tuple)): | ||
| self.value_codecs = value_compressor | ||
| else: | ||
| self.value_codecs = [value_compressor] | ||
| self.value_codecs = value_compressor if isinstance(value_compressor, (list, tuple)) else [value_compressor] | ||
| else: | ||
| self.value_codecs = None | ||
|
|
||
| if count_codecs is not None: | ||
| self.count_codecs = count_codecs | ||
| elif count_compressor is not None: | ||
| if isinstance(count_compressor, (list, tuple)): | ||
| self.count_codecs = count_compressor | ||
| else: | ||
| self.count_codecs = [count_compressor] | ||
| self.count_codecs = count_compressor if isinstance(count_compressor, (list, tuple)) else [count_compressor] | ||
| else: | ||
| self.count_codecs = None | ||
| else: | ||
| # For zarr v2, use compressors | ||
| if codecs is not None: | ||
| # If codecs are specified in v2, use the first codec as compressor | ||
| self.codecs = codecs[0] if isinstance(codecs, (list, tuple)) else codecs | ||
| else: | ||
| self.codecs = compressor # type: ignore[assignment] | ||
|
|
||
| if value_codecs is not None: | ||
| self.value_codecs = value_codecs[0] if isinstance(value_codecs, (list, tuple)) else value_codecs | ||
| else: | ||
| self.value_codecs = value_compressor # type: ignore[assignment] | ||
|
|
||
| if count_codecs is not None: | ||
| self.count_codecs = count_codecs[0] if isinstance(count_codecs, (list, tuple)) else count_codecs | ||
| else: | ||
| self.count_codecs = count_compressor # type: ignore[assignment] | ||
| self.codecs = codecs[0] if isinstance(codecs, (list, tuple)) else codecs if codecs is not None else compressor # type: ignore[assignment] | ||
| self.value_codecs = value_codecs[0] if isinstance(value_codecs, (list, tuple)) else value_codecs if value_codecs is not None else value_compressor # type: ignore[assignment] | ||
| self.count_codecs = count_codecs[0] if isinstance(count_codecs, (list, tuple)) else count_codecs if count_codecs is not None else count_compressor # type: ignore[assignment] | ||
|
Comment on lines
+345
to
+347
Member
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 think this should be reverted to the original code as expressions with multiple |
||
|
|
||
| # Create zarr arrays with appropriate parameters based on version | ||
| if is_zarr_v3: | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard empty codec lists in the v2 fallback.
codecs[0],value_codecs[0], andcount_codecs[0]will raiseIndexErrorfor[]. Please treat an empty sequence asNoneor raise a targetedValueError, and add a regression test for that path. As per coding guidelines, "Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Ensure new or modified definitions will be covered by existing or new unit tests."Suggested fix
🤖 Prompt for AI Agents