Skip to content

Reset client_exception when --ignore-error skips a parse failure - #106523

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-ast-fuzzer-ignore-error-stale-client-exception
Jun 6, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-ast-fuzzer-ignore-error-stale-client-exception

Conversation

@groeneai

@groeneai groeneai commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

When a query in a multi-query batch fails to parse, the local
ClientBase::parseQuery on the is_interactive || ignore_error branch
catches the parser exception, sets client_exception, and returns nullptr.

Without ignore_error, analyzeMultiQueryText's outer try/catch takes
over and the test-hint logic in the PARSING_EXCEPTION stage clears the
exception when the failure was expected.

With ignore_error = true, the !parsed_query branch instead skipped
past the offending query and returned CONTINUE_PARSING, but never
cleared client_exception or have_error. The pinned exception then
surfaced as the process exit code in Client::main, even though the run
had explicitly elected to ignore parse errors.

This affects --query-fuzzer-runs, which auto-enables ignore_error in
Client::processOptions: a targeted-fuzzer run over any corpus that
contains a parse-time clientError hint (the test hints BAD_ARGUMENTS
or SYNTAX_ERROR thrown inside parser parseImpl, e.g.
Multiple primary keys are not allowed) exits with that error's code
(36 for BAD_ARGUMENTS). The praktika ast_fuzzer_job.py runner
treats any fuzzer_exit_code outside (0, 137, 143, 227) as ERROR,
so the job goes red even though the fuzzer behaved correctly.

This was discovered while triaging the two failing CI checks on
#105579 (AST fuzzer (amd_debug, targeted) and the old_compatibility variant). The PR
adds a new test that contains a -- { clientError BAD_ARGUMENTS }
hint on a parser-time throw; the targeted fuzzer fed exactly that
file to clickhouse-client --query-fuzzer-runs=... and exited with
Code 36.

The fix clears have_error and client_exception at the
ignore_error skip in analyzeMultiQueryText, so the skip is honest
and the process exit code reflects the rest of the run.
PARSING_EXCEPTION already clears current_exception through the
rethrow / TestHint path, so this only affects the ignore_error
skip — no other behavior changes.

Adds a stateless regression test
04318_client_query_fuzzer_runs_ignore_parse_error.sh that runs
clickhouse-client --query-fuzzer-runs=2 --create-query-fuzzer-runs=2 --queries-file <file>
where <file> ends with a query that throws BAD_ARGUMENTS at parse
time. Pre-fix the script exits 36; post-fix it exits 0. Verified
locally by reverting the fix, rebuilding, and observing the test fail
(exit: 36 vs reference exit: 0), then rebuilding with the fix and
seeing it pass 5/5.

Related: #105579
CI report (failing AST fuzzer checks): https://s3.amazonaws.com/clickhouse-test-reports/PRs/105579/0b07d9f2e61d8742d1889c96d8530b37186594cd/ast_fuzzer_amd_debug_targeted/job.log

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Documentation entry for user-facing changes

  • Documentation is unchanged: client-internal behavior fix in batch / fuzzer mode; no user-visible API or syntax changes.

Version info

  • Merged into: 26.6.1.441

When a query in a multi-query batch fails to parse, the local
ClientBase::parseQuery on the is_interactive || ignore_error branch
catches the parser exception, sets client_exception, and returns
nullptr. Without ignore_error, analyzeMultiQueryText's outer try/catch
takes over and the test-hint logic in the PARSING_EXCEPTION stage
clears the exception when the failure was expected.

With ignore_error true, the !parsed_query branch instead skipped past
the offending query and returned CONTINUE_PARSING, but never reset
client_exception or have_error. The pinned exception then surfaced as
the process exit code in Client::main, even though the run had
explicitly elected to ignore parse errors.

This affects --query-fuzzer-runs, which auto-enables ignore_error in
Client::processOptions: targeted-fuzzer runs over a corpus that
includes any test with a parse-time clientError hint exit Code 36
BAD_ARGUMENTS for example, which reads as a CI failure even though the
fuzzer behaved correctly.

Reset have_error and client_exception when the loop elects to skip the
failing query, so the skip is honest and the exit code reflects the
rest of the run. PARSING_EXCEPTION already clears current_exception
through the rethrow / TestHint path, so this only affects the
ignore_error skip.

Adds a stateless regression test exercising the
clickhouse-client --query-fuzzer-runs <file with parse-time error>
shape that the targeted AST fuzzer in CI uses.

CI report (PR-only checks failing):
https://s3.amazonaws.com/clickhouse-test-reports/PRs/105579/0b07d9f2e61d8742d1889c96d8530b37186594cd/ast_fuzzer_amd_debug_targeted/job.log
Related: ClickHouse#105579
@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. clickhouse-client --query-fuzzer-runs=2 --queries-file <(printf 'SELECT 1;\nCREATE TABLE t (a Int32 PRIMARY KEY, b Int32) PRIMARY KEY a ORDER BY a;\n') exits with code 36 every time on the master binary. The repro is encoded in the new 04318_client_query_fuzzer_runs_ignore_parse_error.sh.
b Root cause explained? Yes. --query-fuzzer-runs > 0 calls config().setBool("ignore-error", true) in Client::processOptions. With ignore_error=true, the local ClientBase::parseQuery catches parser exceptions, sets client_exception, returns nullptr. analyzeMultiQueryText then takes the !parsed_query + ignore_error branch which skips past the failing query and returns CONTINUE_PARSING — but never clears have_error / client_exception. The pinned exception survives through runNonInteractive and Client::main returns client_exception->code() = 36 (BAD_ARGUMENTS). The symmetric PARSING_EXCEPTION branch (used when ignore_error=false) does honor TestHint and clears current_exception; only the ignore_error skip branch was missing the reset.
c Fix matches root cause? Yes. The fix clears have_error and client_exception exactly at the point where the loop elected to skip the failing query. No symptom-masking; the skip itself is the documented behavior of ignore_error=true, and the fix only makes the exit code reflect that decision.
d Test intent preserved / new tests added? New stateless regression test 04318_client_query_fuzzer_runs_ignore_parse_error.sh that exercises exactly the clickhouse-client --query-fuzzer-runs=N --queries-file <FILE-WITH-PARSE-TIME-THROW> shape that the targeted AST fuzzer in CI uses. The test exits 36 pre-fix, 0 post-fix. No existing tests removed or weakened.
e Both directions demonstrated? Yes. Verified locally: (1) reverted the fix (kept 04318 test), rebuilt build-asan, ran bash tests/queries/0_stateless/04318_*.sh — output exit: 36, diff exit 1. (2) restored the fix, rebuilt, ran 5x — output exit: 0 every time, diff exit 0. Also manually re-verified the simulated PR #105579 corpus (SELECT; CREATE TABLE...PRIMARY KEY a; -- { clientError BAD_ARGUMENTS }) under --query-fuzzer-runs=1000 --create-query-fuzzer-runs=50 exits 0 post-fix.
f Fix is general, not a narrow patch? Yes. Investigated symmetric paths: the PARSING_EXCEPTION branch already handles its current_exception correctly (rethrow on mismatch / TestHint skip path resets state); the EXECUTE_QUERY have_error path resets via prepareAndExecuteQuery calling client_exception.reset() for each new query (line 2305) and via the error_matches_hint block (line 2916). The only branch where client_exception is set but never cleared was the ignore_error parse-skip branch. The fix is the minimal change at the exact site that introduces the asymmetry; no defensive guard masking an upstream bug. The bug only manifests when (i) ignore_error=true, (ii) a query throws at parse time (not at server execution), and (iii) the throwing query is the last in the batch (otherwise a later successful query's prepareAndExecuteQuery resets client_exception).

Session id: cron:clickhouse-ci-task-worker:20260605-041200

@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @azat - could you review this? Small ClientBase fix that clears client_exception on the ignore_error parse-skip branch, so clickhouse-client --query-fuzzer-runs=N --queries-file <FILE-WITH-PARSE-TIME-CLIENT-ERROR> exits 0 instead of pinning Code 36. New stateless regression test verifies both directions.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jun 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d40ac24]

Summary: ✅


AI Review

Summary

This PR resets the client error state when analyzeMultiQueryText skips an unparsable statement under ignore_error, including stale client_exception, server_exception, and error_code state, and adds a stateless regression covering both the AST fuzzer path and a preceding server-error path. I verified the current code against the prior review thread; the stale server_exception finding is addressed, and I did not find remaining blockers or majors.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 5, 2026
Comment thread src/Client/ClientBase.cpp Resolved
The previous commit cleared have_error and client_exception when the
ignore_error branch in `analyzeMultiQueryText` skipped past a parse
failure. That covered the targeted-fuzzer red-check scenario, where the
parse failure is the only error, but it left a gap reported in inline
review on PR ClickHouse#106523:

If a previous statement under `--ignore-error` produced a server error,
`server_exception` and `error_code` were set and have not been reset.
The loop continues; the next statement fails to parse and reaches the
ignore_error skip; the skip clears `client_exception` and `have_error`,
but `server_exception` and `error_code` remain pinned.
`Client::main` prefers `server_exception` over `client_exception` for
the exit code, so the process exits with the stale server error code
even though `--ignore-error` had elected to continue past both failures.
The `assert(have_error == (client_exception || server_exception))`
debug-build invariant in `Client::processError` is also briefly false
until the next executed query.

Mirror the full per-query reset at the top of `processParsedSingleQuery`
(have_error/error_code/client_exception/server_exception) so the skip
leaves the same state a successful query would.

Extend the regression test
`04318_client_query_fuzzer_runs_ignore_parse_error` with the bot's
two-statement scenario: a server-error statement followed by a
parse-error statement under `--ignore-error`. Pre-fix: exit 60
(UNKNOWN_TABLE leaks). Post-fix: exit 0.

Bot review:
ClickHouse#106523 (comment)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (commit d40ac24, gap-closing iteration)

# Question Answer
a Deterministic repro? Yes. clickhouse-client --multiquery --ignore-error --query "SELECT * FROM nonexistent_table_for_04318; CREATE TABLE t (a Int32 PRIMARY KEY, b Int32) PRIMARY KEY a ORDER BY a;". On the unfixed binary this exits 60 every time; on the fixed binary it exits 0 every time. The original Scenario A (parse-failure under --query-fuzzer-runs) is also still deterministic.
b Root cause explained? analyzeMultiQueryText enters the !parsed_query && ignore_error skip branch when the next statement fails to parse. The previous fix cleared client_exception and have_error there, but did not touch server_exception or error_code. The earlier statement (executed under --ignore-error) had set both. Because no executed query intervenes between the two failures, neither field is reset elsewhere. Client::main line 443-447 picks server_exception over client_exception for the exit code, so the stale server error from statement 1 is what the process returns. The `assert(have_error == (client_exception
c Fix matches root cause? Yes. The skip now mirrors the per-query reset at the top of processParsedSingleQuery (have_error = false; error_code = 0; client_exception.reset(); server_exception.reset();). This is the same set of fields the successful path would clear before the next query, so a "pretend this never happened" skip is now honest in both directions.
d Test intent preserved / new tests added? Regression test 04318_client_query_fuzzer_runs_ignore_parse_error.sh extended with Scenario B: server-error + parse-error under --ignore-error. Reference asserts exit: 0 for both scenarios. Pre-fix scenario B asserts exit: 60 (verified). Scenario A from the prior commit is unchanged and still locks in the original parse-skip behavior.
e Both directions demonstrated? Yes, end-to-end with rebuild between iterations: pre-fix binary exits 60 on Scenario B and the test FAILS (+exit: 60 vs -exit: 0); post-fix binary exits 0 on both scenarios and the test PASSES 5/5 with --test-runs 5.
f Fix is general, not a narrow patch? Yes. The fix is at the source — the parse-skip branch — not at any of the symptom sites (Client::main, Client::processError, the per-iteration loop). I scanned the rest of analyzeMultiQueryText for sibling skip branches that mutate have_error/*_exception: none. The other resets (processParsedSingleQuery start at line 2300-2306, the matched-test-hint reset at 2916-2920, the per-query reset at 2300-2306) all set the same four fields together. After this fix the parse-skip is now consistent with that pattern. No similar bug-class found in sibling code paths.

Bot review:
#106523 (comment)

Session id: cron:clickhouse-ci-task-worker:20260605-141700

@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.50% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 8/8 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

CI ledger — covered SHA: d40ac24

CI fully finished and clean (Finish Workflow + Mergeable Check completed at 18:32:22Z, >20 min buffer; mergeStateStatus=CLEAN).

Check Disposition
All 146 reportable checks pass
20 checks skipping (unrelated workflows)
0 failures —

No PR-caused failures. CI fully green. Ball with reviewer (@azat).

Session: cron:our-pr-ci-monitor:20260605-190000

@alexey-milovidov alexey-milovidov self-assigned this Jun 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 6, 2026
Merged via the queue into ClickHouse:master with commit 482aefd Jun 6, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants