Skip to content

Fix flaky test 05346_distributed_insert_connection_buffers_freed_by_same_query - #125909

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-05346-uncharged-memory-samples
Oct 15, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-05346-uncharged-memory-samples

Conversation

@groeneai

@groeneai groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Not required.

Description

Related: #125573

Failure: Stateless tests (amd_release, parallel, 1/2) on master returned 8 1 instead of 8 0. The test also failed on 3 unrelated PRs, 2 of them in Fast test.

The oracle counted every MemorySample with the INSERT's query_id, including samples taken under a MemoryTrackerBlockerInThread, which are not charged to the query. System log pushes run under such a blocker (SystemLogQueue::add). When a flush cycle is larger than the previous one, the shared queue vector regrows inside whichever query pushes the overflowing row, and that query frees the old buffer, which the flush thread or an earlier push allocated. In the failing run's trace_log the only flagged pointer is a -10485760 free in SystemLogQueue<ProcessorProfileLogElement>::push from logQueryFinishImpl, with memory_blocked_context = User.

The test now counts only samples charged to the query (memory_blocked_context = 'Max'). The per-query connection buffers it checks are charged, so it still catches the regression it was added for.

Validation (debug build):

  • Filling the processors_profile_log queue to just below its capacity before the test moves the regrowth into its first INSERT: the old test gives 8 1 10/10 (same stack as CI), the new one 8 0 10/10.
  • With ConnectionPool::releaseObject made a no-op, the new test still fails (8 14), with frees in Connection::resetQueryBuffers called from sendQuery.
  • 50/50 with randomized settings, idle and under concurrent load.

Workflow [PR]
Sync PR [sync-upstream/pr/125909]

Version info

  • Merged into: 26.10.1.2450-master (included in 26.10 and later)

…ame_query

The test counted every MemorySample in system.trace_log with the INSERT's
query_id, including samples taken under a MemoryTrackerBlockerInThread.
Those are not charged to the query, but MemoryTracker still samples them
with the thread's query_id.

System log pushes run under such a blocker (SystemLogQueue::add). After a
flush the queue vector keeps max(reserved_size_rows, rows just popped) of
capacity, so a larger cycle regrows the shared vector inside whichever
query pushes the overflowing row, and that query frees the old buffer,
which the flush thread allocated. The oracle read it as memory the INSERT
freed without allocating it. In the failing master run the only flagged
pointer was a -10485760 free in
SystemLogQueue<ProcessorProfileLogElement>::push from logQueryFinishImpl,
with memory_blocked_context = User.

Count only samples charged to the query (memory_blocked_context = 'Max').
The per-query connection buffers the test checks are charged, so the
regression it guards (ConnectionPool::releaseObject not releasing them)
is still caught.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 The filter also hides a free made under a blocker by a later query of memory an earlier query was charged for AGREE, noted The regression the test exists for still fails it: with ConnectionPool::releaseObject made a no-op the test gives 8 14, frees in Connection::resetQueryBuffers from sendQuery, memory_blocked_context = Max
2 💡 Commit message says the freed queue buffer was allocated by the flush thread; in the CI run it came from an earlier regrowth AGREE, noted Corrected in the PR description; the mechanism is unchanged

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-8:20261015-000200

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Filling the processors_profile_log queue to 8165 rows right after a flush, then running the test: the old test gives 8 1 10/10.
b Root cause explained? Blocked (uncharged) samples are still recorded with the query's id; a shared system log queue regrowing inside the INSERT frees its old buffer.
c Fix matches root cause? Yes. The oracle drops exactly the uncharged samples (memory_blocked_context != 'Max'); no tag, pin or bound change.
d Test intent preserved / new tests added? Yes. With ConnectionPool::releaseObject made a no-op the new test still fails (8 14). Test-only change.
e Both directions demonstrated? Yes. Same repro: old test 10/10 8 1, new test 10/10 8 0.
f Fix is general across code paths? Yes. Every system log queue and the pooled Connection::connect buffers push under the same blocker, so all are excluded by one filter.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test oracle); 50/50 with randomized settings, idle and under load.
h Backward compatible? (maintainer-approved exception only) N/A (test-only).
i Invariants and contracts preserved? Yes. The asserted invariant (an INSERT's charged frees match its charged allocations) is unchanged.

Session id: cron:clickhouse-impl-slot-6:20261014-231300

@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1930aee]

Summary: ✅


AI Review

Summary

PR #125909 limits the allocation assertion in 05346_distributed_insert_connection_buffers_freed_by_same_query to samples with memory_blocked_context = 'Max'. This excludes shared system-log queue memory that is not charged to the query while preserving coverage of pooled connection-buffer cleanup. The documented deterministic reproduction and mutation check support the change, and the test passes in Fast test at commit 1930aee44a17ab8e1675fefc62fcbd3bd4e9e331. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Oct 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano, could you review this? The new 05346 oracle also counted uncharged samples taken under a MemoryTrackerBlockerInThread (a shared SystemLogQueue vector regrowing inside an INSERT and freeing its old buffer), so it now counts only samples charged to the query (memory_blocked_context = 'Max').

@alexey-milovidov alexey-milovidov self-assigned this Oct 15, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Oct 15, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit c2f98bb Oct 15, 2026
350 of 351 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants