[FLINK-31951][format] Reset decoder on Avro deserialization failure#28715
Open
spratt wants to merge 2 commits into
Open
[FLINK-31951][format] Reset decoder on Avro deserialization failure#28715spratt wants to merge 2 commits into
spratt wants to merge 2 commits into
Conversation
- Add resetDecoder() to AvroDeserializationSchema; reinitialises the BinaryDecoder against the existing input stream via DecoderFactory - Wrap datumReader.read() in RegistryAvroDeserializationSchema with try-catch(IOException | RuntimeException) that calls resetDecoder() before rethrowing, clearing stale bytes from the internal read-ahead buffer so the next message is not corrupted - Add RegistryAvroDeserializationSchemaDecoderResetTest verifying that a valid message decodes correctly after a prior malformed-payload failure
Collaborator
Author
|
@flinkbot run azure |
davidradl
reviewed
Jul 13, 2026
| } | ||
|
|
||
| void resetDecoder() throws IOException { | ||
| if (encoding == AvroEncoding.BINARY) { |
Contributor
There was a problem hiding this comment.
this looks like a great catch and one to get fixed. I am curious whether this scenario can be hit in the base avro format itself - if so could we fix it there?
Author
There was a problem hiding this comment.
Great question. I'll investigate and get back to you.
Author
There was a problem hiding this comment.
Well caught! My investigation showed that the scenario could be hit independently in the base class and in the registry-specific case, so I've included tests and fixes for both.
- Wrap datumReader.read() in AvroDeserializationSchema.deserialize() with try-catch(IOException | RuntimeException) that calls resetDecoder() before rethrowing; the base class has the same stale read-ahead buffer bug as RegistryAvroDeserializationSchema, which fully overrides deserialize() and is therefore unaffected by fixing only the base class - Add AvroDeserializationSchemaDecoderResetTest verifying that a valid raw Avro binary message decodes correctly after a prior malformed-payload failure
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.
What is the purpose of the change
This PR addresses FLINK-31951. We are encountering this bug in production and opened this PR after finding the two prior fix attempts closed due to inactivity: #22507 (approved, closed inactive) and #26397 (closed inactive).
This fix was developed independently. The prior PRs applied the reset in
AvroDeserializationSchema.deserialize(), butRegistryAvroDeserializationSchemaoverrides that method entirely, so the base-class fix is never reached when using the registry schema. This PR extends the fix to cover both: it adds the same try-catch toAvroDeserializationSchema.deserialize()(addressing the base-class bug) and separately toRegistryAvroDeserializationSchema.deserialize()(which cannot rely on the base-class fix since it overrides the method). Both catchRuntimeExceptionin addition toIOException, coveringAvroRuntimeException, the exception type thrown when the decoder reads malformed data. We are happy to defer if the maintainers prefer to revive one of the earlier approaches instead.AvroDeserializationSchemaand its subclassRegistryAvroDeserializationSchemaboth reuse a singleBinaryDecoderinstance across all messages on a Kafka partition. WhendatumReader.read()fails mid-message (e.g. a malformed payload, a schema mismatch, or anAvroRuntimeException), stale bytes remain in the decoder's internal read-ahead buffer. The next call todatumReader.read()then starts from those leftover bytes rather than the beginning of the next message, producing corrupt output or cascading failures.This fix wraps
datumReader.read()in a try-catch in both classes and callsresetDecoder()before re-throwing, discarding any pre-fetched bytes and leaving the decoder ready for the next message.Brief change log
AvroDeserializationSchema: add package-privateresetDecoder()method that reinitialises theBinaryDecoderagainst the existinginputStreamviaDecoderFactory.get().binaryDecoder(inputStream, decoder); no-op for JSON encoding; wrapdatumReader.read()in a try-catch onIOException | RuntimeExceptionthat callsresetDecoder()before re-throwingRegistryAvroDeserializationSchema: wrapdatumReader.read()in the same try-catch; this is necessary independently of the base-class fix because the registry class overridesdeserialize()entirelyAvroDeserializationSchemaDecoderResetTest: same pattern with raw Avro binary (no registry header), verifying the base-class fixRegistryAvroDeserializationSchemaDecoderResetTest: serialises a Confluent-framed malformed message followed by a valid one and asserts the valid message deserialises correctly after the failed decodeVerifying this change
Please make sure both new and modified tests in this PR follow the conventions for tests defined in our code quality guide.
This change added tests and can be verified as follows:
AvroDeserializationSchemaDecoderResetTestwhich feeds a raw Avro binary payload with a valididfield but an invalid string length forname(triggering anAvroRuntimeExceptionmid-decode), followed by a correctly encoded message, and asserts the second message producesid=42,name="hello". Without the base-class fix, the second decode reads the stale zero bytes left by the first failure and returnsid=0,name="".RegistryAvroDeserializationSchemaDecoderResetTestwhich does the same with Confluent Schema Registry framed messages (magic byte + schema ID prefix). Without the registry fix, the second decode similarly returns wrong values from the stale buffer, demonstrating that the registry class must be fixed independently.Does this pull request potentially affect one of the following parts:
@Public(Evolving): no (resetDecoder()is package-private; no public method signatures are changed)datumReader.read()is now wrapped in a try-catch on every deserialization call. In the happy path (no exception thrown) this has negligible overhead in modern JVMsDocumentation