1/6 Don't crash on non-UTF-8 ApiError bodies#2134
Merged
Merged
Conversation
The ApiError handler in execute_single() decodes `e.body`, `e.error`, and `e.info` as UTF-8 to build a human-readable error message. When the body is binary (e.g., binary protobuf returned by ES OTLP endpoints on 4xx/5xx), the strict decode raises UnicodeDecodeError, which crashes the worker mid-task. Switch the six decode() calls to use errors="replace" so undecodable bytes become U+FFFD instead of aborting the worker. No semantic change for valid UTF-8 (the common case). This is a latent bug independent of OTLP — any operation that surfaces a binary error body would have hit it.
2 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens execute_single in the driver so Elasticsearch ApiError handling does not crash when error bodies contain non-UTF-8 bytes.
Changes:
- Updates ApiError body/error/info decoding to use UTF-8 replacement for undecodable bytes.
- Adds an inline comment explaining the binary response-body motivation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if isinstance(e.body, bytes): | ||
| # could be an empty body | ||
| if error_body := e.body.decode("utf-8"): | ||
| if error_body := e.body.decode("utf-8", errors="replace"): |
This was referenced May 29, 2026
pquentin
approved these changes
Jun 1, 2026
pquentin
left a comment
Member
There was a problem hiding this comment.
It's wild that OTLP ingest does this. A bug IMHO. Still approving to unblock you.
gareth-ellis
commented
Jun 1, 2026
| body = io.BytesIO(b"\xff") | ||
| str_literal = str(body) | ||
| error_meta = elastic_transport.ApiResponseMeta( | ||
| status=499, |
Member
Author
There was a problem hiding this comment.
@copilot why 499 and not 400 like the test is called? A server is unlikely to return a 499, since that is typically used as a client timeout
Contributor
There was a problem hiding this comment.
Updated in 835a0e1: changed that test fixture and expectation to use HTTP 400 so it matches the test intent.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When an Elasticsearch ApiError's body is non-UTF-8 (e.g. binary protobuf returned by OTLP endpoints under coordinator-bytes backpressure),
e.body.decode("utf-8")inexecute_singleraisesUnicodeDecodeErrorand crashes the worker.Replaces six
.decode("utf-8")call sites in the driver's ApiError handler with.decode("utf-8", errors="replace")so undecodable bytes downgrade to U+FFFD instead of crashing. No semantic change for valid UTF-8 (the common case).Part 1 of 6 in the OTLP ingest series, but a standalone hardening fix that's useful on its own. Has no dependencies on the rest of the series.
Series
Test plan
🤖 Generated with Claude Code