Repository navigation
fix(utils): synchronize BufferMap total updates across threads (fixes #11152) - #11159
Open
ghanashyam03 wants to merge 2 commits into
Open
ghanashyam03 wants to merge 2 commits into
ghanashyam03 wants to merge 2 commits into
Conversation
…rift under concurrency (fixes celery#11152)
auvipy
self-requested a review
November 24, 2026 08:36
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #11152.
BufferMaptracks aggregate buffered items across all sub-buffers inself.total. In multi-threaded consumer/producer workloads (such as RPC and Redis result backends with_pending_messages), concurrentput(),extend(),take(), and_pop_to_evict()mutateself.totalvia non-atomic operations (+=,-=) without synchronization.This leads to drift between
BufferMap.totaland the true item count under concurrency, which causes premature eviction or eviction starvation. In addition,BufferMap.clear()did not resettotalto 0.Changes
self.mutex = threading.RLock()) toBufferMapguardingput,extend,take,_LRUpop,_pop_to_evict, andclear.__enter__/__exit__) toBufferMapso external callers can synchronize multi-step operations on the mapping.BufferMap.clear()to resetself.total = 0.pop()andtotal -= len(buf)inRPCBackend.forget()andResultConsumer.on_wait_for_pending()using the buffer map mutex.Testing
Added unit and concurrency regression tests in
t/unit/utils/test_collections.py:test_clear_resets_totaltest_context_manager_locks_mutextest_concurrent_puts_preserve_totaltest_concurrent_puts_and_takes_preserve_totalVerified locally:
pytest -p no:remotedata t/unit/utils/test_collections.py(76 passed)pytest -o markers=celery -p no:remotedata t/unit/utils/test_collections.py t/unit/backends/test_rpc.py t/unit/backends/test_redis.py(318 passed)flake8 celery/utils/collections.py celery/backends/redis.py celery/backends/rpc.py t/unit/utils/test_collections.py(clean)