Repository navigation
Conversation
Member
|
Missing test. |
alexey-milovidov
marked this pull request as draft
March 3, 2024 00:46
HappenLee
marked this pull request as ready for review
March 3, 2024 01:53
alexey-milovidov
marked this pull request as draft
March 3, 2024 02:49
Member
|
@HappenLee, waiting for tests. |
Member
|
@HappenLee, could you please add tests? |
Member
|
@HappenLee, we need tests. |
Member
|
Hi, @HappenLee! A change requires a test to be accepted. |
Member
|
@HappenLee, the change needs to be proven by some tests. |
Member
|
Hi, @HappenLee. How are you? Please add a test. |
| state.array_of_aggregate_datas = new_state; | ||
| state.dynamic_array_size = new_size; | ||
|
|
||
| // Since destroyImpl cleans up state.array_of_aggregate_datas. we should to clean up old_states after |
Member
There was a problem hiding this comment.
"should to" is incorrect.
Member
|
@HappenLee, you didn't explain. |
Member
|
@HappenLee, We need to prove this change with a test. |
Member
|
@HappenLee We need to know the scenario reproducing the hypothetical problem. |
Member
|
@HappenLee, we need more information about this change. |
Member
|
Abandoned |
This was referenced Aug 12, 2026
rorylshanks
pushed a commit
to rorylshanks/ClickHouse
that referenced
this pull request
Sep 2, 2026
AggregateFunctionForEach::ensureAggregateData grows the per-element nested state array. It deliberately does not memcpy the old states, because a nested state may contain a pointer into itself (ClickHouse#5751): it allocates a fresh arena buffer, creates new_size fresh states, merges the old_size old ones into them, and swaps the pointer. It never destroyed the old states. After the swap nothing holds a pointer to them, because destroyImpl walks only array_of_aggregate_datas, so any heap memory an old nested state owned was unreachable and unfreed for the lifetime of the process. The arena is not the leak: the arena frees its own chunks, but a nested state's own allocation is freed only by its destructor. QuantileExact keeps five values inline and allocates past that, which is why each leaked object is exactly 64 bytes. Found by LeakSanitizer on master (Stress test (amd_asan_ubsan), commit a7cd483), which reported both allocation sites this predicts: QuantileExactBase::merge under ensureAggregateData, and QuantileExactBase::add under add. It reproduces on a plain ASan build with a single query and no stress phase, five times out of five. The destroy runs strictly after the pointer swap: destroying before it would double-free if any path reached destroyImpl with the state still pointing at the old buffer, and IAggregateFunction::destroy is noexcept so nothing in between can throw. The nested_size_of_data != 0 guard mirrors the one on the merge loop above, because for a zero-size nested state the zero-byte arena allocation does not advance and the new buffer aliases the old one. The second hunk makes the migration loop exception-safe. QuantileExactBase merge allocates, so a throw there previously unwound with the state still pointing at the old buffer and leaked every freshly created state. Measured on a fixture that throws mid-loop, that path leaks 12 to 29 MB against zero for the fixed build. The cleanup bound is new_size rather than old_size, because every new state was created by this frame and is owned by it until the swap. Present since the create-then-merge design was introduced in bd33bf8. The same fix was proposed in ClickHouse#60683 and closed in 2024 for want of a test rather than on its merits; the reproduction is what that PR lacked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix potential memory leaks in AggregateFunctionForEach