Skip to content

Make the lazy CPU-feature dispatch thread-safe - #4

Closed
alexey-milovidov wants to merge 1 commit into
streaming-decompress-x86-bmi2from
thread-safe-cpu-dispatch
Closed

alexey-milovidov wants to merge 1 commit into
streaming-decompress-x86-bmi2from
thread-safe-cpu-dispatch

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

The crc32 / adler32 / one-shot-decompress / streaming-decompress implementations are selected on first use through a function pointer declared volatile. volatile is 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_n with __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).

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
alexey-milovidov force-pushed the thread-safe-cpu-dispatch branch from f8f6cd9 to cb6b5c9 Compare June 29, 2026 22:32
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]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Integrated into v1.25-base via merge commit 3a9791b (the refined cb6b5c9, which renames the dispatch accessor to *_impl_load() and tidies the comments). ClickHouse/ClickHouse#108074 pins contrib/libdeflate at cb6b5c9, now reachable from the default branch. Closing as 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]>
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.

1 participant