Skip to content

Fix use-after-free in async insert queue due to deadline collision - #113363

Merged
mstetsyuk merged 1 commit into
masterfrom
fix-async-insert-deadline-collision
Aug 5, 2026
Merged

mstetsyuk merged 1 commit into
masterfrom
fix-async-insert-deadline-collision

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Linked issues

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a rare use-after-free bug that is triggered in a rare event of the async insert queue having different entries with identical deadlines. Now it's handled gracefully.

Version info

  • Merged into: 26.8.1.836 (included in 26.8 and later)
  • Backported to: 26.7.3.16, 26.6.3.9, 26.5.7.12, 26.3.18.5, 25.8.30.4

@mstetsyuk
mstetsyuk requested a review from CurtizJ August 4, 2026 17:35
@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8334528]

Summary: ❌

job_name test_name status info comment
Stress test (arm_release) FAIL
Logical error: ColumnBLOB should be converted to a regular column before usage (STID: 3059-3663) FAIL cidb, issue
Stress test (arm_debug) FAIL
Test script failed FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR fixes the async insert deadline-collision bug by switching the per-shard ordered queue from std::map to std::multimap and by storing the iterator returned from the real queue insertion. That keeps distinct InsertQuery batches separate even when steady_clock::now() + timeout collides, so the key.hash -> queue iterator index no longer aliases another batch and cannot dangle after that batch is erased. I read the current diff, the full queue lifecycle (pushDataChunk, timer flush, manual flush, and shutdown), the prior PR discussion, and the current CI state, and I did not find a remaining correctness or lifecycle issue in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Aug 4, 2026
@watson-ch

watson-ch Bot commented Aug 4, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2004

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@CurtizJ CurtizJ self-assigned this Aug 5, 2026
@mstetsyuk
mstetsyuk enabled auto-merge August 5, 2026 11:38
@mstetsyuk

mstetsyuk commented Aug 5, 2026 •

Copy link
Copy Markdown
Member Author

@groeneai, please confirm the CI failures are unrelated.

@groeneai

groeneai commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Confirmed unrelated. Both Stress reds are pre-existing failures that already have owners, and Finish Workflow is only the aggregator reporting them. Evidence below, since "unrelated" on a pr-critical-bugfix is worth substantiating.

Both jobs built merge commit a7649885ef8d (master parent e1d5eb2abf46, 2026-08-04 17:04:36Z).

Stress test (arm_release): Logical error: ColumnBLOB should be converted to a regular column before usage (STID 3059-3663)

Owner: #111997 (merged 2026-08-04 21:56:55Z), tracked as issue #113165. The job classified it itself: Marking ... as flaky (matched: ..., issue: #113165).

The aborting query is SELECT key FROM m2 ORDER BY key, from 03581_nested_storage_merge_distributed_order_by: nested Merge over Distributed, which is exactly the local-shard-plan path #111997 repairs in StorageMerge.cpp.

Your build predates that fix. is_local_plan_for_distributed_query occurs 0 times in src/Interpreters/SelectQueryOptions.h at a7649885ef8d and at its master parent, and 2 times at the fix's merge commit; 04647_no_blocks_marshalling_for_local_shard_plan.sh is likewise absent at your build and present at the fix. So this is "built before the fix landed", not "still failing with the fix applied".

Breadth: 11 distinct PRs hit this signature between 2026-08-01 and 08-05. I resolved the actually-built commit for each from its job log and checked the fix symbol at that commit: 11 of 11 built from a master parent predating the fix merge, with the symbol absent in all 11. Rebasing onto master should clear it.

Stress test (arm_debug): Test script failed, script exit code 1

Owner: #113248 (open, @alexey-milovidov). This job never ran a functional test; it dies in the Python harness beforehand.

install_thread_pool_fault_injection runs SELECT value FROM system.server_settings WHERE name = 'cannot_allocate_thread_fault_injection_probability' with --receive_timeout=15 and no retry (ci/jobs/scripts/stress/stress.py:291 at your build). The preceding SYSTEM RELOAD CONFIG took 15.685 s, so the client returned TIMEOUT_EXCEEDED (159) and check_output raised. Of those 15.685 s, 15.448 s is config-reload work: the server-side AST fuzzer re-executed that one client statement 6 times, each pass reloading config.xml (2.083 s to 2.712 s) plus users.xml. #113248 adds the missing retry at exactly that call.

Breadth: 4 further PRs in the same window reach the identical failure, each confirmed in its job log by install_thread_pool_fault_injection plus the same verify query plus returned non-zero exit status 159: 111869, 112088, 111817, 112950. #113248 cites a fifth, on 109496. The authors are unrelated to each other.

Why neither can be caused by your change

Your diff is confined to AsynchronousInsertQueue (std::map to std::multimap). Neither failure enters it:

  • The ColumnBLOB stack is 23 frames, ColumnBLOB::throwInapplicable via columnMatchesType, executeAction, ExpressionTransform::transform, with 0 AsynchronousInsertQueue frames and 0 pushDataChunk or QueueIterator frames. 03581 contains no async inserts. The PullingAsyncPipelineExecutor frame is generic pipeline machinery, unrelated to async INSERT.
  • The arm_debug failure is a config-reload timeout inside a Python harness step, so no query reaches the insert path at all. Async inserts did run in that job and completed normally, the queue shut down cleanly (Asynchronous insertion queue finished), and clickhouse-server.err.log contains 0 AsynchronousInsert lines.

One caveat on reading the arm_debug log: the traceback quotes system.server_settings, but the server log shows the fuzzer had rewritten the statement to SELECT value FROM merge('system', ''), which is why it became slow.

@mstetsyuk
mstetsyuk added this pull request to the merge queue Aug 5, 2026
Merged via the queue into master with commit 3a383ab Aug 5, 2026
176 of 180 checks passed
@mstetsyuk
mstetsyuk deleted the fix-async-insert-deadline-collision branch August 5, 2026 14:05
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 5, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 5, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 5, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 5, 2026
Backport #113363 to 26.7: Fix use-after-free in async insert queue due to deadline collision
mstetsyuk added a commit that referenced this pull request Aug 6, 2026
Backport #113363 to 26.3: Fix use-after-free in async insert queue due to deadline collision
mstetsyuk added a commit that referenced this pull request Aug 6, 2026
Backport #113363 to 26.5: Fix use-after-free in async insert queue due to deadline collision
mstetsyuk added a commit that referenced this pull request Aug 6, 2026
Backport #113363 to 26.6: Fix use-after-free in async insert queue due to deadline collision
mstetsyuk added a commit that referenced this pull request Aug 6, 2026
Backport #113363 to 25.8: Fix use-after-free in async insert queue due to deadline collision
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.2-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants