Commit bb7146e
authored
Fix race condition in InMemoryQueue.AbandonAsync causing flaky RunUntilEmptyAsync (#470)
* Fix race condition in InMemoryQueue causing RunUntilEmptyAsync to exit prematurely
During AbandonAsync, there is a window between removing an entry from
_dequeued and re-enqueueing it for retry where the item exists in
neither collection. If RunUntilEmptyAsync checks queue stats during
this gap, it sees Queued=0 + Working=0 and terminates the job loop
while retryable items are still in flight.
Add a _pendingRetryCount that bridges the gap: incremented before
TryRemove, decremented after the item lands in its destination
(re-queued, deadlettered, or scheduled for delayed retry). The count
is included in the Queued stat so the continuation callback sees
items in transit.
For delayed retries (RetryDelay > 0), the counter is decremented
immediately after scheduling since the item is intentionally parked
and RunUntilEmptyAsync should not spin-wait for it.
Fixes flaky CanRunQueueJobWithLockFailAsync test.
Made-with: Cursor
* Move _pendingRetryCount decrement before _deadletterQueue.Enqueue
In the deadletter path, the item moves out of Queued/Working entirely,
so decrementing _pendingRetryCount before enqueuing to the deadletter
queue avoids a transient overcount where the item appears in both the
Queued stat (via _pendingRetryCount) and the Deadletter stat (via
_deadletterQueue.Count).
The synchronous retry path intentionally keeps the current ordering
(Retry then Decrement) because decrementing first would re-open the
race window this PR fixes: between the decrement and _queue.Enqueue
inside Retry(), both _pendingRetryCount and _queue.Count would be
zero, allowing RunUntilEmptyAsync to exit prematurely.
Made-with: Cursor1 parent 877f769 commit bb7146e
File tree
3 files changed
+13
-4
lines changed- src/Foundatio
- Extensions
- Queues
- Utility
3 files changed
+13
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
292 | 293 | | |
293 | 294 | | |
294 | 295 | | |
| 296 | + | |
| 297 | + | |
295 | 298 | | |
296 | 299 | | |
| 300 | + | |
| 301 | + | |
297 | 302 | | |
298 | 303 | | |
299 | 304 | | |
| |||
323 | 328 | | |
324 | 329 | | |
325 | 330 | | |
| 331 | + | |
326 | 332 | | |
327 | | - | |
| 333 | + | |
328 | 334 | | |
329 | 335 | | |
330 | 336 | | |
| |||
334 | 340 | | |
335 | 341 | | |
336 | 342 | | |
| 343 | + | |
337 | 344 | | |
338 | 345 | | |
339 | 346 | | |
340 | 347 | | |
341 | 348 | | |
| 349 | + | |
342 | 350 | | |
343 | 351 | | |
344 | 352 | | |
| |||
373 | 381 | | |
374 | 382 | | |
375 | 383 | | |
| 384 | + | |
376 | 385 | | |
377 | 386 | | |
378 | 387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
0 commit comments