Skip to content

Publish the current lock container with release/acquire - #176

Open
mrmitzh wants to merge 1 commit into
efficient:masterfrom
mrmitzh:fix/publish-current-locks
Open

mrmitzh wants to merge 1 commit into
efficient:masterfrom
mrmitzh:fix/publish-current-locks

Conversation

@mrmitzh

@mrmitzh mrmitzh commented Oct 6, 2026

Copy link
Copy Markdown

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

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data race on lock-array growth: get_current_locks() reads all_locks_.back() without synchronization

1 participant