Repository navigation
Make the lazy CPU-feature dispatch thread-safe - #4
Closed
alexey-milovidov wants to merge 1 commit into
Closed
alexey-milovidov wants to merge 1 commit into
alexey-milovidov wants to merge 1 commit into
Conversation
The crc32/adler32/decompress/stream-decompress dispatchers cache the runtime-resolved best implementation in a function pointer on the first call. The pointer was 'volatile', so the benign first-call race (every thread resolves the same pointer, a pure function of the CPU) was still undefined behavior and was flagged by ThreadSanitizer. Access the pointer with relaxed atomics (__atomic_load_n/__atomic_store_n) instead. The generated code is unchanged on real hardware, but the access is now well-defined and TSan-clean. Relaxed ordering is sufficient because no other memory is published through the pointer.
alexey-milovidov
force-pushed
the
thread-safe-cpu-dispatch
branch
from
June 29, 2026 22:32
f8f6cd9 to
cb6b5c9
Compare
alexey-milovidov
added a commit
that referenced
this pull request
Jun 29, 2026
Make the lazy runtime CPU-feature dispatch thread-safe (relaxed atomics instead of volatile) so ThreadSanitizer no longer flags the benign first-call race in crc32/adler32/decompress/stream-decompress.
alexey-milovidov
added a commit
to ClickHouse/ClickHouse
that referenced
this pull request
Jun 29, 2026
The `crc32`/`adler32`/`decompress`/`stream-decompress` dispatchers cache the runtime-resolved best implementation in a function pointer on first use. That pointer was `volatile`, which is not atomic, so the benign first-call race (every thread resolves the same pointer, a pure function of the CPU) was undefined behavior. ThreadSanitizer flagged it as a data race in `libdeflate_crc32`, producing the TSan "data race" and "Server died" failures and an intermittent `04408_parquet_gzip_libdeflate` result mismatch under TSan. Access the pointers with relaxed atomics (`__atomic_load_n`/`__atomic_store_n`) instead. The generated code is unchanged on real hardware, but the access is now well-defined and TSan-clean. Relaxed ordering is sufficient because no other memory is published through the pointer. Fork commit: ClickHouse/libdeflate@cb6b5c96c7d5 Related: ClickHouse/libdeflate#4 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Member
Author
|
Integrated into |
This was referenced Jun 29, 2026
Merged
thevar1able
pushed a commit
that referenced
this pull request
Sep 10, 2026
The previous fix (PR #4) made the per-codec dispatch function pointers (crc32_impl, adler32_impl, ...) atomic, but each dispatcher still calls get_x86_cpu_features() / get_arm_cpu_features(), which lazily initialize the global libdeflate_x86_cpu_features / libdeflate_arm_cpu_features bitmask on the first call. That global was only 'volatile', so a plain load racing with the store in libdeflate_init_*_cpu_features() is undefined behavior and was still flagged by ThreadSanitizer: WARNING: ThreadSanitizer: data race Write ... libdeflate_init_x86_cpu_features cpu_features.c Read ... get_x86_cpu_features cpu_features.h Location is global 'libdeflate_x86_cpu_features' This reproduced as a "Server died" failure when two HTTP connections first compressed a gzip response concurrently under TSan. Access the global with relaxed __atomic_load_n / __atomic_store_n, matching the dispatch-pointer fix. The first-call initialization is a benign race (every thread computes the same bitmask, a pure function of the CPU), and relaxed ordering suffices because no other memory is published through it. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> (cherry picked from commit ec0718b)
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
The `crc32`/`adler32`/`decompress`/`stream-decompress` dispatchers cache the runtime-resolved best implementation in a function pointer on first use. That pointer was `volatile`, which is not atomic, so the benign first-call race (every thread resolves the same pointer, a pure function of the CPU) was undefined behavior. ThreadSanitizer flagged it as a data race in `libdeflate_crc32`, producing the TSan "data race" and "Server died" failures and an intermittent `04408_parquet_gzip_libdeflate` result mismatch under TSan. Access the pointers with relaxed atomics (`__atomic_load_n`/`__atomic_store_n`) instead. The generated code is unchanged on real hardware, but the access is now well-defined and TSan-clean. Relaxed ordering is sufficient because no other memory is published through the pointer. Fork commit: ClickHouse/libdeflate@cb6b5c96c7d5 Related: ClickHouse/libdeflate#4 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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.
The crc32 / adler32 / one-shot-decompress / streaming-decompress implementations are selected on first use through a function pointer declared
volatile.volatileis not atomic, so concurrent first calls (e.g. several HTTP handlers compressing/decompressing gzip at once in ClickHouse) race on the store and the load, which ThreadSanitizer correctly reports as a data race.The race is benign in value — the pointer is always either the dispatcher or the final implementation (both valid to call) and the store is idempotent — so switch the accesses to relaxed atomic load/store (
__atomic_load_n/__atomic_store_nwith__ATOMIC_RELAXED). This compiles to a plain aligned load/store on every supported target (no fence) but is no longer a data race.Found via the ClickHouse TSan stateless run while integrating libdeflate (ClickHouse/ClickHouse#108074).