Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions anylabeling/services/auto_labeling/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,22 @@ def _load_model(self, model_id):

model_config = copy.deepcopy(self.model_configs[model_id])

# Download and extract model
if not model_config.get("has_downloaded", True):
model_config = self._download_and_extract_model(model_config)
if model_config is None:
return

self.model_configs[model_id].update(model_config)
try:
# Download, extract, and initialize under one error boundary. An
# invalid archive or partially downloaded model must not escape the
# Qt worker slot and strand its thread.
if not model_config.get("has_downloaded", True):
model_config = self._download_and_extract_model(model_config)
if model_config is None:
return

model_type = model_config["type"]
model_class = ModelRegistry.get(model_type)
self.model_configs[model_id].update(model_config)

if not model_class:
raise Exception(f"Unknown model type: {model_type}")
model_type = model_config["type"]
model_class = ModelRegistry.get(model_type)
if not model_class:
raise ValueError(f"Unknown model type: {model_type}")

try:
model_config["model"] = model_class(
model_config, on_message=self.new_model_status.emit
)
Expand All @@ -445,9 +446,11 @@ def _load_model(self, model_id):
else:
self.auto_segmentation_model_unselected.emit()

except Exception as e: # noqa
self.new_model_status.emit(self.tr(f"Error in loading model: {str(e)}"))
print(f"Error in loading model: {str(e)}")
except Exception as error: # noqa
logging.exception("Error loading auto-labeling model")
self.new_model_status.emit(
self.tr("Error in loading model: {error}").format(error=error)
)
return

self.loaded_model_config = model_config
Expand Down
10 changes: 8 additions & 2 deletions anylabeling/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot


Expand All @@ -12,5 +14,9 @@ def __init__(self, func, *args, **kwargs):

@pyqtSlot()
def run(self):
self.func(*self.args, **self.kwargs)
self.finished.emit()
try:
self.func(*self.args, **self.kwargs)
except Exception:
logging.exception("Unhandled error in background task")
finally:
self.finished.emit()
32 changes: 32 additions & 0 deletions tests/test_model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ def test_unknown_model_reports_completion(self, mock_load):

self.assertEqual(completions, [{}])

@patch(
"anylabeling.services.auto_labeling.model_manager.ModelManager.load_model_configs"
)
def test_download_error_is_reported_without_escaping_worker(self, mock_load):
manager = ModelManager()
manager.model_configs = [
{
"display_name": "Broken download",
"has_downloaded": False,
"type": "yolov8",
}
]
statuses = []
completions = []
manager.new_model_status.connect(statuses.append)
manager.model_loaded.connect(completions.append)

with (
patch.object(
manager,
"_download_and_extract_model",
side_effect=ValueError("invalid model archive"),
),
patch("anylabeling.services.auto_labeling.model_manager.logging.exception"),
):
result = manager._load_model(0)

self.assertIsNone(result)
self.assertEqual(statuses, ["Error in loading model: invalid model archive"])
manager.on_model_download_finished()
self.assertEqual(completions, [{}])


if __name__ == "__main__":
unittest.main()
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Tests for background worker cleanup."""

import unittest
from unittest import mock

from anylabeling.utils import GenericWorker


class TestGenericWorker(unittest.TestCase):
def test_finished_is_emitted_when_task_raises(self):
finished = []

def fail():
raise RuntimeError("background failure")

worker = GenericWorker(fail)
worker.finished.connect(lambda: finished.append(True))

with mock.patch("anylabeling.utils.logging.exception") as log_exception:
worker.run()

self.assertEqual(finished, [True])
log_exception.assert_called_once_with("Unhandled error in background task")


if __name__ == "__main__":
unittest.main()
Loading