Refactor scheduler lock implementation with heartbeat mechanism
- Add heartbeat-based lock renewal in scheduler_lock_heartbeat.py - Update scheduler_lock.py with improved lock management - Add comprehensive tests for scheduler lock functionality - Update deployment and task documentation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,50 +1,3 @@
|
||||
## [IMPORTANT] Database transactions lack explicit isolation
|
||||
|
||||
**Where found**
|
||||
|
||||
- `backend/app/repositories/session_repo.py:40-60` — multiple queries without `BEGIN TRANSACTION`
|
||||
- Similar pattern in multi-step operations across repositories
|
||||
|
||||
**Why this is needed**
|
||||
|
||||
Without explicit boundaries, concurrent requests can race: Thread A checks if exists → not found, Thread B checks same → not found, Thread A inserts → succeeds, Thread B inserts → duplicate error or silent overwrite.
|
||||
|
||||
**Goal**
|
||||
|
||||
Wrap all multi-step operations in explicit transactions with appropriate isolation level.
|
||||
|
||||
**What to do**
|
||||
|
||||
1. Use explicit `BEGIN IMMEDIATE` transaction:
|
||||
```python
|
||||
await db.execute("BEGIN IMMEDIATE")
|
||||
try:
|
||||
await db.execute("INSERT INTO sessions ...")
|
||||
await db.commit()
|
||||
except Exception:
|
||||
await db.rollback()
|
||||
raise
|
||||
```
|
||||
|
||||
2. Use `IMMEDIATE` mode to lock immediately for writes
|
||||
3. Document transaction boundaries clearly
|
||||
|
||||
**Possible traps and issues**
|
||||
|
||||
- Nested transactions (SAVEPOINTs) may be needed
|
||||
- Locks held too long cause contention
|
||||
- Deadlocks possible with concurrent writers
|
||||
|
||||
**Docs changes needed**
|
||||
|
||||
- Add section in `Docs/Backend-Development.md` § Database Transactions
|
||||
|
||||
**Doc references**
|
||||
|
||||
- `Docs/Backend-Development.md` (database design)
|
||||
|
||||
---
|
||||
|
||||
## [IMPORTANT] Scheduler lock race condition
|
||||
|
||||
**Where found**
|
||||
|
||||
Reference in New Issue
Block a user