Repository navigation
Conversation
Readers find the "current" lock container with get_current_locks(), which was all_locks_.back() on the std::list of lock containers -- a plain read of the list tail, done before taking any lock. A resizer holding all locks may append a new container concurrently (maybe_resize_locks, called from cuckoo_fast_double / cuckoo_expand_simple while the lock array is below kMaxNumLocks). Nothing ordered the new node's construction before a reader that observes it: a data race in the C++ model, and on weakly ordered CPUs (AArch64) a reader can see the newly hooked node while its vector data pointer is still stale and index into garbage before any spinlock synchronizes. x86 (TSO) hides it. Fix: wrap the list in all_locks_t, which keeps an std::atomic<locks_t*> to the current container. A container is published with a release store only after it is fully constructed and linked (push_back); get_current_locks() acquire-loads it. lock_all starts from the acquired current container (current_iterator()) instead of std::prev(end()). Copy/move/swap re-publish. Construction and add_locks_from_other build the container fully before publishing. No change to the locking protocol or the hot path beyond the acquire load (plain mov on x86, ldar on ARM). Fixes efficient#174.
manugoyal
added a commit
that referenced
this pull request
Nov 14, 2026
When a table grows, it appends a new lock array to a list. Currently access to the list is completely un-synchronized. At the same time, other threads can read the list's tail to get the current locks array or walk the list while releasing locks in other resize operations. Those accesses can race with the append, causing undefined behavior even when failures are rare. We solve the reader/writer race by maintaining an atomic pointer, which is always kept pointing at the tail of the locks list. So readers will just need to load the atomic pointer, and writers will republish the pointer whenever they add a new locks container. We solve the writer race by protecting writes to the `all_locks_` container with a mutex. This should only cost a mutex acquisition on the resize / clear paths, which are meant to be low-frequency, expensive operations anyways. Also added a focused test that grows a table while another thread repeatedly reads a stable key. It checks that the key remains readable. Under ThreadSanitizer, it also catches the original reader/resize data race. This change is meant to address #174, and draws on the approaches from #176 and #177.
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.
Readers find the "current" lock container with get_current_locks(), which was all_locks_.back() on the std::list of lock containers -- a plain read of the list tail, done before taking any lock. A resizer holding all locks may append a new container concurrently (maybe_resize_locks, called from cuckoo_fast_double / cuckoo_expand_simple while the lock array is below kMaxNumLocks). Nothing ordered the new node's construction before a reader that observes it: a data race in the C++ model, and on weakly ordered CPUs (AArch64) a reader can see the newly hooked node while its vector data pointer is still stale and index into garbage before any spinlock synchronizes. x86 (TSO) hides it.
Fix: wrap the list in all_locks_t, which keeps an std::atomic<locks_t*>
to the current container. A container is published with a release store only after it is fully constructed and linked (push_back); get_current_locks() acquire-loads it. lock_all starts from the acquired current container (current_iterator()) instead of std::prev(end()). Copy/move/swap re-publish. Construction and add_locks_from_other build the container fully before publishing. No change to the locking protocol or the hot path beyond the acquire load (plain mov on x86, ldar on ARM).
Fixes #174. #174