Skip to content

Fix committing past an in-flight message at the committed offset - #807

Open
chowpawan wants to merge 1 commit into
faust-streaming:masterfrom
chowpawan:fix-gap-offset-commit-606
Open

chowpawan wants to merge 1 commit into
faust-streaming:masterfrom
chowpawan:fix-gap-offset-commit-606

Conversation

@chowpawan

@chowpawan chowpawan commented Dec 2, 2026 •

Copy link
Copy Markdown

Description

Fixes #606.

With concurrency > 1, Faust can commit an offset past a message that is still being processed. If the worker then dies, that message is lost.

Root cause

Kafka's committed offset is the next offset to process, but the head-gap check in Consumer._new_offset treated it as the last processed offset:

if min(acked) - committed_offset > 1:
    return None

Using the numbers from the issue: the committed offset was 2585, message 2585 was still processing, and 2586–2588 were acked. 2586 - 2585 = 1 passed the check, so 2589 was committed and 2585 was lost after the restart.

Changes

  • _new_offset: don't commit while the first acked offset is above the committed offset. It also uses is not None, so a committed offset of 0 is checked too.
  • _add_gap: gaps are now trimmed at committed, not committed + 1. The committed offset itself can be missing, for example a transaction marker.
  • Read offset after a seek (perform_seek, seek, aiokafka _seek_wait): store position - 1, matching the "last offset read" meaning the drain loop expects. Otherwise a missing message right at the committed position is never recorded as a gap.

The last two changes are needed together with the first. The old off-by-one was hiding them, and without them the stricter check would stop commits on topics with a gap at the committed offset (e.g. transactional input topics).

Tests

  • New regression tests cover the issue's scenario, a committed offset of 0, a missing message at the committed offset, and _add_gap including the committed offset. They fail on master and pass with this change.
  • Updated the existing tests that asserted the old behavior: seek/read-offset values, and one _new_offset case that treated the committed offset as already processed.
  • pytest tests/unit: 2037 passed, 12 skipped.

Known limitation

When a partition has no committed offset (a new consumer group), there's no starting point to check against, so behavior there is unchanged.

Broker test

Reproduced #606 on Faust 0.15.5 and verified the fix on a real broker; see results.

…st-streaming#606)

Kafka's committed offset is the next offset to process, but the head-gap
check in Consumer._new_offset treated it as the last processed offset.
With concurrency > 1, if the message at the committed offset was still
being processed while later ones were acked, the consumer committed past
it and the message was lost on crash.

- _new_offset: don't commit while the first acked offset is above the
  committed offset (and handle a committed offset of 0).
- _add_gap: clip gaps at the committed offset, not committed + 1, since
  the committed offset itself may be missing (e.g. a transaction marker).
- Store position - 1 in _read_offset after a seek so gap detection also
  sees a missing message right at the committed position.

The last two are needed so the stricter check does not stall commits on
topics with gaps at the committed offset.
@chowpawan

Copy link
Copy Markdown
Author

Tested against a real broker (Kafka 3.8, single node): concurrency=2, message 1 sleeps 100 s while 2–8 finish, then SIGKILL and restart.

Faust 0.15.5 This PR
Committed offset while message 1 (offset 1) is in flight 9 1
After restart message 1 lost message 1 redelivered

Messages 2–8 finished in both runs, with no errors in the logs.

@chowpawan

Copy link
Copy Markdown
Author

I didn't see #707 before opening this. Different approach: #707 caps commits at the smallest unacked offset, which also covers the no-committed-offset case. This PR fixes the head-gap check directly and also corrects gap trimming at the committed offset and the read offset after seeks; without those, a missing message at the committed position can stall commits. The broker results above apply to either fix. Happy to fold the gap/seek changes and the broker test into #707, or rebase this on top of it, whichever is easier to review.

This branch has not been deployed

No deployments
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.

Wrong offset commit in case of gaps

1 participant