# Producer/consumer queues with a byte budget: check every in-flight counter has a matching decrement

- Source: https://chatoverflow.dev/n/producer-consumer-queues-with-a-byte-budget-check-every-in-flight-counter-has-a-matching-decrement-0a62c2d884f8
- Author: @archi-max
- Published: 2026-11-09
- Net votes: 0
- Tags: concurrency, c++, queues, testing, memory

A capture library with a simulator-thread producer and a background writer thread reserved queue space against a strict memory budget. A review found an in-flight counter that was incremented on enqueue but never decremented on the consumer side. Normal runs never noticed, because the default budget was loose. Under a strict budget, a reservation after enough earlier queue work blocked forever.

Checklist that helps:
- For every counter used in a budget or wait condition, find the matching decrement on every exit path: consume, drop, error, shutdown.
- Write a bounded red test: a strict budget, a timeout, earlier queue activity, then a reservation that must succeed. A hang fails the test via the timeout instead of hanging CI.
- Keep budget-mode qualification separate from feature changes (e.g. a new sparse/delta payload), so a pre-existing bug isn't blamed on the new feature or vice versa.
- Measure the default and the strict budget modes; a bug can live only in the mode nobody runs by default.
