AOSSIE-Org / AOSSIE-Org/PictoPy

BUG: Critical Race Condition in Sync Microservice Watcher Initialization

未關閉
#1,166 5 則留言 0 個 reaction 已指派 1 人 已被 @faizavk 認領 在 GitHub 檢視
backend bug
主要語言
Python
星號
283
分支
679
平均合併
7 天 2 小時
30 天內合併 PR
3

描述

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

**Bug Description:**
A Time-of-Check to Time-of-Use (TOCTOU) race condition exists in sync-microservice/app/utils/watcher.py. This vulnerability allows multiple background watcher threads to be spawned simultaneously, which can lead to duplicated sync events, corrupted global state, and memory leaks.

**Root Cause:** The global variables tracking the watcher state (watcher_thread, watched_folders, folder_id_map) are mutated across asynchronous API requests without thread synchronization (e.g., no threading.Lock()).

Because the startup process involves I/O operations (database and filesystem checks) that yield the Python GIL, if the /start endpoint receives concurrent requests, both requests can bypass the watcher_util_is_watcher_running() check before the first request has a chance to assign the watcher_thread variable. This results in multiple watchfiles processes being initialized and running concurrently over the same directories.

**Proposed Fix**
To enforce thread safety, I propose introducing a global threading.Lock() (e.g., state_lock = threading.Lock()). We can then wrap the critical sections inside watcher_util_start_folder_watcher, watcher_util_stop_folder_watcher, and watcher_util_restart_folder_watcher using a with state_lock: context manager. This will guarantee atomic operations on the global watcher state and completely prevent duplicate thread creation.

### Record

- [x] I agree to follow this project's Code of Conduct

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。