Skip to content

Fix double destroy of aggregate states when create throws after destroy - #132755

Merged
Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:fix-aggregate-state-double-destroy
Nov 19, 2026
Merged

Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:fix-aggregate-state-double-destroy

Conversation

@Algunenano

@Algunenano Algunenano commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

When create of an aggregate state threw (for example MEMORY_LIMIT_EXCEEDED) right after the previous state in the same place had been destroyed, the owner destroyed the state a second time. This happened in AggregateState::recreate (aggregate functions used as window functions, on every new partition or frame) and in the -ArgMin / -ArgMax combinator when a new extreme key resets the nested state (add, merge, parallel merge). Depending on the state type this is a double free, which crashes the server, e.g. for flameGraph. Both now track whether the state is alive and skip the second destroy.

create of -ArgMin / -ArgMax, -Distinct and groupFormat also leaked the parts already built when a later part threw. They now destroy them before rethrowing.

The review skill gets a check for objects destroyed and re-created in place: the AI review missed this pattern when it was added to -ArgMin / -ArgMax.

Related: #132557

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 crash (double free) when an allocation failed, e.g. because of the memory limit, while an aggregate function used as a window function moved to a new partition or frame, or while an aggregate function with the -ArgMin / -ArgMax combinator reset its state for a new extreme value.


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

Version info

  • Merged into: 26.11.1.2900-master (included in 26.11 and later)
  • Backported to: 26.10.8.38, 26.9.34.18, 26.8.42.16, 26.3.69.3

Algunenano and others added 3 commits November 18, 2026 18:48
…stroy`

`AggregateState::recreate` and the `-ArgMin` / `-ArgMax` combinator destroyed a state and created it again in place; if `create` threw, the owner destroyed the state a second time. Track whether the state is alive. Also destroy the already built parts when `create` of `-ArgMin` / `-ArgMax` or `-Distinct` throws.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A review of a diff that added `destroy` then `create` of a nested aggregate state did not flag that a throwing `create` leads to a second `destroy`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The columns were added to the state after it was constructed in place, so an allocation that threw left them allocated with nobody to destroy them. Build them first and construct the state from them.

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

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0a914a9]

Summary: ✅


AI Review

Summary

The PR prevents double destruction when re-creating window aggregate states or resetting nested states in -ArgMin / -ArgMax fails, and cleans up partial construction in these combinators, -Distinct, and groupFormat. No actionable findings remain after reviewing the current diff, state ownership, exception cleanup, and prior discussion. The focused regression passed 50 ASan CI repetitions on 3678e018765f6b6fd5db97da23ce1b58615f0364; the reviewed source and regression test are unchanged by the subsequent merge commit.

Final Verdict

Status: ✅ Approve

@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 Nov 18, 2026
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0a914a919 with master 7210d2d96 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). label Nov 18, 2026
`checkCreated` was not inlined, so every row paid for a call with a full frame. Inline the test and keep only the throw out of line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Nov 19, 2026
Merged via the queue into ClickHouse:master with commit 025fc51 Nov 19, 2026
195 checks passed
@Algunenano
Algunenano deleted the fix-aggregate-state-double-destroy branch November 19, 2026 23:24
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 19, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 19, 2026
Algunenano added a commit that referenced this pull request Nov 20, 2026
`AggregateState` does not exist in this release, so the window part of the fix is ported to `WindowTransform`: the workspace tracks whether its state is alive and skips the second `destroy`. The review skill change is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano added a commit that referenced this pull request Nov 20, 2026
`AggregateState` does not exist in this release, so the window part of the fix is ported to `WindowTransform`: the workspace tracks whether its state is alive and skips the second `destroy`. The review skill change is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano added a commit that referenced this pull request Nov 20, 2026
`AggregateState` does not exist in this release, so the window part of the fix is ported to `WindowTransform`: the workspace tracks whether its state is alive and skips the second `destroy`. The review skill change is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Algunenano added a commit that referenced this pull request Nov 20, 2026
`AggregateState` does not exist in this release, so the window part of the fix is ported to `WindowTransform`: the workspace tracks whether its state is alive and skips the second `destroy`. The review skill change is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 20, 2026
Cherry pick #132755 to 26.3: Fix double destroy of aggregate states when `create` throws after `destroy`
robot-clickhouse added a commit that referenced this pull request Nov 20, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 20, 2026
Cherry pick #132755 to 26.8: Fix double destroy of aggregate states when `create` throws after `destroy`
robot-clickhouse added a commit that referenced this pull request Nov 20, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 20, 2026
Cherry pick #132755 to 26.9: Fix double destroy of aggregate states when `create` throws after `destroy`
robot-clickhouse added a commit that referenced this pull request Nov 20, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 20, 2026
Cherry pick #132755 to 26.10: Fix double destroy of aggregate states when `create` throws after `destroy`
robot-clickhouse added a commit that referenced this pull request Nov 20, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 20, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 20, 2026
Backport #132755 to 26.10: Fix double destroy of aggregate states when `create` throws after `destroy`
clickhouse-gh Bot added a commit that referenced this pull request Nov 20, 2026
Backport #132755 to 26.9: Fix double destroy of aggregate states when `create` throws after `destroy`
clickhouse-gh Bot added a commit that referenced this pull request Nov 20, 2026
Backport #132755 to 26.8: Fix double destroy of aggregate states when `create` throws after `destroy`
Algunenano added a commit that referenced this pull request Nov 21, 2026
Backport #132755 to 26.3: Fix double destroy of aggregate states when `create` throws after `destroy`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregate-functions Aggregate function implementations (sum, avg, count, quantile, combinators, etc.). 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-performance Pull request with some performance 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.

3 participants