From 22a1a46a4b87b9cfd7e26fb4a3887898d338e242 Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Fri, 30 Oct 2026 21:39:39 -0700 Subject: [PATCH] refactor(quality): release model gates in finally, not except BaseException --- py/api/autotag_routes.py | 15 ++++++++------- py/api/image_tag_jobs.py | 12 +++++++----- tests/test_init_package.py | 2 +- tests/test_utils_logging_config.py | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/py/api/autotag_routes.py b/py/api/autotag_routes.py index d4038448..9e82d30d 100644 --- a/py/api/autotag_routes.py +++ b/py/api/autotag_routes.py @@ -424,6 +424,7 @@ class AutotagRoutesMixin: title = f"Tagging model download ยท {label}" _download_titles.clear() _download_titles[owner] = title + admitted = False try: job = self._jobs.start( MODEL_DOWNLOAD_JOB_KIND, @@ -439,13 +440,13 @@ class AutotagRoutesMixin: self._run_model_download, service, model_type, owner ), ) - except BaseException as refused: - # JobBusy, or anything else the registry raised: give the gate back. - model_gate.release(owner) - _download_titles.pop(owner, None) - if isinstance(refused, JobBusy): - return None, busy_response(refused) - raise + admitted = True + except JobBusy as refused: + return None, busy_response(refused) + finally: + if not admitted: # JobBusy, or anything else: give the gate back. + model_gate.release(owner) + _download_titles.pop(owner, None) def ended(_task): model_gate.release(owner) diff --git a/py/api/image_tag_jobs.py b/py/api/image_tag_jobs.py index c16751b0..33b679c6 100644 --- a/py/api/image_tag_jobs.py +++ b/py/api/image_tag_jobs.py @@ -531,6 +531,7 @@ class ImageTagJobsMixin: run = AutotagRun( model_type, existing, options, keep_in_memory, backup_on, owner ) + admitted = False try: job = self._jobs.start( AUTOTAG_BATCH_JOB_KIND, @@ -541,11 +542,12 @@ class ImageTagJobsMixin: groups=AUTOTAG_GROUP_NAMES, runner=functools.partial(self._run_autotag_batch, run), ) - except BaseException as refused: - model_gate.release(owner) - if isinstance(refused, JobBusy): - return None, _busy_refusal(refused) - raise + admitted = True + except JobBusy as refused: + return None, _busy_refusal(refused) + finally: + if not admitted: + model_gate.release(owner) job.task.add_done_callback(lambda _task: model_gate.release(owner)) return job, None diff --git a/tests/test_init_package.py b/tests/test_init_package.py index 43661084..01ab75b5 100644 --- a/tests/test_init_package.py +++ b/tests/test_init_package.py @@ -527,7 +527,7 @@ class TestPackageLoggerIsolation(unittest.TestCase): def log_from_background_thread(): try: logging.getLogger("prompt_manager.late_emitter").warning(message) - except BaseException as error: + except Exception as error: errors.append(error) self.handler.addFilter(pause_before_handler_lock) diff --git a/tests/test_utils_logging_config.py b/tests/test_utils_logging_config.py index d64ee51e..4d1cf609 100644 --- a/tests/test_utils_logging_config.py +++ b/tests/test_utils_logging_config.py @@ -998,7 +998,7 @@ class TestConfiguration(IsolatedLoggerTestCase): def log_from_background_thread() -> None: try: self.manager.logger.info(message) - except BaseException as error: + except Exception as error: errors.append(error) # Allow this record through the fixture's normal main-thread filter.