Repository navigation
Two requests with one X-Request-Id no longer fail with a 500 - #205
Merged
Merged
Conversation
zlareb1
force-pushed
the
fix/request-id-query-ids
branch
5 times, most recently
from
November 3, 2026 13:18
8de42ef to
def3ebb
Compare
Every statement's query_id was <request_id>:<n>, so two requests sent at once with the same caller id sent the same query_id and ClickHouse refused the second (QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING). A per-request nonce makes it <request_id>:<n>:<nonce>. A caller id that is not 1-64 letters, digits, '.', '_' or '-' is replaced by a generated one: a space split the query tag and query_id kept only the first word. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A caller id the server would replace was sent and logged as given while the server recorded its own, so the SDK's log lines named an id the server never saw. Both SDKs now send a generated id in its place. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…names the id form Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…est statement's query_id Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…DK replaces it Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…equal Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…test imports at the top Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…the nonce Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
fix/request-id-query-ids
branch
from
November 3, 2026 13:41
2499b50 to
04391e0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
Two requests sent at once with the same
X-Request-Id(an SDK retry of a call still in flight, for example) no longer fail with a 500. AnX-Request-Idthat is not 1 to 64 letters, digits,.,_or-is replaced by a generated id, which the response echoes.Why
The caller's
X-Request-Idwas used as given in two places:log_comment = "<op> <request_id>");query_id(<request_id>:<n>).That breaks in two ways:
query_id = <id>:1,<id>:2and so on. ClickHouse refuses aquery_idthat is already running (code 216,QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING), and the request answers 500.main: 6 concurrentmemory.searchcalls withX-Request-Id: probe-dup-7gave 4 answers of 500 and 2 of 200, with 18 statements failing with code 216 insystem.query_log.X-Request-Idacross their own retries, so a retry of a slow call that is still running on the server can hit this.X-Request-Id: probe spacegavequery_id = probe:N, the first word only, whilelog_commentand the ledger carriedprobe space. The join between them broke, and the id collided with any other request whose id isprobe.The fix has two parts:
query_id = <request_id>:<n>:<nonce>, where the nonce is 8 hex characters drawn once per request. The id stays the first field of a request statement'squery_id(a carried insert prefixes it withloom-carry/<process>/), andlog_commentstill carries it.[A-Za-z0-9._-]{1,64}is replaced by a uuid4, which the response'sX-Request-Idreturns. That covers the space and the 120-character tag cap, which used to truncate long ids.Scope and risk
server/transport/_common.py(request_id_from), the two placesserver/transport/http.pyread the header (ops and/v1/me/*),server/core/request_work.pyandserver/core/ch_request_cost.py.request_id/requestIdand send a generated id in its place. The id the SDK logs and puts on its errors is then the id the server records.:,/, non-ASCII) or longer than 64 characters now gets a different id back inX-Request-Id. The SDKs send uuids.system.query_logby exactquery_id: the form gains a third field. The pages that describe the header and thequery_idform are updated: the HTTP, error, Python SDK and TypeScript SDK references, the debugging, integrations, telemetry and monitoring pages,docs/architecture/clickhouse.md,docs/architecture/system-overview.md, and the SDK docstring and README text.Compatibility, rollout and rollback
X-Request-Idechoes the caller's value only when it matches the token form above.query_idform only matters per statement.Tests
tests/unit/test_request_ids.py:query_ids with the id first andncounting.tests/unit/test_request_ch_cost.pypins the new form.tests/unit/test_sdk_hardening.pyandsdk/typescript/src/hardening.test.tscheck that each SDK sendsdbg-0001as given and replacesmy id.tests/unit/test_me_routes_measured.pysendstwo wordsto a/v1/me/*route and checks that the response header and the ledger row carry the same generated uuid.tests/unit/test_request_id_binding.pysendsx:yto an op's_begin_requestand checks that the echoed header and the query tag carry the same generated uuid.system.query_logshowed 54 statements, 54 distinctquery_ids and 6 nonces, with no exception. The spaced id came back as a uuid.mainas well and would not guard the race. The unit test pins the mechanism (disjointquery_ids per request).make format lint typecheck test-unitpass.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code. Both failures were reproduced against a server from
mainbefore the change, and the fix was checked against the same reproduction.I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited (none).frontend/changed (unchanged).docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s).🤖 Generated with Claude Code