Skip to content

Fix potential memory leaks in AggregateFunctionForEach - #60683

Closed
HappenLee wants to merge 1 commit into
ClickHouse:masterfrom
HappenLee:bug
Closed

HappenLee wants to merge 1 commit into
ClickHouse:masterfrom
HappenLee:bug

Conversation

@HappenLee

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix potential memory leaks in AggregateFunctionForEach

@alexey-milovidov

Copy link
Copy Markdown
Member

Missing test.

@alexey-milovidov alexey-milovidov self-assigned this Mar 2, 2024
@alexey-milovidov
alexey-milovidov marked this pull request as draft March 3, 2024 00:46
@alexey-milovidov alexey-milovidov added the close in a month if not active This will be closed in case of no information label Mar 3, 2024
@HappenLee
HappenLee marked this pull request as ready for review March 3, 2024 01:53
@alexey-milovidov
alexey-milovidov marked this pull request as draft March 3, 2024 02:49
@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, waiting for tests.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, could you please add tests?

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, we need tests.

@alexey-milovidov

Copy link
Copy Markdown
Member

Hi, @HappenLee! A change requires a test to be accepted.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, the change needs to be proven by some tests.

@alexey-milovidov

Copy link
Copy Markdown
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"should to" is incorrect.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, you didn't explain.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, We need to prove this change with a test.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee We need to know the scenario reproducing the hypothetical problem.

@alexey-milovidov

Copy link
Copy Markdown
Member

@HappenLee, we need more information about this change.

@Algunenano

Copy link
Copy Markdown
Member

Abandoned

@Algunenano Algunenano closed this Apr 19, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

close in a month if not active This will be closed in case of no information

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants