| # Applied Backend Fixes | |
| 1. **WebSocket Concurrency Error Fix**: | |
| - **Location**: `backend/main.py` -> `ConnectionManager.broadcast` and `ConnectionManager.broadcast_to_agents`. | |
| - **Change**: Updated the iterator from `self.active_connections` (and `self.agent_connections`) to `list(self.active_connections)`. | |
| - **Reason**: In an async environment, yielding execution via `await` while iterating a list leaves the list open to modification (e.g., another coroutine closing the socket and removing it). Iterating over a copy prevents iteration issues like missing elements or `RuntimeError`. | |
| 2. **Detections History Memory Leak Fix**: | |
| - **Location**: `backend/main.py` -> `_upsert_detection_sighting`. | |
| - **Change**: Added `_trim_memory_list(detections_history, 500)` immediately after appending a new record to `detections_history`. | |
| - **Reason**: Most history arrays in the project use `_trim_memory_list` to enforce a maximum cap. `detections_history` was appending arbitrarily, representing an unbounded memory leak if there's a constant influx of unique/unknown person detections over prolonged uptime. | |