Add support for mid-stream exception detection via X-ClickHouse-Exception-Tag header#168
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR introduces support for detecting mid-stream exceptions in ClickHouse queries when the server is configured with http_write_exception_in_output_format (ClickHouse 25.11+). Previously, exceptions occurring during result streaming would surface as generic EndOfStreamExceptions. Now, when the server includes an X-ClickHouse-Exception-Tag header, the client wraps the response stream with ExceptionTagAwareStream to buffer recent bytes and detect exception markers (format: __exception__<token>\n<message>\n<size> <token>__exception__). When an EndOfStreamException occurs, the system extracts and throws a proper ClickHouseServerException with the server's error message and code.
Changes:
- Adds
ExceptionTagAwareStreamwith a 4KB ring buffer to track recent bytes and parse exception markers - Updates
ClickHouseDataReaderto conditionally wrap HTTP streams and convert mid-stream failures toClickHouseServerException - Extends
ClickHouseServerException.FromMidStreamExceptionto handle parsed error messages - Introduces
Feature.ExceptionTagenum value for version detection (25.11+) - Comprehensive test coverage for stream parsing logic and ADO integration
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| RELEASENOTES.md | Documents the new mid-stream exception detection feature for ClickHouse 25.11+ |
| ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs | Implements ring buffer stream wrapper with marker detection and exception parsing |
| ClickHouse.Driver/ClickHouseServerException.cs | Adds factory method for creating exceptions from mid-stream error messages |
| ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs | Integrates exception-aware stream wrapping and converts EndOfStreamException to ClickHouseServerException |
| ClickHouse.Driver/ADO/Feature.cs | Adds ExceptionTag feature flag for ClickHouse 25.11+ |
| ClickHouse.Driver.Tests/Formats/ExceptionTagAwareStreamTests.cs | Unit tests for stream buffering, marker detection, and exception parsing |
| ClickHouse.Driver.Tests/ADO/MidStreamExceptionTests.cs | Integration test verifying end-to-end exception detection with ClickHouse server |
See ClickHouse/ClickHouse#88818 for the changes in core.
Note
Introduces mid-stream exception detection when the server sets
X-ClickHouse-Exception-Tag(25.11+), converting truncated reads into meaningfulClickHouseServerExceptionwith parsed error codes/messages.ExceptionTagAwareStreamthat buffers recent bytes and extracts__exception__<token>markers to buildClickHouseServerExceptionClickHouseDataReaderto read theX-ClickHouse-Exception-Tagheader, wrap the response stream, and onEndOfStreamExceptiontryTryExtractMidStreamException()ClickHouseServerExceptionwithFromMidStreamExceptionand code parsing; addsFeature.ExceptionTagMidStreamExceptionTests,ExceptionTagAwareStreamTestsRELEASENOTES.mddocumenting the new behaviorWritten by Cursor Bugbot for commit 3ba34a3. This will update automatically on new commits. Configure here.