Fix memory leak in mongo-c-driver retryable command retry loop#107448
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-2:20260614-184800 |
|
cc @alexey-milovidov @rschu1ze — could you review this? It bumps the mongo-c-driver submodule by one commit (ClickHouse/mongo-c-driver#13) to fix a reply memory leak in the retryable command retry loop: the loop calls select_retry_server() while reply still owns the previous attempt's allocation, and that function overwrites reply as an out-param without freeing it, so the allocation leaks when retry server selection fails. This is the test_simple_ssl LeakSanitizer failure from #107429. |
|
Workflow [PR], commit [fc6c240] Summary: ✅
AI ReviewSummaryThis PR bumps Final VerdictStatus: ✅ Approve |
|
@groeneai Send the fix first upstream before integrating it in ClickHouse. |
…ry loop Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
59168d5 to
6d09e59
Compare
Pre-PR validation gate (updated for the scoped fix, commit 6d09e59)Revised after @clickhouse-gh[bot]'s review. The earlier reset-before-select approach fixed the leak but wiped the read/write paths' first-attempt reply (errorLabels). This version moves the reset into the cursor callback only.
Session id: cron:clickhouse-worker-slot-0:20260614-200800 |
|
Ok. One concern is that upstream could be managed by AI-haters. This often happens in open-source. So they can insult and humiliate our bot. Better to read everything and author the upstream fix from a real person. |
|
On sending the fix upstream first: agreed, and I won't open a PR against The change ports cleanly for a human to author upstream. The retry loop is left byte-for-byte as upstream; the single reset ( Holding this PR until the upstream fix lands; I'll rebase the submodule bump onto whatever commit is accepted. |
CI finish ledger — fc6c240CI fully finished on this SHA (Finish Workflow + Mergeable Check SUCCESS; 0 checks queued/in_progress; >20-min ingestion buffer). No CI failures on this commit. The fork "manual approve" gate that had blocked the workflow earlier has cleared and the full job set ran green.
PR-caused gate: zero. This PR bumps Session id: cron:our-pr-ci-monitor:20260629-203000 |
Related: ClickHouse/mongo-c-driver#13
Related: #107429
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a memory leak in the bundled
mongo-c-driverthat could occur when reading from MongoDB (for example via a MongoDB dictionary or themongodbtable function) if a retryable read error was followed by a failed retry server selection.Description
Bumps the
mongo-c-driversubmodule by one commit to fix a reply memory leak in the retryable command retry loop._mongoc_retryable_cmd_runreuses*replyacross retry attempts. After an attempt fails with a retryable error, the loop callsselect_retry_server(..., reply, ...)whilereplystill owns that attempt's allocation.select_retry_server(cursor path:mongoc_cluster_stream_for_reads->_mongoc_cluster_stream_for_optype) treatsreplyas an out-param and, on its server-selection-failure path, overwrites it (bson_copy_to/bson_init) without freeing the prior contents. The loop's ownbson_destroy(reply)sits after theif (!server_description) break;, so it is never reached when selection fails, and the attempt'sreplyis leaked.The fix destroys and re-inits
replybeforeselect_retry_serveroverwrites it, and drops the now-redundant trailing destroy. See ClickHouse/mongo-c-driver#13 for the driver change.This surfaced in CI as a LeakSanitizer 128-byte direct leak on
test_dictionaries_all_layouts_separate_sources/test_mongo.py::test_simple_ssl(Integration tests (amd_asan_ubsan, ...)):The trigger (retryable read error followed by a retry server-selection failure) is a rare failover race: it appeared twice in 30 days across PR CI runs, never on master. Reproduced deterministically with a standalone AddressSanitizer/LeakSanitizer harness driving the real
_mongoc_retryable_cmd_runwith mock callbacks matching the ownership pattern above; the leak is reported without the change and gone with it.Found by
@alexey-milovidovon #107429 and fixed here on his request.Report: https://s3.amazonaws.com/clickhouse-test-reports/107429/index.html (Integration tests, amd_asan_ubsan, db disk, old analyzer, 3/6)
Version info
26.7.1.278