Force GC in WSIReader tests to suppress ResourceWarning for unclosed files#8919
Force GC in WSIReader tests to suppress ResourceWarning for unclosed files#8919vishnukannaujia wants to merge 2 commits into
Conversation
…files
The WSI reader tests emit ResourceWarning ("unclosed file <_io.FileIO ...>"
/ BufferedReader) for the temp TIFFs they exercise (e.g. the CMU-1 generic
TIFF). All direct `reader.read(...)` call sites in this test module already
either use `with` or call `obj.close()` explicitly, so they are not the
leak source.
The remaining leak comes from `LoadImage.__call__`, which calls
`reader.read(filename)` and `reader.get_data(img)`, then returns
`img_array` while letting the reader-returned `img` (the backend handle)
fall out of scope without `close()`. The two `test_with_dataloader*` tests
in this module reach `LoadImage` via `LoadImaged(reader=WSIReader, ...)`
and inherit that leak, so the temp TIFF handles are only closed when the
garbage collector eventually runs - frequently after the warning has
already been emitted.
Add `gc.collect()` to `tearDown` of the shared `WSIReaderTests.Tests`
base class. Each of `TiffFile.__del__` / `OpenSlide.__del__` /
`CuImage.__del__` closes the underlying file descriptor, so running gc
explicitly at the end of every test invokes those finalizers
deterministically and eliminates the warning. This is a focused
test-hygiene change with no impact on production code paths.
Fixes Project-MONAI#5461.
Signed-off-by: Vishnu Kannaujia <vishnu.kannaujia@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/utils/enums/test_wsireader.py (1)
478-486: ⚡ Quick winAdd a Google-style docstring to
tearDown.Line 478 introduces a new definition without a docstring, which violates the file-level Python guideline.
Suggested patch
def tearDown(self): + """Run deterministic cleanup after each test. + + Args: + None. + + Returns: + None. + + Raises: + None. + """ # Force deterministic cleanup of any backend WSI handles that may have # leaked through `LoadImage` (which calls `reader.read` and discards the # returned object after `get_data`). Without this, the temp TIFFs used # by these tests can survive past test teardown long enough for the # interpreter to emit ResourceWarning ("unclosed file ..."). Running gc # here invokes `TiffFile.__del__` / `OpenSlide.__del__` / `CuImage.__del__`, # all of which close the underlying handle. gc.collect()As per coding guidelines, “Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/utils/enums/test_wsireader.py` around lines 478 - 486, The tearDown method is missing a Google-style docstring, which violates the file-level Python guideline. Add a proper Google-style docstring to the tearDown method that describes its purpose. The existing inline comment explaining the deterministic cleanup and garbage collection behavior should be converted into the docstring following Google style conventions, including a summary line describing what the method does. Reference the tearDown method definition when making this change.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/utils/enums/test_wsireader.py`:
- Around line 478-486: The tearDown method is missing a Google-style docstring,
which violates the file-level Python guideline. Add a proper Google-style
docstring to the tearDown method that describes its purpose. The existing inline
comment explaining the deterministic cleanup and garbage collection behavior
should be converted into the docstring following Google style conventions,
including a summary line describing what the method does. Reference the tearDown
method definition when making this change.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f8548496-0b87-42ea-84e4-9275d35f9113
📒 Files selected for processing (1)
tests/utils/enums/test_wsireader.py
Convert the inline comment on the new ``tearDown`` into a Google-style docstring so the method satisfies the repository's docstring guideline, without changing behavior. Signed-off-by: Vishnu Kannaujia <vishnu.kannaujia@gmail.com>
Fixes #5461.
Summary
WSI reader tests emit
ResourceWarning: unclosed file <_io.FileIO ...>/BufferedReaderfor the temp TIFF inputs they exercise (e.g. the CMU-1 generic TIFF). Investigation:reader.read(...)call site intests/utils/enums/test_wsireader.pyalready either useswith reader.read(...) as obj:or explicitly callsobj.close()on the returned WSI object. Those sites are not the leak.LoadImage.__call__inmonai/transforms/io/array.py(around L256–289), which does:test_with_dataloader*tests in this module useLoadImaged(reader=WSIReader, backend=..., ...)and inherit that leak. The temp TIFF handles only get closed when the garbage collector eventually runs, frequently after Python has already emitted the warning.This PR keeps the fix at test-hygiene scope: add
gc.collect()totearDownof the sharedWSIReaderTests.Testsbase class. Each ofTiffFile.__del__/OpenSlide.__del__/CuImage.__del__closes the underlying file descriptor, so running gc explicitly at the end of every test invokes those finalizers deterministically and eliminates the warning.Why not change
LoadImage/BaseWSIReaderA reader-level or
LoadImage-level fix is feasible (e.g. closingimgafterget_datareturns) but is broader in scope, affects all readers, and would need to land alongside changes to the public contract ofBaseWSIReader.read(currently documented to return an open WSI object). Happy to follow up with that if a maintainer prefers; this PR was scoped narrowly to the test symptom that the issue raises.Verify
Expected: tests pass and no
ResourceWarningescalations.