Skip to content

Add support for mid-stream exception detection via X-ClickHouse-Exception-Tag header#168

Merged
alex-clickhouse merged 3 commits intomainfrom
new-exception-handling
Jan 15, 2026
Merged

Add support for mid-stream exception detection via X-ClickHouse-Exception-Tag header#168
alex-clickhouse merged 3 commits intomainfrom
new-exception-handling

Conversation

@alex-clickhouse
Copy link
Copy Markdown
Collaborator

@alex-clickhouse alex-clickhouse commented Jan 14, 2026

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 meaningful ClickHouseServerException with parsed error codes/messages.

  • Adds ExceptionTagAwareStream that buffers recent bytes and extracts __exception__<token> markers to build ClickHouseServerException
  • Updates ClickHouseDataReader to read the X-ClickHouse-Exception-Tag header, wrap the response stream, and on EndOfStreamException try TryExtractMidStreamException()
  • Extends ClickHouseServerException with FromMidStreamException and code parsing; adds Feature.ExceptionTag
  • Adds unit/integration tests for detection behavior and parsing: MidStreamExceptionTests, ExceptionTagAwareStreamTests
  • Updates RELEASENOTES.md documenting the new behavior

Written by Cursor Bugbot for commit 3ba34a3. This will update automatically on new commits. Configure here.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 14, 2026

Codecov Report

❌ Patch coverage is 91.45299% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ickHouse.Driver/Formats/ExceptionTagAwareStream.cs 89.13% 8 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ExceptionTagAwareStream with a 4KB ring buffer to track recent bytes and parse exception markers
  • Updates ClickHouseDataReader to conditionally wrap HTTP streams and convert mid-stream failures to ClickHouseServerException
  • Extends ClickHouseServerException.FromMidStreamException to handle parsed error messages
  • Introduces Feature.ExceptionTag enum 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

Comment thread ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs
Comment thread ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs
Comment thread ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs
Comment thread ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs
Comment thread ClickHouse.Driver/Formats/ExceptionTagAwareStream.cs Outdated
Comment thread ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs
Comment thread ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs
Comment thread ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs
Comment thread ClickHouse.Driver.Tests/ADO/MidStreamExceptionTests.cs
Comment thread ClickHouse.Driver.Tests/Formats/ExceptionTagAwareStreamTests.cs Outdated
@alex-clickhouse alex-clickhouse merged commit 23a4966 into main Jan 15, 2026
18 checks passed
@alex-clickhouse alex-clickhouse deleted the new-exception-handling branch January 15, 2026 13:54
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.

2 participants