Skip to content

Two requests with one X-Request-Id no longer fail with a 500 - #205

Merged
zlareb1 merged 15 commits into
mainfrom
fix/request-id-query-ids
Nov 3, 2026
Merged

zlareb1 merged 15 commits into
mainfrom
fix/request-id-query-ids

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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. An X-Request-Id that is not 1 to 64 letters, digits, ., _ or - is replaced by a generated id, which the response echoes.

Why

The caller's X-Request-Id was used as given in two places:

  • the second word of the query tag (log_comment = "<op> <request_id>");
  • the first field of every statement's query_id (<request_id>:<n>).

That breaks in two ways:

  • One id, two requests at once. Both requests send query_id = <id>:1, <id>:2 and so on. ClickHouse refuses a query_id that is already running (code 216, QUERY_WITH_SAME_ID_IS_ALREADY_RUNNING), and the request answers 500.
    • Reproduced against a server from main: 6 concurrent memory.search calls with X-Request-Id: probe-dup-7 gave 4 answers of 500 and 2 of 200, with 18 statements failing with code 216 in system.query_log.
    • Both SDKs send one X-Request-Id across their own retries, so a retry of a slow call that is still running on the server can hit this.
  • An id with a space. X-Request-Id: probe space gave query_id = probe:N, the first word only, while log_comment and the ledger carried probe space. The join between them broke, and the id collided with any other request whose id is probe.

The fix has two parts:

  • The nonce. Every request's statements carry 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's query_id (a carried insert prefixes it with loom-carry/<process>/), and log_comment still carries it.
  • The format check. A caller id outside [A-Za-z0-9._-]{1,64} is replaced by a uuid4, which the response's X-Request-Id returns. That covers the space and the 120-character tag cap, which used to truncate long ids.

Scope and risk

  • Touches: server/transport/_common.py (request_id_from), the two places server/transport/http.py read the header (ops and /v1/me/*), server/core/request_work.py and server/core/ch_request_cost.py.
  • Unchanged: MCP calls already get a generated id, and worker passes keep the driver's own query ids.
  • SDKs: both SDKs apply the same check to a caller-supplied request_id / requestId and send a generated id in its place. The id the SDK logs and puts on its errors is then the id the server records.
  • Risk: a client that sends an id with other characters (a space, :, /, non-ASCII) or longer than 64 characters now gets a different id back in X-Request-Id. The SDKs send uuids.
  • For anyone reading system.query_log by exact query_id: the form gains a third field. The pages that describe the header and the query_id form 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

  • Wire: no op or field changes. X-Request-Id echoes the caller's value only when it matches the token form above.
  • Operator steps: none. N-1 and N can serve side by side: the query_id form only matters per statement.
  • Rollback: revert. Nothing is stored.

Tests

  • tests/unit/test_request_ids.py:
    • which caller ids are kept and which are replaced;
    • two requests with one id send disjoint query_ids with the id first and n counting.
  • tests/unit/test_request_ch_cost.py pins the new form.
  • tests/unit/test_sdk_hardening.py and sdk/typescript/src/hardening.test.ts check that each SDK sends dbg-0001 as given and replaces my id.
  • tests/unit/test_me_routes_measured.py sends two words to 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.py sends x:y to an op's _begin_request and checks that the echoed header and the query tag carry the same generated uuid.
  • Against a local server from this branch, the same 6 concurrent calls with one id all answered 200. system.query_log showed 54 statements, 54 distinct query_ids and 6 nonces, with no exception. The spaced id came back as a uuid.
  • No integration test: a test namespace's statements finish too fast to overlap, so a concurrent-call test passed against main as well and would not guard the race. The unit test pins the mechanism (disjoint query_ids per request).
  • make format lint typecheck test-unit pass.

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 main before 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 typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand (none change).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited (none).
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version (none).
  • The frontend builds and its tests pass, if frontend/ changed (unchanged).
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s).

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the fix/request-id-query-ids branch 5 times, most recently from 8de42ef to def3ebb Compare November 3, 2026 13:18
zlareb1 and others added 15 commits November 3, 2026 13:40
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]>
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]>
…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]>
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
zlareb1 force-pushed the fix/request-id-query-ids branch from 2499b50 to 04391e0 Compare November 3, 2026 13:41
@zlareb1
zlareb1 merged commit b12a664 into main Nov 3, 2026
21 of 22 checks passed
@zlareb1
zlareb1 deleted the fix/request-id-query-ids branch November 3, 2026 13:43
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant