Skip to content

fix: Add thread safety to session initialization in ModelRotationDetector#54

Merged
duartebarbosadev merged 3 commits intomainfrom
fix--auto-rotation-not-showing-model-missing-dialog
Oct 25, 2025
Merged

fix: Add thread safety to session initialization in ModelRotationDetector#54
duartebarbosadev merged 3 commits intomainfrom
fix--auto-rotation-not-showing-model-missing-dialog

Conversation

@duartebarbosadev
Copy link
Copy Markdown
Owner

This pull request improves the thread safety and error handling of the model rotation detector, particularly around loading the model session. The main changes ensure that model loading is protected by a lock to prevent race conditions, and that missing model errors are surfaced immediately and more clearly, allowing for better user feedback and recovery.

Thread safety and concurrency improvements:

  • Added a threading.Lock (_session_init_lock) to the singleton instance to ensure that model session loading is thread-safe and prevents duplicate loads in concurrent scenarios. [1] [2] [3]

Error handling and user feedback:

  • Changed the behavior when the model file is missing: instead of marking the detector as permanently failed, now raises a ModelNotFoundError with a best-effort path hint, allowing the UI to prompt the user to add the model and retry without restarting the app. [1] [2]
  • Added a new _build_expected_model_path method to generate a helpful path hint for missing model errors, improving the feedback shown to users.

Copilot AI review requested due to automatic review settings October 25, 2025 13:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances thread safety and error handling in the ModelRotationDetector's session initialization. It introduces a lock to prevent race conditions during concurrent model loading and changes the error handling strategy for missing models to allow runtime recovery without application restart.

Key Changes:

  • Added thread-safe session initialization using a threading lock with double-checked locking pattern
  • Changed missing model handling to raise an exception instead of permanently disabling the detector
  • Added helper method to generate user-friendly model path hints for error messages
Comments suppressed due to low confidence (1)

src/core/image_features/model_rotation_detector.py:1

  • Setting s.failure_logged = True before raising the exception (line 142-143) creates a race condition. If the exception is caught and handled elsewhere, and another thread enters this block before the first thread completes, the second thread will skip logging because failure_logged is already True, but will still raise the exception. The flag should be set after all logging is complete, or the logic should ensure atomic operation of both logging and flag setting.
"""Lazy-loading rotation detector for image orientation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +123 to +134
with self._session_init_lock:
# Double-check after acquiring the lock to avoid duplicate loads.
if s.session is not None:
return True

# Always re-check for the model path and raise if missing so the UI can show the dialog every run
model_path = self._resolve_model_path()
if not model_path:
# Notify callers immediately so UI can surface actionable feedback,
# but don't mark the detector as permanently failed; users might add
# the model and retry without restarting the app.
if not s.failure_logged:
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The state flags s.tried_load and s.load_failed are checked at line 146 but are never set within the lock when the model path is missing. This creates a race condition where multiple threads could simultaneously raise ModelNotFoundError and log the same warning multiple times before s.failure_logged is set to True. Consider setting s.tried_load = True after raising the exception or restructuring the logic to prevent duplicate exception raising and logging across threads.

Copilot uses AI. Check for mistakes.
@duartebarbosadev duartebarbosadev merged commit 3e13bb5 into main Oct 25, 2025
3 checks passed
@duartebarbosadev duartebarbosadev deleted the fix--auto-rotation-not-showing-model-missing-dialog branch October 25, 2025 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants