Skip to content

Keep the non spillable bytes of PackedFilesWriter consistent if a chunk allocation throws - #127375

Merged
alexey-milovidov merged 7 commits into
masterfrom
packed-files-writer-exception-safe-add-chunk
Oct 30, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
packed-files-writer-exception-safe-add-chunk

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

PackedFilesWriter::Data::addChunk stopped counting the previous last chunk as non spillable before allocating the new chunk. If the allocation threw (e.g. MEMORY_LIMIT_EXCEEDED), the previous chunk stayed the last one, and the destructor of Data subtracted it again, so non_spillable_bytes_in_memory wrapped around and skewed the spill decisions of the writer of packed parts. The new chunk is now allocated first.

There is no test: the allocation has to fail exactly at a chunk boundary. It was found by the UBSan check for unsigned integer overflows in a stress test.

Related: #120906

Changelog category (leave one):

  • Improvement

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

Fix the memory accounting of the writer of packed data parts after a failed memory allocation, which could make it spill to disk too much or too little.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.906-master (included in 26.11 and later)

…hunk allocation throws

`Data::addChunk` stopped counting the previous last chunk as non spillable before
allocating the new chunk. If the allocation threw (e.g. `MEMORY_LIMIT_EXCEEDED`),
the previous chunk stayed the last one, and the destructor subtracted it again,
so `non_spillable_bytes_in_memory` wrapped around and skewed the spill decisions.
Allocate the new chunk first.

Found by the UBSan check for unsigned integer overflows in #120906.

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

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [92553cd]

Summary: ✅


AI Review

Summary

The PR allocates the next chunk before changing PackedFilesWriter memory accounting, preventing a second subtraction of the previous last chunk during cleanup after an allocation exception. Reviewed head 92553cd1a88fd4a7562d8a0f491754bed00b14ac and all prior discussion. No actionable findings. The narrowly scoped ordering fix is supported by the exception-safety trace and the author's reported UBSan reproduction; no local build or tests were run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 22, 2026
@alesapin alesapin self-assigned this Oct 22, 2026
Comment thread src/IO/PackedFilesWriter.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 92553cd1a with master 80eb86bf2 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 2824be836; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to avoid underflow on subtraction.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) failure ("Some queries hung") is the sleepEachRow SELECT of 04117_detach_database_kill_query left running on a parallel replica. That is unrelated to this change and is being fixed in #127149.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The Stateless tests (amd_debug, parallel) failure is 02530_dictionaries_update_field_complex_key_hashed hitting the 600 s deadline in its while clickhouse_test_time_left loop while waiting for the dictionary to refresh. It is unrelated to this change (which only touches src/IO/PackedFilesWriter.cpp): CIDB shows the same test failing today in PRs #115988, #125510, #98708 and #127759 as well.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master. The remaining failures on c703135a are unrelated to this change (it only touches src/IO/PackedFilesWriter.cpp) and all ran during the hour repeated at the end of DST on 2026-10-25: 05045_create_token is fixed by #127762 (now merged in), and 01532_execute_merges_on_single_replica_long / 03008_deduplication_remote_insert_select are addressed by #127766. 02530_dictionaries_update_field_* is covered by the earlier comment.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master to pull in #127781, which fixes the only failure on 824583af: 05218_text_index_substreams_opened_lazily with randomized use_columns_cache = 1. That failure has nothing to do with this change, which only touches src/IO/PackedFilesWriter.cpp.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (adf15b0e). The only failure on 25c7208e was the Build profile diff job, which failed on CI infrastructure (LogClusterNotReady: the endpoint never became ready while querying the log cluster). It is unrelated to this change, which only touches src/IO/PackedFilesWriter.cpp. The merge restarts CI.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127375&sha=adf15b0e53fd7cd97457bd99d88af65f19ab0eaa&name_0=PR&name_1=Build%20profile%20diff (LogClusterNotReady: the endpoint never became ready) and provide a fix in a separate PR. If the fix is already in progress, link it here. It looks like #120462, possibly addressed by #128067. This failure is unrelated to the change in src/IO/PackedFilesWriter.cpp.

@groeneai

Copy link
Copy Markdown
Collaborator

The red on adf15b0e ran at 02:25 UTC, during the CI logs cluster outage from #120462: the read-only replica rejected every query, including the SELECT 1 readiness probe, with Code: 241 (memory limit exceeded). @ azat fixed the cluster at 18:24 UTC and all 24 Build profile diff runs since then are green, so re-running this job should clear it.

The job-side fix, which keeps the check green when the probe itself gets the 241, is your #128067; my #126200 covers the same case, so no other PR is needed.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. Merged master (92553cd1) to rerun CI now that the logs cluster has recovered. The only red on adf15b0e was the Build profile diff infrastructure failure (LogClusterNotReady). The job-side fix is tracked in #128067.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 30, 2026
Merged via the queue into master with commit 893be6e Oct 30, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the packed-files-writer-exception-safe-add-chunk branch October 30, 2026 08:16
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 30, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... label Nov 22, 2026
@clickhouse-gh clickhouse-gh Bot removed the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-improvement Pull request with some product improvements 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.

4 participants