Fix: Resolve watcher service deadlock during self-restart on folder deletion
Addressed Issues:
Fixes #1031
Screenshots/Recordings:
Additional Notes:
Description This PR fixes a bug where the sync-microservice watcher service would freeze and eventually fail when attempting to restart itself after detecting a deleted folder.
Previously, when a folder was deleted, the background thread (watcher_thread) triggered a restart sequence that called watcher_util_stop_folder_watcher(). Inside that stop function, the thread attempted to execute watcher_thread.join(). Since it was joining itself, this caused a deadlock that hung for the 5-second timeout, preventing a clean restart and throwing a runtime error.
Changes made
- watcher.py
- Prevent Self-Joining: Added a thread-safety check in watcher_util_stop_folder_watcher() so that .join() is only called if threading.current_thread() != watcher_thread.
- Spawn Restart Worker: Updated watcher_util_restart_folder_watcher() to check if it's being executed by the watcher_thread. If so, it spawns a temporary background daemon thread to handle the stop/start sequence, allowing the original watcher thread to return and terminate gracefully.
How to test
- Start the sync-microservice.
- Add a folder to the watchlist (this automatically starts the watcher).
- Manually delete that folder from the filesystem while the service is running.
- Observe the logs: The watcher should now log "Processing 1 deleted folders", followed immediately by successfully stopping the watcher thread and restarting it, without the 5-second hang or the thread-stopping warning.
AI Usage Disclosure:
We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.
Check one of the checkboxes below:
- [x ] This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.
I have used the following AI models and tools: TODO
Checklist
- [x] My PR addresses a single issue, fixes a single bug or makes a single improvement.
- [x] My code follows the project's code style and conventions
- [x] If applicable, I have made corresponding changes or additions to the documentation
- [x] If applicable, I have made corresponding changes or additions to tests
- [x] My changes generate no new warnings or errors
- [x] I have joined the Discord server and I will share a link to this PR with the project maintainers there
- [x] I have read the Contribution Guidelines
- [x] Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
- [x] I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.
Summary by CodeRabbit
-
Documentation
- Updated OpenAPI schema organization in backend documentation
-
Bug Fixes
- Fixed folder watcher thread management issues during stop operations
- Enhanced folder watcher restart functionality to correctly handle internal thread operations
- Improved overall stability of folder watching processes
Walkthrough
This PR addresses a deadlock in the watcher service thread synchronization and updates OpenAPI schema documentation. The watcher changes prevent the worker thread from joining itself by detecting caller context and delegating internal restarts to daemon threads. Schema definitions are reordered for consistency.
Changes
Watcher Thread Self-Restart Deadlock Prevention
| Layer / File(s) | Summary |
|---|---|
Conditional thread join in stop function sync-microservice/app/utils/watcher.py |
watcher_util_stop_folder_watcher() detects whether it is called from within the watcher thread or externally. When called from inside the watcher thread, it skips the thread.join() call to avoid self-join deadlock and logs an internal-call message. External calls proceed normally with thread joining. |
Async restart delegation for in-thread calls sync-microservice/app/utils/watcher.py |
watcher_util_restart_folder_watcher() checks the caller context; if called from inside the watcher thread, it spawns a daemon restart_worker thread to perform stop/start asynchronously and returns immediately. External calls continue to synchronously stop and start the watcher. |
OpenAPI Schema Documentation Updates
| Layer / File(s) | Summary |
|---|---|
Error response schema reordering and structure cleanup docs/backend/backend_python/openapi.json |
Error response schemas in components.schemas are reordered: app__schemas__face_clusters__ErrorResponse now appears before app__schemas__folders__ErrorResponse. The transition area of DeleteFoldersResponse is adjusted to ensure clean schema definition boundaries. |
🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested labels
Python, Documentation
Suggested reviewers
- rahulharpal1603
🐰 A watcher thread that joins itself with glee,
Now detects with care and stays deadlock-free,
With daemon threads dancing when restarts arise,
And schemas reordered with documentation's eyes! ✨
🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Out of Scope Changes check | ⚠️ Warning | The openapi.json schema reordering appears unrelated to the deadlock fix objective; it only reorganizes error schema definitions without structural changes, suggesting potential scope creep. | Remove the openapi.json schema reordering changes as they are unrelated to the watcher deadlock fix and should be addressed in a separate PR focused on documentation/schema organization. |
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title clearly and specifically describes the main fix: resolving a deadlock issue in the watcher service during self-restart on folder deletion, which matches the core problem addressed in the PR. |
| Linked Issues check | ✅ Passed | The PR successfully addresses the deadlock issue #1031 by implementing thread-safety checks in watcher_util_stop_folder_watcher() and spawning a daemon thread in watcher_util_restart_folder_watcher() when called from the watcher thread, preventing self-join deadlock. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.