Skip to content

Commit

Permalink
Reduce bioformats keeping file handles open
Browse files Browse the repository at this point in the history
When bioformats tries to open a zip file, it opens the file once per
internal file.  If it fails to find a file that can be opened within the
zip, it throws an error and leaves all of the file handles open.  By
dividing the reader into more steps, we can ask to open an empty file,
which somehow releases the file handles.
  • Loading branch information
manthey committed Apr 1, 2024
1 parent 9b17d17 commit feb9a9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add a dependency to the zarr source to read more compression types ([#1480](../../pull/1480))
- Guard fetching internal metadata on zarr sources that have less data ([#1481](../../pull/1481))
- Add a method to list registered extensions and mimetypes ([#1488](../../pull/1488))
- Reduce bioformats keeping file handles open ([#1492](../../pull/1492))

### Changes
- Prohibit bioformats from reading zip directly ([#1491](../../pull/1491))
Expand Down
16 changes: 15 additions & 1 deletion sources/bioformats/large_image_source_bioformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,21 @@ def __init__(self, path, **kwargs): # noqa
try:
javabridge.attach()
try:
self._bioimage = bioformats.ImageReader(largeImagePath)
self._bioimage = bioformats.ImageReader(largeImagePath, perform_init=False)
try:
# So this as a separate step so, if it fails, we can ask to
# open something that does not exist and bioformats will
# release some file handles.
self._bioimage.init_reader()
except Exception as exc:
try:
# Ask to open a file that should never exist
self._bioimage.rdr.setId('__\0__')
except Exception:
pass
self._bioimage.close()
self._bioimage = None
raise exc
except (AttributeError, OSError) as exc:
if not os.path.isfile(largeImagePath):
raise TileSourceFileNotFoundError(largeImagePath) from None
Expand Down

0 comments on commit feb9a9f

Please sign in to comment.