You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BufferMap.total races: unguarded +=/-= drifts eviction accounting across threads #11152
BufferMap.total races: unguarded +=/-= drifts eviction accounting across threads
BufferMap (celery/utils/collections.py) keeps a total message count that drives maxsize eviction, but every update is an unguarded read-modify-write:
put() / extend(): self.total += len(buf) - before
take(): self.total -= 1
plus celery/backends/rpc.py:272 (forget()): self._pending_messages.total -= len(buf)
There is no lock anywhere in put/extend/take. Concurrent updates from different threads lose increments/decrements, so total drifts from the true buffered count.
Cross-thread sharing is real
BaseBackend._pending_messages is one shared BufferMap (celery/backends/base.py:274):
put runs on the result-consumer thread (celery/backends/asynchronous.py:457, on_state_change),
take runs on arbitrary client threads (asynchronous.py:283, _maybe_resolve_from_buffer ← add_pending_result ← AsyncResult.get()),
total -= len(buf) runs on client threads (rpc.py:272, forget()).
So put-vs-take and take-vs-take on total happen in normal operation.
Repro (hammer, no exceptions, wrong count)
4 threads sharing one BufferMap (producers put, consumers take), 640k ops total on CPython 3.14 (GIL build), main @ 9a1371c:
total=3 vs true buffered 0
i.e. total over-counted by 3 with zero errors raised — three take decrements were silently lost. (Under the GIL the window is narrow — quantum preemption inside the RMW — but it fires at scale; on free-threaded builds the window is wide open.) A TLA+ model of the put/take interleaving violates total == true count in exactly this schedule.
Why it matters
total feeds maxsize eviction (self.maxsize and self._evict(), MESSAGE_BUFFER_MAX=1000). Over-counting over-evicts: buffered result metas get dropped, _maybe_resolve_from_buffer raises Empty, and the result stays pending — AsyncResult.get() blocks until timeout (or longer if the message was already consumed). It presents as a rare, unreproducible result-fetch stall under concurrency.
Suggested fix
Serialise the total updates with a lock (e.g. guard put/extend/take's counter maintenance, and the rpc.forget adjustment, with one shared lock on the BufferMap).
Found via formal modeling (TLA+ model of the put/take interleaving, model-checked with specfence) plus the threaded hammer above.
Follow-up with a sharper mechanism analysis from further hammering — there are two distinct races here, and I led with the narrower one above:
1. The before/len(buf) delta race (wide window, primary mechanism).put() reads before = len(buf), calls buf.put(item) (a Python-level call — a certain thread-switch point), then adds len(buf) - before. A concurrent put landing between the two len() reads inflates the delta. Proof: 8 threads x 20k puts on one key with a forced tiny switch interval ends at total=284056 vs true 160000 (+124k over-count, buffer length exact). The +3 in my first hammer was this race, not the RMW below.
2. The total +=/-= RMW race (narrow window, still real under the GIL). Puts-only single-key hammers at the default switch interval repeatedly end 1–2 under (total=159998/159999 vs 160000, buffer exact). In a puts-only run the deltas provably sum to ≥ truth (buffer length is monotonic), so an under-count means increments were lost inside total = total + delta itself — i.e. a thread switch landed between the load and the store of total, twice per ~160k puts. Narrow, but it fires at scale — and wide open on free-threaded builds.
Both races are fixed by the same suggested change (serialise the counter maintenance with one lock on the BufferMap), so the recommendation stands — but the delta race is the one to model/test against, since it dominates by orders of magnitude.
Activity
ashitpatel commentedon Nov 23, 2026
Follow-up with a sharper mechanism analysis from further hammering — there are two distinct races here, and I led with the narrower one above:
1. The
before/len(buf)delta race (wide window, primary mechanism).put()readsbefore = len(buf), callsbuf.put(item)(a Python-level call — a certain thread-switch point), then addslen(buf) - before. A concurrent put landing between the twolen()reads inflates the delta. Proof: 8 threads x 20k puts on one key with a forced tiny switch interval ends attotal=284056vs true160000(+124k over-count, buffer length exact). The+3in my first hammer was this race, not the RMW below.2. The
total +=/-=RMW race (narrow window, still real under the GIL). Puts-only single-key hammers at the default switch interval repeatedly end 1–2 under (total=159998/159999vs160000, buffer exact). In a puts-only run the deltas provably sum to ≥ truth (buffer length is monotonic), so an under-count means increments were lost insidetotal = total + deltaitself — i.e. a thread switch landed between the load and the store oftotal, twice per ~160k puts. Narrow, but it fires at scale — and wide open on free-threaded builds.Both races are fixed by the same suggested change (serialise the counter maintenance with one lock on the
BufferMap), so the recommendation stands — but the delta race is the one to model/test against, since it dominates by orders of magnitude.