fix: Pop is incorrect while concurrent#1372
Conversation
Additional Comments (2)
Prompt To Fix With AIThis is a comment left during a code review.
Path: queue/driver_database.go
Line: 55:57
Comment:
**In-memory lock doesn't ensure** correctness.
This per-queue `sync.Mutex` only serializes `Pop()` calls within the same Go process; multiple workers/processes (or multiple app instances) will still concurrently `SELECT ... FOR UPDATE` different rows and reserve multiple jobs from the same queue. If the intended fix is to prevent more than one concurrent reservation per queue globally, this needs a DB-level constraint/locking strategy (e.g., queue-level lock table/advisory lock) rather than an in-process mutex.
How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix With AIThis is a comment left during a code review.
Path: queue/driver_database.go
Line: 34:34
Comment:
**Unbounded mutex growth**.
`queueMutexes` stores one `*sync.Mutex` per distinct queue string and never evicts entries, so a system that uses dynamic/tenant-based queue names will leak memory over time. If queue names aren’t strictly bounded, consider a bounded/evicting structure or avoid storing per-queue locks altogether.
How can I resolve this? If you propose a fix, please make it concise. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v1.17.x #1372 +/- ##
==========================================
Coverage ? 68.71%
==========================================
Files ? 281
Lines ? 17768
Branches ? 0
==========================================
Hits ? 12209
Misses ? 5055
Partials ? 504 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Greptile Overview
Greptile Summary
Database.Pop()using async.Mapof*sync.Mutexkeyed by queue name.Confidence Score: 2/5
Important Files Changed
Sequence Diagram
sequenceDiagram participant W1 as Worker (proc A) participant DB as Database driver participant SQL as DB participant W2 as Worker (proc B) W1->>DB: Pop(queue) DB->>DB: Lock per-queue mutex (proc A only) DB->>SQL: BEGIN DB->>SQL: SELECT ... FOR UPDATE (first available job) SQL-->>DB: job row DB->>SQL: UPDATE job reserved_at, attempts DB->>SQL: COMMIT DB-->>W1: ReservedJob W2->>DB: Pop(queue) Note over W2,DB: No shared mutex across processes/instances DB->>SQL: BEGIN DB->>SQL: SELECT ... FOR UPDATE (next available job) SQL-->>DB: different job row DB->>SQL: UPDATE job reserved_at, attempts DB->>SQL: COMMIT DB-->>W2: ReservedJob Note over W1,W2: In-memory mutex does not prevent Note over W1,W2: multiple concurrent reservations globally.