Skip to content

Erase sorted_dynamic_paths entries before dynamic_paths ones - #100758

Merged
Ergus merged 12 commits into
ClickHouse:masterfrom
Ergus:Fix_Incident_1512
Apr 30, 2026
Merged

Ergus merged 12 commits into
ClickHouse:masterfrom
Ergus:Fix_Incident_1512

Conversation

@Ergus

@Ergus Ergus commented Mar 25, 2026 •

Copy link
Copy Markdown
Member

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 into CHANGELOG.md):

Fix an exception that could occur when inserting a row into a JSON column where the row introduced a new dynamic field alongside a typed field with an incompatible value, and the column was subsequently used as a GROUP BY key.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Attempt to fix incident 1512.
/fix https://github.com/ClickHouse/clickhouse-core-incidents/issues/1512

ColumnObject::tryInsert contains a restore_sizes rollback lambda that removes newly-added dynamic paths when the insert fails mid-object. It correctly erases each path from dynamic_paths and dynamic_paths_ptrs, but not erase it from sorted_dynamic_paths first.

sorted_dynamic_paths is an std::set<std::string_view> whose string_views point into the std::string keys owned by dynamic_paths.

After dynamic_paths.erase(path) frees the key string, the corresponding view in sorted_dynamic_paths becomes dangling.

The bug only fires when all of the following are true:

  1. tryInsert is called on a ColumnObject with an object that contains a path not yet in the column (triggering tryToAddNewDynamicPath).
  2. a later field in the same object causes the insert to fail (e.g. a typed path rejects the value)
  3. the same ColumnObject is later serialised via serializeValueIntoArena (GROUP BY keys) or has another dynamic path added (triggering set comparison).

The crash manifests as a segfault deep inside SerializationObject::serializeBinaryBulkStatePrefix when iterating or calling .size() on dynamic_paths, making the root cause non-obvious without ASAN.

Version info

  • Merged into: 26.5.1.185
  • Backported to: 26.4.1.1138, 26.3.11.12, 26.2.19.16, 25.8.24.21

@clickhouse-gh

clickhouse-gh Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5e92a4e]

Summary: ✅


AI Review

Summary

This PR fixes rollback correctness in ColumnObject::tryInsert by tracking newly-added dynamic paths and erasing sorted_dynamic_paths entries before erasing owning strings from dynamic_paths, plus adds a focused regression test for the failing insert path. The change is small, targeted, and matches the reported failure mode.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 25, 2026
@Ergus
Ergus requested review from Avogar and Copilot March 25, 2026 22:38
@Avogar Avogar self-assigned this Mar 25, 2026
Comment thread src/Columns/ColumnObject.cpp Resolved
@Avogar

Avogar commented Mar 25, 2026

Copy link
Copy Markdown
Member

Can we add a test somehow? At least a new unit test in [gtest_column_object.cpp](https://github.com/ClickHouse/ClickHouse/blob/master/src/Columns/tests/gtest_column_object.cpp)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a potential use-after-free in ColumnObject::tryInsert rollback by ensuring sorted_dynamic_paths (a std::set<std::string_view>) does not retain views into erased dynamic_paths keys.

Changes:

  • Update ColumnObject::tryInsert rollback to erase entries from sorted_dynamic_paths before erasing from dynamic_paths.

Comment thread src/Columns/ColumnObject.cpp Resolved
Comment thread src/Columns/ColumnObject.cpp Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

Comment thread src/Columns/ColumnObject.cpp Resolved

@Avogar Avogar left a comment

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.

Let's write a Changelog entry and fix comments in the test as bot suggested

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

@npakeer

npakeer commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

@Ergus
Ergus deleted the Fix_Incident_1512 branch April 30, 2026 02:39
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: ColumnObject::tryInsert rollback (restore_sizes lambda) — core code present in all supported branches.

Why: The bug exists in ColumnObject::tryInsert rollback logic. git log -S 'sorted_dynamic_paths' -- src/Columns/ shows the data structure and code pattern existed since 24.8 (commits f1377b0 'Fix uniq and GROUP BY for JSON/Dynamic types' and 8734b91 backport to 24.8). All currently supported release branches (25.8 LTS, 26.1, 26.2, 26.3 LTS) contain the unfixed tryInsert rollback. The PR has no backport label set yet (only pr-bugfix), so explicit backport routing is needed. The fix is small (2-line code change) and self-contained.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 30, 2026
@npakeer npakeer added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label May 4, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 4, 2026
Cherry pick #100758 to 25.8: Erase sorted_dynamic_paths entries before dynamic_paths ones
robot-clickhouse added a commit that referenced this pull request May 4, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 4, 2026
Cherry pick #100758 to 26.2: Erase sorted_dynamic_paths entries before dynamic_paths ones
robot-clickhouse added a commit that referenced this pull request May 4, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 4, 2026
Cherry pick #100758 to 26.3: Erase sorted_dynamic_paths entries before dynamic_paths ones
robot-clickhouse added a commit that referenced this pull request May 4, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 4, 2026
Cherry pick #100758 to 26.4: Erase sorted_dynamic_paths entries before dynamic_paths ones
robot-clickhouse added a commit that referenced this pull request May 4, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 4, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 4, 2026
alexey-milovidov added a commit that referenced this pull request May 5, 2026
Backport #100758 to 26.4: Erase sorted_dynamic_paths entries before dynamic_paths ones
Avogar added a commit that referenced this pull request May 12, 2026
Backport #100758 to 26.3: Erase sorted_dynamic_paths entries before dynamic_paths ones
Avogar added a commit that referenced this pull request May 12, 2026
Backport #100758 to 26.2: Erase sorted_dynamic_paths entries before dynamic_paths ones
Avogar added a commit that referenced this pull request May 19, 2026
Backport #100758 to 25.8: Erase sorted_dynamic_paths entries before dynamic_paths ones
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…0758

Backport ClickHouse#100758 to 26.4: Erase sorted_dynamic_paths entries before dynamic_paths ones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants