Skip to content

Consumer: keep raising record errors instead of spinning in poll() - #3196

Open
waw666waw666 wants to merge 1 commit into
dpkp:masterfrom
waw666waw666:fix-fetch-exception-spin
Open

waw666waw666 wants to merge 1 commit into
dpkp:masterfrom
waw666waw666:fix-fetch-exception-spin

Conversation

@waw666waw666

Copy link
Copy Markdown

If a value_deserializer/key_deserializer raises (or a per-record CRC check fails) in the middle of a fetched batch, the consumer gets stuck on the next poll():

consumer = KafkaConsumer(..., value_deserializer=json.loads)
# partition holds  {"a":1}  {"a":2}  not-json  {"a":4}
consumer.poll()   # -> {"a":1}, {"a":2}
consumer.poll()   # -> raises JSONDecodeError
consumer.poll()   # never returns, 100% CPU, timeout_ms ignored

PartitionRecords.take() raises the stored error once, but the record generator has already terminated with that exception. The partition records are still truthy and still in line at the current position, so fetched_records() keeps calling _append(), which takes 0 records each time, and while records_remaining > 0 never ends.

The usual way out is to seek past the bad record, but that didn't work either when the record sits in the middle of a batch. The broker returns the whole batch again, and the records below the fetch offset were only skipped (dropwhile) after they had been CRC-checked and deserialized, so the same record failed again.

This change:

  • keeps the error on PartitionRecords and raises it on every take() until the records are drained (for example after seek()), which matches the Java client;
  • skips records below the fetch offset inside _unpack_records, before validation and deserialization.

The behaviour has been there since the inline-exception handling was added in 2.2.0.

Tests: test__unpack_records_corrupted now checks that the error is raised again; new tests cover the repeated deserializer error at the PartitionRecords and fetched_records() level (the latter hangs without the fix, so it has a 10s timeout) and seeking past a bad record in the middle of a batch. All four fail on master and pass with the change. Full pytest test: 2762 passed, 68 skipped; pylint --errors-only kafka test clean.

When a deserializer (or a per-record CRC check) failed in the middle of a
fetched batch, the error was raised once and the record generator was left
exhausted but still in line. The next poll() then looped forever in
fetched_records(), taking no records and never returning, regardless of
timeout_ms.

Keep raising the same error until the partition records are drained, and
skip records below the fetch offset before validating or deserializing
them, so seeking past a bad record in the middle of a batch lets the
consumer continue.
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.

1 participant