Repository navigation
Conversation
…r-inversion SerializationObject::deserializeBinaryBulkStatePrefix wraps the non-thread-safe stream callbacks in safe_* lambdas guarded by a stack-local callbacks_mutex, then splits dynamic-path prefix deserialization across prefixes_deserialization_thread_pool. The per-task settings_copy reused the same pool pointer, so a nested JSON-in-JSON level re-entered the parallel branch, built a second callbacks_mutex, and re-wrapped the outer level's already-safe getter, holding the inner mutex across the re-entrant callback. The stack-local mutex addresses are recycled across independent deserialization trees, so TSAN's address-keyed lock-order graph saw the pair locked in both orders and reported a lock-order-inversion (potential deadlock). The getter-wrapping chain is monotonic on any live pair of instances, so a real cycle cannot form; the warning is a false positive but recurs as ongoing stress-test noise. Null the pool in the per-task settings_copy so nested Object levels take the sequential branch (no second callbacks_mutex, no re-wrapping). Top-level parallelism, the S3 read win, is preserved. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-3:20260617-205300 |
|
cc @Avogar — could you review this? It nulls |
|
Workflow [PR], commit [d4a82a0] Summary: ✅
AI ReviewSummaryThe code now keeps nested PR Metadata
Final VerdictStatus: Minimum required actions: update the PR title/body to describe the flag-based callback reuse, and replace the active issue-closing line with the full issue URL. No code changes requested. |
…l-safe The Style check (various_checks.sh) requires any stateless test issuing a SYSTEM DROP* to carry the no-parallel tag, because SYSTEM DROP affects server-wide shared state. Mark-cache warmth does not influence whether the prefix-deserialization pool path is taken (that branch is gated only by merge_tree_use_prefixes_deserialization_thread_pool plus a Wide part with dynamic paths, and the prefix state is deserialized fresh on each read setup). Drop the SYSTEM DROP MARK CACHE line so the test needs no no-parallel tag and stays parallel-safe. Output and the guarded code path are unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The previous test inserted a plain nested object, which JSON flattens into scalar dynamic paths (nested.x, nested.y.deep, nested.y.deeper). The prefix pool tasks then only deserialize scalar dynamic paths and never recurse into a nested SerializationObject, so the test did not exercise the changed invariant and passed even with the production fix reverted. Make the 'arr' path an array of objects so JSON stores it as a dynamic path of type Array(JSON), i.e. a genuine nested SerializationObject. Reading subcolumns of the array elements drives a prefix-deserialization pool task to recurse into the nested object's prefix. Verified locally on a debug build by instrumenting the parallel-pool branch: with the fix reverted the branch is entered for the nested level too (the re-entrant path that creates the second callbacks_mutex); with the fix it is entered only for the top level. Pool=1 and pool=0 reads give bit-identical results. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (test reshape, 4a3e0f8)
Verified locally on a debug build (clang-21), 50/50 passes with randomized settings via Session id: cron:clickhouse-worker-slot-3:20260617-234300 |
…ntry A subcolumn read of an Array(JSON) path goes through SerializationObjectDynamicPath and reaches the nested object as a standalone pool owner, so it never enters the outer SerializationObject::deserializeBinaryBulkStatePrefix pool task where the fix nulls the per-task pool pointer. Only a full-column read makes the outer object's pool task recurse into the nested object's prefix, which is the re-entrant path this PR changes. Replace the subcolumn-only assertions with a full-column read (SELECT data WHERE id = 5) plus a whole-column read across all granules. Verified by instrumenting the pool branch on a debug build: with the fix reverted the full-column read enters the pool branch twice (top-level depth 1 and nested depth 7), and with the fix only the top-level entry remains; the subcolumn reads enter only the nested branch in both cases and so do not exercise the change. Results are bit-identical between pool=1 and pool=0. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (updated for the test reshape in c596639)
Session id: cron:clickhouse-worker-slot-6:20260618-011000 |
|
CI finished on HEAD
Functional and style checks are green; the reshaped Array(JSON) full-column-read regression test passes. |
|
@alexey-milovidov Investigated. The What the artifacts show:
It is chronic, independent of this PR: the same The opaque FileNotFoundError is exactly what #107676 makes transparent (report the runner abort instead). A BuzzHouse re-run clears the Mergeable Check. Session: cron:clickhouse-worker-slot-1:20260620-161300 |
|
@groeneai, update the branch here, so we will have a chance to merge. |
|
@alexey-milovidov branch updated: merged current master (7ef8534) into |
|
CI finished on
|
| /// A nested Object level must deserialize sequentially: re-entering the pool here would | ||
| /// create a second callbacks_mutex and re-wrap the already-safe getter, which TSAN flags | ||
| /// as a lock-order-inversion across recycled stack-mutex addresses. | ||
| settings_copy.prefixes_deserialization_thread_pool = nullptr; |
There was a problem hiding this comment.
It's a bad fix, it will lead to performance degradation on cases with wide nested JSONs, especiall when top level JSON contains a few Array(JSON) keys. We should not disable parallel prefixes deserialization for nested objects. Let's think about other solution.
There was a problem hiding this comment.
Maybe we can add a bool flag to settings that the getters are safe and don't recreate them in nested deserializations?
There was a problem hiding this comment.
Done, in 1c74eac. Added a prefix_deserialization_callbacks_are_thread_safe flag to the deserialize settings. The top-level parallel branch wraps the callbacks under its callbacks_mutex and sets the flag in each task's settings_copy; a nested level sees the flag, keeps the pool (still takes the parallel branch, so nested parallelism is preserved), and reuses the ancestor's already-safe callbacks instead of installing its own wrappers. The nested callbacks_mutex is thus never locked, so only the single top-level mutex is acquired down the whole tree and the lock-order pair TSAN reported cannot form. No parallelism is disabled for wide/multi-Array(JSON) nested objects. The flag defaults to false, so other callers are unchanged.
Verified on a debug build via instrumentation at the parallel-branch entry, reading the full data column of an Array(JSON) path:
- with the flag fix: top-level enters with
wrap_callbacks=1; the nestedArray(JSON)level (depth 7) still enters the parallel branch but withwrap_callbacks=0(no second mutex installed). - with the flag forced off (old always-wrap behavior): the nested level enters with
wrap_callbacks=1, installing and locking the second mutex (the LOI source).
pool=1 vs pool=0 reads are bit-identical; the 04357 regression test passes 15/15 with randomization on.
The previous fix nulled prefixes_deserialization_thread_pool in the per-task settings_copy so nested JSON-in-JSON Object levels took the sequential branch. That removed the second callbacks_mutex (the TSAN lock-order-inversion source) but also disabled parallel prefix deserialization for nested objects, which degrades wide nested JSONs, especially when the top-level JSON has several Array(JSON) keys. Instead, keep the pool for nested levels and add a flag, prefix_deserialization_callbacks_are_thread_safe, to the deserialize settings. The top-level parallel branch wraps the stream callbacks in safe_* lambdas under its stack-local callbacks_mutex and sets the flag in each task's settings_copy. A nested level sees the flag set, still enters the parallel branch (nested parallelism preserved), but reuses the ancestor's already-safe callbacks instead of installing its own wrappers. The nested callbacks_mutex is therefore never locked, so only the single top-level mutex is ever acquired down the whole tree and no lock-order edge between two nested-level mutexes can form. This removes the TSAN potential-deadlock warning (recycled stack-mutex addresses conflated across deserialization trees) without serializing nested deserialization. The flag defaults to false, so all other callers are unchanged.
Pre-PR validation gateUpdated for the flag-based redesign (commit 1c74eac, per the review on this thread).
Session id: cron:clickhouse-worker-slot-4:20260623-150000 |
The fix now keeps nested prefix deserialization parallel and reuses the ancestor's already thread-safe callbacks via settings.prefix_deserialization_callbacks_are_thread_safe, instead of forcing the nested level onto a sequential branch. Correct the test comment, which still described the old sequential approach. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 19/19 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
Retitled to "Fix TSAN lock-order-inversion in nested JSON prefix deserialization". The old title said "sequentially", which described the first approach; the merged fix keeps nested prefix deserialization parallel and only avoids the second callbacks mutex via |
…6-nested-json-prefixes-pool-tsan Fix TSAN lock-order-inversion in nested JSON prefix deserialization
|
Need to backport to fix CI in 26.6 |
…N prefix deserialization
Backport #107794 to 26.6: Fix TSAN lock-order-inversion in nested JSON prefix deserialization
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not required.
Description
Closes: #107776
ThreadSanitizer reported a lock-order-inversion (potential deadlock), STID 5654-6326, in
SerializationObject::deserializeBinaryBulkStatePrefix.CI report (Stress test (arm_tsan)): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107754&sha=67e40e52a110e6688b33af1371400f77fe8f7f5e&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29
Root cause. The parallel branch wraps the non-thread-safe stream callbacks in
safe_*lambdas guarded by a stack-localcallbacks_mutex, then splits dynamic-path prefix deserialization acrossprefixes_deserialization_thread_pool. A nested JSON-in-JSON level re-entered the parallel branch, built a secondcallbacks_mutex, and re-wrapped the outer level's already-safe getter, holding the inner mutex across the re-entrant callback. M0 and M1 are these two stack-local mutexes; their addresses are recycled across independent deserialization trees, so TSAN's address-keyed lock-order graph saw the pair locked in both orders and reported a cycle. The getter-wrapping chain is monotonic on any live pair of instances, so a real cycle cannot form: the report is a false positive, but it recurs as ongoing stress-test noise (6 distinct PRs in 30 days, 0 on master).Fix. Add an internal deserialize-settings flag
prefix_deserialization_callbacks_are_thread_safe. The top level wraps the callbacks once under itscallbacks_mutexand sets the flag in each per-tasksettings_copy. A nested level sees the flag set, still takes the parallel branch (nested parallelism is preserved), but reuses the ancestor's already-thread-safe callbacks instead of wrapping them under a second mutex. Only onecallbacks_mutexis ever installed down the tree, so the lock-order-inversion pair cannot form. The flag is per-deserialization (not global), so independent deserialization trees do not interfere.Verified locally on a debug build: with the flag fix a nested
Array(JSON)full-column read still enters the parallel pool branch at the nested level but installs no second mutex, while reverting the flag re-creates the second mutex (the inversion source). Pool on vs off returns bit-identical results on a Wide-part nested-JSON column. Added a regression test (04357_json_nested_prefixes_deserialization_pool).Version info
26.7.1.64(included in26.7and later)26.6.7.11