Repository navigation
Fix possible OOM due to a too-large string literal sent within the query - #105996
Conversation
|
Workflow [PR], commit [84e7efd] Summary: ✅
AI ReviewSummaryThis PR moves the normal native TCP query-body receive path to Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Minimum required actions: eliminate the remaining full-query plain |
|
|
||
| bool Connection::poll(size_t timeout_microseconds) | ||
| { | ||
| /// `disconnect()` resets `in`, so reaching here from an outer loop after |
There was a problem hiding this comment.
First it crashed right after OOM with the following stack:
(version 26.6.1.1, build id: 43EFBE2551DD08C486BD9577020C1D769CD96B4F, git hash: ce552bdb05e1113e84d1b570f849fda27551a6d7) (from thread 262524) (no query) Received signal Segmentation fault (11)
Address: 0x108. Access: read. Address not mapped to object.
Stack trace: 0x000062a39d13e305 0x000062a39d0f2b73 0x000062a39d0f1717 0x000062a39d0eef20 0x000062a39d0fb65c 0x000062a39d0fc9f7 0x000062a39d108e0e 0x000062a393647e6b 0x000062a3a20758b4 0x000062a393656e8e 0x000062a38c9fb21e 0x00007f1f3602a1ca 0x00007f1f3602a28b 0x000062a38c9dc66e
3. src/IO/ReadBufferFromPocoSocketChunked.cpp:47:9: DB::ReadBufferFromPocoSocketChunked::poll(unsigned long) const @ 0x000000001988a305
4. src/Client/ClientBase.cpp:1485:29: DB::ClientBase::receiveResult(boost::intrusive_ptr<DB::IAST>, int, bool) @ 0x000000001983eb73
5. src/Client/ClientBase.cpp:1385:17: DB::ClientBase::processOrdinaryQuery(String, boost::intrusive_ptr<DB::IAST>) @ 0x000000001983d717
6. src/Client/ClientBase.cpp:2479:13: DB::ClientBase::processParsedSingleQuery(std::basic_string_view<char, std::char_traits<char>>, boost::intrusive_ptr<DB::IAST>, bool&, unsigned long) @ 0x000000001983af20
7. src/Client/ClientBase.cpp:2859:21: DB::ClientBase::executeMultiQuery(String const&) @ 0x000000001984765c
8. src/Client/ClientBase.cpp:3121:12: DB::ClientBase::processQueryText(String const&) @ 0x00000000198489f7
9. src/Client/ClientBase.cpp:3950:13: DB::ClientBase::runNonInteractive() @ 0x0000000019854e0e
10. programs/client/Client.cpp:439:9: DB::Client::main(std::vector<String, std::allocator<String>> const&) @ 0x000000000fd93e6b
11. base/poco/Util/src/Application.cpp:315:8: Poco::Util::Application::run() @ 0x000000001e7c18b4
12. programs/client/Client.cpp:1372:23: mainEntryClickHouseClient(int, char**) @ 0x000000000fda2e8e
13. programs/main.cpp:475:21: main @ 0x000000000914721e
There was a problem hiding this comment.
Pls merge it, and I will rebase on top of it.
There was a problem hiding this comment.
@azat, we might as well merge this PR in its current state since 105386 isn't moving anywhere. I don't really like the idea of leaving green PRs hanging for weeks.
There was a problem hiding this comment.
Ok, but let's make it LOGICAL_ERROR, since it is responsibility of the caller not to use connection after disconnect, and it happens usually in some try/catch blocks.
There is already such check in receivePacket (added in #95466) but it is not enough
Let's add some helper and verify the state of "in" there
P.S. and also don't forget to adjust "receivePacketType"
|
|
||
| std::pair<ASTPtr, BlockIO> executeQuery( | ||
| const String & query, | ||
| std::string_view query, |
There was a problem hiding this comment.
This avoids the function-boundary String copy, but the query can still be duplicated outside a throwing allocator a bit later. For a native client that sends a large query with max_query_size = 0, or for a non-secret SECONDARY_QUERY where executeQueryImpl forces max_query_size = 0, state->query is read with StringWithMemoryTracking and then executeQueryImpl can reach query.assign(begin, query_end) for logging/processlist. That allocates a full plain String; with the default min_allocation_size_to_throw_on_memory_limit = 0, this path is accounted but not refused by the memory tracker, so it can still push RSS past max_server_memory_usage after the tracked receive succeeds.
Can we avoid materializing the full query into plain String here, for example by deriving query_for_logging from a bounded slice/view or by using tracked storage for this copy, and add a regression with max_query_size = 0 or a secondary query?
| /// cleanly, rather than driving the server's RSS past | ||
| /// `max_server_memory_usage` via `allocNoThrow` and getting | ||
| /// cgroup-OOM-killed. | ||
| StringWithMemoryTracking query; |
There was a problem hiding this comment.
This tracks the normal Query packet body, but the same native-protocol string is still read into a plain String on the unexpected-packet path. A client can send a query that makes the server enter receivePacketsExpectData, then send another Query packet; processUnexpectedQuery reads both the query id and the query body into String skip_string before throwing UNEXPECTED_PACKET_FROM_CLIENT. The query-body read at src/Server/TCPHandler.cpp:2577 still resizes a plain String, so an oversized malformed packet can take the non-refusing allocation path and push RSS past max_server_memory_usage before the handler rejects it.
Can we skip these encoded strings without materializing them, or at least use StringWithMemoryTracking on this path too?
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 24/29 (82.76%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
|
I've considered the two auto-suggestions above, and I think we shouldn't try to solve every possible problem here; instead, we're better off continuing to apply more WMT containers, including the Client/Server code, to close the remaining gaps. |
There was a problem hiding this comment.
The flaky check was done locally:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ok [Integration tests (arm_binary, distributed plan, 3/4)]
| Failures: 0/50
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Cherry pick #105996 to 26.3: Fix possible OOM due to a too-large string literal sent within the query
Cherry pick #105996 to 26.4: Fix possible OOM due to a too-large string literal sent within the query
Cherry pick #105996 to 26.5: Fix possible OOM due to a too-large string literal sent within the query
Cherry pick #105996 to 25.8: Fix possible OOM due to a too-large string literal sent within the query
…literal sent within the query
…literal sent within the query
…literal sent within the query
…literal sent within the query
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix possible OOM due to a too-large string literal sent within the query ClickGap verified: reproduced it with the script below on ReproductionBisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: ClickGap · Finding: |
Backport #105996 to 26.5: Fix possible OOM due to a too-large string literal sent within the query
Backport #105996 to 26.4: Fix possible OOM due to a too-large string literal sent within the query
…eded the fix Per @azat's directive (ClickHouse#105386 (comment)), PR ClickHouse#105996 ("Fix possible OOM due to a too-large string literal sent within the query", merged 2026-06-04T15:07:46Z) supersedes the source-code fix in this PR by adding a `Connection::ensureConnected()` helper and an early-return guard at the top of `ClientBase::receiveResult` for the disconnected case. Both halves together prevent the same segfault this PR was originally addressing. Drop the remaining `ClientBase.cpp` change (the `connection->isConnected()` guard inside the `processOrdinaryQuery` `NetException` catch block) — it is strictly subsumed by the new check in `receiveResult`. Keep the regression test as forward-looking coverage for issue ClickHouse#105292: - tests/queries/0_stateless/04319_client_segfault_send_error_105292.sh - tests/queries/0_stateless/04319_client_segfault_send_error_105292.reference - tests/queries/0_stateless/helpers/04319_client_segfault_proxy.py Renumbered 04260 -> 04319 (next free slot on master; 04260 already taken by several merged tests). Verified locally against post-ClickHouse#105996 master: 5/5 iterations of the test report `CLIENT_EXIT=210` with `Code: 210. DB::NetException: Connection reset by peer ... (NETWORK_ERROR)` instead of the original SIGSEGV at Address 0x108 in `ReadBufferFromPocoSocketChunked::poll`. The PR's `Bug Fix` changelog category will be updated in a follow-up to `CI Fix or Improvement (changelog entry is not required)` because the source fix lives in ClickHouse#105996; this PR's scope is now regression test coverage only. Closes: ClickHouse#105292 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…rcised the regression path The previous version of the test could pass without ever hitting the mid-write cutoff path that triggered issue ClickHouse#105292: 1. The proxy returned normally (no exit-code signal) on every early-exit path (target-connect failure, accept timeout, client EOF before cutoff). The shell wrapper accepted any non-zero clickhouse-client exit that mentioned NETWORK_ERROR / Broken pipe / Connection reset by peer, so a failure that occurred BEFORE Connection::sendQuery (e.g. during the native handshake) would still produce one of those markers and falsely "pass" the test. 2. The proxy invocation used `${CLICKHOUSE_CLIENT} ... --host=127.0.0.1 --port=${PROXY_PORT}`. In CI ${CLICKHOUSE_CLIENT} already carries the runner-injected `--host=` and `--port=` of the real server, and Boost program_options keeps the FIRST occurrence of `--port`. The appended proxy port was silently dropped, the client connected directly to the server, the INSERT succeeded, CLIENT_EXIT=0, and the wrapper would have hit its `CLIENT_EXIT=0` BAD branch — so the test could only pass on a different early-error path, not on the regression path. Both issues are fixed: - The proxy now exits 0 ONLY after forwarding more than `cutoff` bytes AND injecting the RST. Every other path (target-connect failure, accept timeout, EOF / recv error / send error before cutoff) prints a labelled marker and exits 1. - The shell wrapper waits for the proxy, captures PROXY_EXIT, and refuses to declare OK unless the proxy emitted CUTOFF_EXCEEDED and exited 0. - The proxy-bound clickhouse-client invocation now uses ${CLICKHOUSE_CLIENT_BINARY} directly instead of ${CLICKHOUSE_CLIENT}, so the proxy `--host=` / `--port=` are the only host/port flags and are honoured. Verified on a debug build, post-ClickHouse#105996 master: 20/20 PASS with the new assertions; 1/1 deterministic FAIL when cutoff is set above the INSERT size (CLIENT_EOF_BEFORE_CUTOFF surfaces and the wrapper rejects it). Related: ClickHouse#105386 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Backport #105996 to 26.3: Fix possible OOM due to a too-large string literal sent within the query
…ge string literal sent within the query
…5996 Backport ClickHouse#105996 to 26.4: Fix possible OOM due to a too-large string literal sent within the query
…eded the fix Per @azat's directive (ClickHouse#105386 (comment)), PR ClickHouse#105996 ("Fix possible OOM due to a too-large string literal sent within the query", merged 2026-06-04T15:07:46Z) supersedes the source-code fix in this PR by adding a `Connection::ensureConnected()` helper and an early-return guard at the top of `ClientBase::receiveResult` for the disconnected case. Both halves together prevent the same segfault this PR was originally addressing. Drop the remaining `ClientBase.cpp` change (the `connection->isConnected()` guard inside the `processOrdinaryQuery` `NetException` catch block) — it is strictly subsumed by the new check in `receiveResult`. Keep the regression test as forward-looking coverage for issue ClickHouse#105292: - tests/queries/0_stateless/04319_client_segfault_send_error_105292.sh - tests/queries/0_stateless/04319_client_segfault_send_error_105292.reference - tests/queries/0_stateless/helpers/04319_client_segfault_proxy.py Renumbered 04260 -> 04319 (next free slot on master; 04260 already taken by several merged tests). Verified locally against post-ClickHouse#105996 master: 5/5 iterations of the test report `CLIENT_EXIT=210` with `Code: 210. DB::NetException: Connection reset by peer ... (NETWORK_ERROR)` instead of the original SIGSEGV at Address 0x108 in `ReadBufferFromPocoSocketChunked::poll`. The PR's `Bug Fix` changelog category will be updated in a follow-up to `CI Fix or Improvement (changelog entry is not required)` because the source fix lives in ClickHouse#105996; this PR's scope is now regression test coverage only. Closes: ClickHouse#105292 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…rcised the regression path The previous version of the test could pass without ever hitting the mid-write cutoff path that triggered issue ClickHouse#105292: 1. The proxy returned normally (no exit-code signal) on every early-exit path (target-connect failure, accept timeout, client EOF before cutoff). The shell wrapper accepted any non-zero clickhouse-client exit that mentioned NETWORK_ERROR / Broken pipe / Connection reset by peer, so a failure that occurred BEFORE Connection::sendQuery (e.g. during the native handshake) would still produce one of those markers and falsely "pass" the test. 2. The proxy invocation used `${CLICKHOUSE_CLIENT} ... --host=127.0.0.1 --port=${PROXY_PORT}`. In CI ${CLICKHOUSE_CLIENT} already carries the runner-injected `--host=` and `--port=` of the real server, and Boost program_options keeps the FIRST occurrence of `--port`. The appended proxy port was silently dropped, the client connected directly to the server, the INSERT succeeded, CLIENT_EXIT=0, and the wrapper would have hit its `CLIENT_EXIT=0` BAD branch — so the test could only pass on a different early-error path, not on the regression path. Both issues are fixed: - The proxy now exits 0 ONLY after forwarding more than `cutoff` bytes AND injecting the RST. Every other path (target-connect failure, accept timeout, EOF / recv error / send error before cutoff) prints a labelled marker and exits 1. - The shell wrapper waits for the proxy, captures PROXY_EXIT, and refuses to declare OK unless the proxy emitted CUTOFF_EXCEEDED and exited 0. - The proxy-bound clickhouse-client invocation now uses ${CLICKHOUSE_CLIENT_BINARY} directly instead of ${CLICKHOUSE_CLIENT}, so the proxy `--host=` / `--port=` are the only host/port flags and are honoured. Verified on a debug build, post-ClickHouse#105996 master: 20/20 PASS with the new assertions; 1/1 deterministic FAIL when cutoff is set above the INSERT size (CLIENT_EOF_BEFORE_CUTOFF surfaces and the wrapper rejects it). Related: ClickHouse#105386 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a possible crash due to a too-large string literal sent within the query.
Version info
26.6.1.39126.5.2.35,26.4.4.33,26.3.13.31