From 0eb98cbdaf4f4807623404e799c5f1bf2d4e7393 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Thu, 1 Feb 2024 11:50:34 -0500 Subject: [PATCH] Retry read_region if openslide reports an error --- CHANGELOG.md | 1 + .../large_image_source_openslide/__init__.py | 23 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b1bf26b..c4300f3d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Infer DICOM file size, when possible ([#1448](../../pull/1448)) - Swap styles faster in the frame viewer ([#1452](../../pull/1452)) - Reduce color fringing in multi source compositing ([#1456](../../pull/1456)) +- Retry read_region if openslide reports an error ([#1457](../../pull/1457)) ### Bug Fixes - Fix an issue with compositing sources in the multi source caused by alpha range ([#1453](../../pull/1453)) diff --git a/sources/openslide/large_image_source_openslide/__init__.py b/sources/openslide/large_image_source_openslide/__init__.py index 488e238bf..ad6875da5 100644 --- a/sources/openslide/large_image_source_openslide/__init__.py +++ b/sources/openslide/large_image_source_openslide/__init__.py @@ -297,14 +297,21 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs): if svslevel['scale'] > 2 ** self._maxSkippedLevels: tile = self._getTileFromEmptyLevel(x, y, z, **kwargs) else: - try: - tile = self._openslide.read_region( - (offsetx, offsety), svslevel['svslevel'], - (self.tileWidth * svslevel['scale'], - self.tileHeight * svslevel['scale'])) - except openslide.lowlevel.OpenSlideError as exc: - raise TileSourceError( - 'Failed to get OpenSlide region (%r).' % exc) + retries = 3 + while retries > 0: + try: + tile = self._openslide.read_region( + (offsetx, offsety), svslevel['svslevel'], + (self.tileWidth * svslevel['scale'], + self.tileHeight * svslevel['scale'])) + break + except openslide.lowlevel.OpenSlideError as exc: + # Reopen handle after a lowlevel error + self._openslide = openslide.OpenSlide(self._largeImagePath) + retries -= 1 + if retries <= 0: + raise TileSourceError( + 'Failed to get OpenSlide region (%r).' % exc) # Always scale to the svs level 0 tile size. if svslevel['scale'] != 1: tile = tile.resize((self.tileWidth, self.tileHeight),