-
Notifications
You must be signed in to change notification settings - Fork 73
Fix streaming: make mid-stream exception detection sound (no false positives, no event-loop hang) #975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
polyglotAI-bot
wants to merge
2
commits into
main
Choose a base branch
from
polyglot/fix-midstream-exception-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix streaming: make mid-stream exception detection sound (no false positives, no event-loop hang) #975
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
114 changes: 114 additions & 0 deletions
114
packages/client-node/__tests__/unit/node_exception_tag.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import type { DataFormat } from "@clickhouse/client-common"; | ||
| import { Readable } from "stream"; | ||
| import { ResultSet } from "../../src"; | ||
| import { guid } from "../../../client-common/__tests__/utils/guid"; | ||
|
|
||
| // Regression coverage for the in-band mid-stream exception detector. When an | ||
| // error occurs after a 200 response has started streaming, ClickHouse (25.11+) | ||
| // terminates the body with `<tag>\r\n__exception__\r\n`, echoing the random | ||
| // per-response token from the `x-clickhouse-exception-tag` header. The detector | ||
| // must fire ONLY on that real trailer, never on a stray `\r\n` in a successful | ||
| // body (binary Parquet, or CRLF-terminated CSV/TSV rows). | ||
| describe("[Node.js] mid-stream exception tag detection", () => { | ||
| const tag = "abcdefghijklmnop"; | ||
|
|
||
| function makeResultSet(chunks: Buffer[], format: DataFormat = "CSV") { | ||
| return ResultSet.instance({ | ||
| stream: Readable.from(chunks), | ||
| format, | ||
| query_id: guid(), | ||
| log_error: () => undefined, | ||
| response_headers: { "x-clickhouse-exception-tag": tag }, | ||
| }); | ||
| } | ||
|
|
||
| async function collectRowText( | ||
| rs: ReturnType<typeof makeResultSet>, | ||
| ): Promise<string[]> { | ||
| const rows: string[] = []; | ||
| for await (const chunk of rs.stream()) { | ||
| for (const row of chunk) { | ||
| rows.push(row.text); | ||
| } | ||
| } | ||
| return rows; | ||
| } | ||
|
|
||
| it("streams a successful CRLF-terminated CSV body to completion", async () => { | ||
| const rs = makeResultSet([Buffer.from("0\r\n1\r\n2\r\n")]); | ||
| const rows = await collectRowText(rs); | ||
| expect(rows).toHaveLength(3); | ||
| }); | ||
|
|
||
| it("streams a binary body containing \\r\\n to completion", async () => { | ||
| const parquetish = Buffer.from([ | ||
| 0x50, | ||
| 0x41, | ||
| 0x52, | ||
| 0x31, // "PAR1" | ||
| 0x0d, | ||
| 0x0a, // stray \r\n | ||
| 0x00, | ||
| 0x01, | ||
| 0x02, | ||
| 0x03, | ||
| 0x0d, | ||
| 0x0a, // stray \r\n | ||
| 0xff, | ||
| 0xfe, | ||
| 0xfd, | ||
| ]); | ||
| await expect( | ||
| collectRowText(makeResultSet([parquetish], "Parquet")), | ||
| ).resolves.toBeInstanceOf(Array); | ||
| }); | ||
|
|
||
| it("still surfaces a genuine mid-stream exception with the real message", async () => { | ||
| const errMsg = | ||
| "Code: 395. DB::Exception: Value passed to 'throwIf' function is non-zero: " + | ||
| "while executing 'FUNCTION throwIf(equals(number, 3))'. " + | ||
| "(FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 26.5.1.882)"; | ||
| const body = | ||
| "0\n1\n2\n" + | ||
| "\r\n__exception__\r\n" + | ||
| tag + | ||
| "\r\n" + | ||
| errMsg + | ||
| "\n" + | ||
| (errMsg.length + 1) + | ||
| " " + | ||
| tag + | ||
| "\r\n__exception__\r\n"; | ||
| await expect( | ||
| collectRowText(makeResultSet([Buffer.from(body, "latin1")])), | ||
| ).rejects.toThrow("Value passed to 'throwIf' function is non-zero"); | ||
| }); | ||
|
|
||
| // With output_format_*_crlf_end_of_line the row terminator is itself `\r\n`, | ||
| // so the `\r`-before-`\n` pre-filter matches at the FIRST row rather than at | ||
| // the trailer. Detection must still surface the genuine server error | ||
| // (extractErrorAtTheEndOfChunk always parses the trailer at the end of the | ||
| // chunk, independent of which newline triggered the check) — not a bogus | ||
| // row-keyed error — and must not hang. | ||
| it("surfaces the real exception message when preceding rows are CRLF-terminated", async () => { | ||
| const errMsg = | ||
| "Code: 395. DB::Exception: Value passed to 'throwIf' function is non-zero: " + | ||
| "while executing 'FUNCTION throwIf(equals(number, 3))'. " + | ||
| "(FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 26.5.1.882)"; | ||
| const body = | ||
| "0\r\n1\r\n2\r\n" + | ||
| "\r\n__exception__\r\n" + | ||
| tag + | ||
| "\r\n" + | ||
| errMsg + | ||
| "\n" + | ||
| (errMsg.length + 1) + | ||
| " " + | ||
| tag + | ||
| "\r\n__exception__\r\n"; | ||
| await expect( | ||
| collectRowText(makeResultSet([Buffer.from(body, "latin1")])), | ||
| ).rejects.toThrow("Value passed to 'throwIf' function is non-zero"); | ||
| }); | ||
| }); |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks — I checked this against the exact scenario, both on a live 26.5 server and in a unit test.
1. The surfaced error is the real server error, not a row-1 error.
extractErrorAtTheEndOfChunkalways parses the trailer at the end of the chunk, independent of which newline iteration triggers the check. So even when the\r-before-\npre-filter matches at the first CRLF row, theErrorhanded tocallbackis the genuine server exception. I added a regression test (node_exception_tag.test.ts→ "surfaces the real exception message when preceding rows are CRLF-terminated"): it streams0\r\n1\r\n2\r\nfollowed by a realthrowIftrailer and asserts the actualFUNCTION_THROW_IF_VALUE_IS_NON_ZEROmessage surfaces — which it does, with no hang.2. Dropping the rows accumulated in the terminal chunk is pre-existing behavior, unchanged by this PR. The only change this PR makes to
result_set.tsis adding the&& endsWithExceptionMarker(chunk, exceptionTag)conjunct to the existing condition; thereturn callback(err)(which returns before pushingrows) is exactly as it was before. When a mid-stream exception occurs the query has failed, so — like rows from earlier chunks that were already delivered — the client surfaces the error and discards the incomplete tail rather than handing back a partial result.Detecting at the trailer position rather than the first qualifying CRLF, and flushing partial rows, is the same larger robustness area as the deferred cross-chunk-split handling, and is intentionally out of scope here — this PR's goal is narrowly to make detection sound (kill the false-positive aborts and the event-loop hang). Happy to open a separate issue if you'd like the failed-query partial-row semantics reconsidered. Leaving this thread for your call.