Skip to content

Fix a possible segmentation fault in AvroConfluent when reading an Avro map into a Map column - #125505

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/avro-confluent-pin-cached-deserializer
Oct 15, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/avro-confluent-pin-cached-deserializer

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Fixed a possible segmentation fault when reading an Avro map into a Map column (for example Map(DateTime, String)) with the AvroConfluent format, including Kafka tables. Closes #125457.

Description

Closes: #125457

On 26.3, 26.8 and 26.9 (the same code as master), reading an Avro map into a Map(K, V) column with AvroConfluent reads the format settings through a destroyed stack object for every map key. The issue reports a segfault on a 26.10 master build with Map(DateTime, String); the 26.3.42.3, 26.8.18.2 and 26.9.9.28 release images I tried returned correct rows, so whether it crashes depends on what is left in the dead stack slot.

AvroConfluentRowInputFormat::getOrCreateDeserializer built a local AvroDeserializer and moved it into deserializer_cache. The map-key closure created during construction captures this, and moving a std::function keeps that pointer, so the cached closure later reads settings through the returned frame. #104751 fixed the other self-reference of this object by switching the cache from a copy to a move; this PR makes AvroDeserializer non-movable and constructs the cache entry in place with try_emplace (std::unordered_map never relocates its nodes). Plain Avro and the Iceberg manifest reader never move their deserializer and were not affected.

Validation: the new test fails on master's ASan build (stack-use-after-return in the map-key lambda at AvroRowInputFormat.cpp:897, in the frame of getOrCreateDeserializer) and passes with the fix (ASan 10/10, debug 50/50). The old emplace(schema_id, std::move(deserializer)) no longer compiles.


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

Version info

  • Merged into: 26.10.1.2441-master (included in 26.10 and later)
  • Backported to: 26.9.20.15, 26.8.28.14, 26.7.31.13, 26.3.52.11

groeneai and others added 2 commits October 12, 2026 20:17
AvroConfluentRowInputFormat::getOrCreateDeserializer built a stack-local
AvroDeserializer and moved it into deserializer_cache. The AVRO_MAP key
closure created while constructing it captures `this` (ClickHouse#48119), and moving
a std::function keeps that pointer, so every map key read `settings`
through the frame of the returned function: stack-use-after-return under
ASan for any key type, and a segfault for key types whose text parser
dereferences FormatSettings (DateTime, Date, ...) on some builds.

ClickHouse#104751 already replaced the copy into the cache by a move to keep the
other self-reference (references into symbolic_skip_fn_map) valid. Finish
it: delete the move constructor and construct the cache entry in place
with try_emplace; unordered_map never relocates its nodes, so both
self-references stay valid, and relocating the object no longer compiles.

Closes ClickHouse#125457

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test reserved a port by binding port 0 in one process, closing it and binding the same
number again in the schema registry process; a parallel process could take the port in
between. The registry now binds port 0 itself and writes the port it got to a file (atomically,
via os.replace), and the test waits for that file. TCPServer binds and listens in its
constructor, so the server accepts connections as soon as the file exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review: my own cold review plus an independent model (engine: codex); 5 findings;
bounded: 1 full pass + 1 fix round + 1 delta recheck (the recheck raised nothing new).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test reserved the registry port by binding port 0, closing it and binding it again in another process, so a parallel run could take it in between AGREE, fixed @ e451bee The registry binds port 0 itself and reports the port through a file, as in 04615_kill_query_url_function.sh
2 ⚠️ The repeated runs that were meant to show parallel safety used one worker AGREE, re-run 50/50 OK with 16 workers (debug), 10/10 OK with 10 workers (ASan)
3 💡 AvroSerializer (output side) has the same this capture in its map-key lambda DISAGREE It is a direct member of both output formats, built in the member-init list and never moved (AvroRowOutputFormat.cpp:657, :708)
4 💡 The test only reddens under sanitizers; release builds return correct rows without the fix DISAGREE Not a defect: CI's ASan lanes and Bugfix validation's master ASan builds run it; without the fix the ASan run fails with stack-use-after-return
5 💡 The new header comment also names symbolic_skip_fn_map references, which a std::map move keeps valid DROPPED The comment lists what the closures capture, which is true; the map references are why copying is deleted

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-9:20261012-205200

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The issue's data piped into clickhouse local --input-format AvroConfluent -S "id Int64, m Map(DateTime('UTC'), String)" aborts master's ASan build with stack-use-after-return on the first map row, every run, for any key type (also Map(String, String)).
b Root cause explained? getOrCreateDeserializer moved a stack-local AvroDeserializer into deserializer_cache. Its map-key closure captures this, and moving a std::function keeps the pointer, so the cached closure reads settings through the returned frame (AvroRowInputFormat.cpp:897).
c Fix matches root cause? Yes. The object is pinned: move constructor deleted, cache entry constructed in place with try_emplace. Both self-references (this and the symbolic_skip_fn_map node references) stay valid for the entry's life, and relocating it is now a compile error.
d Test intent preserved / new tests added? New 05385_avro_confluent_map_datetime_key.sh (a schema registry that binds port 0 and reports it through a file, + clickhouse-local). It fails on master's ASan build and passes with the fix.
e Both directions demonstrated? Yes, by command and through tests/clickhouse-test: unfixed ASan return code: 134 with the ASan report, fixed ASan OK (10/10, --jobs 10), fixed debug OK (50/50, --jobs 16). Restoring the old emplace(schema_id, std::move(deserializer)) fails to compile.
f Fix is general across code paths? All three users of AvroDeserializer checked: the Confluent cache (fixed), AvroRowInputFormat (make_unique, never moved), the Iceberg manifest reader (scoped local, never moved). The output side AvroSerializer also captures this but is a direct member of its format and never moved.
g Fix generalizes across inputs (params/datatypes/wrappers)? The fault is in the object's lifetime, not in a key type: every key type and nesting reaches the same pinned closure. Checked 40 schema ids interleaved over 200 messages (cache rehashing) under ASan: correct result, no report.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format, wire or on-disk change.
i Invariants and contracts preserved? Closures stored in an AvroDeserializer only refer to memory that lives as long as the object: unordered_map nodes are stable across rehash, and settings refers to the input format's member. A throwing constructor inside try_emplace inserts nothing, as before.

Session id: cron:clickhouse-impl-slot-4:20261012-192800

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f48bce5]

Summary: ✅


AI Review

Summary

This PR fixes the AvroConfluent deserializer lifetime bug by constructing cached AvroDeserializer instances in place and making the type non-movable, which closes the stored-lambda this/member-reference hazard described in the issue. The new stateless test exercises the failing Map(DateTime('UTC'), String) path through a local schema registry and provides sufficient regression coverage for the reported Bug Fix.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 12, 2026
@groeneai
groeneai requested review from Avogar and mstetsyuk October 12, 2026 21:10
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Oct 12, 2026
The port file name depended only on CLICKHOUSE_TEST_UNIQUE_NAME. Under a
fixed `--database` (the stress test's odd workers loop tests that way) a
file left by a SIGKILLed earlier run was read before the new registry
published its port, so the query hit a dead port. mktemp makes the name
unique per run, as in 04615_kill_query_url_function.sh.

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

clickhouse-gh Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f48bce50b with master af433c160 (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - f48bce5

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (arm_binary, 1/4) / test_storage_iceberg_concurrent/test_concurrent_reads.py::test_concurrent_reads not from this PR (it only changes how AvroConfluent caches its deserializer): Spark's CREATE TABLE at test_concurrent_reads.py:23 fails with Failed to refresh the table, because the test_storage_delta/test_writes_interop.py module ran in the same job and its reused Spark JVM kept an s3a filesystem bound to that module's MinIO; the fix merged after the tree of this run was pinned #125448 (mine, merged)
Mergeable Check, PR praktika rollups of the row above #125448 (mine, merged)
CH Inc sync - CH Inc sync (private, not actionable by me)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261013-060009

@Algunenano Algunenano self-assigned this Oct 14, 2026
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 14, 2026

@Algunenano Algunenano 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.

The test is a bit complex, but it is what it is. Rest looks great

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit 39e872e Oct 15, 2026
350 of 351 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 15, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125505 to 26.9: Fix a possible segmentation fault in `AvroConfluent` when reading an Avro `map` into a `Map` column
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125505 to 26.8: Fix a possible segmentation fault in `AvroConfluent` when reading an Avro `map` into a `Map` column
clickhouse-gh Bot added a commit that referenced this pull request Oct 15, 2026
Backport #125505 to 26.7: Fix a possible segmentation fault in `AvroConfluent` when reading an Avro `map` into a `Map` column
Algunenano pushed a commit that referenced this pull request Oct 15, 2026
…onflict

The only conflict is in `AvroConfluentRowInputFormat::getOrCreateDeserializer`.
On master the `getSchema` call takes the registry timeouts and retry settings
added by #101935 (AvroConfluent output format), which is not on 26.3.

Resolution: keep 26.3's one-argument `schema_registry->getSchema(schema_id)` and
take #125505's change as is: build the cached `AvroDeserializer` in place with
`try_emplace` instead of moving a stack-local one into the cache. The header and
the test are unchanged from the PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-ch-test-poll3 added a commit that referenced this pull request Oct 15, 2026
Cherry pick #125505 to 26.3: Fix a possible segmentation fault in `AvroConfluent` when reading an Avro `map` into a `Map` column
robot-clickhouse added a commit that referenced this pull request Oct 15, 2026
…onfluent` when reading an Avro `map` into a `Map` column
@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 Oct 15, 2026
Algunenano added a commit that referenced this pull request Oct 15, 2026
Backport #125505 to 26.3: Fix a possible segmentation fault in `AvroConfluent` when reading an Avro `map` into a `Map` column
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2026
… in `AvroConfluent` when reading an Avro `map` into a `Map` column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AvroConfluent SIGSEGV reading Avro map into Map(DateTime, String)

6 participants