Skip to content

Release fetch_context when the fetch raises - #1190

Open
kratos0718 wants to merge 3 commits into
aio-libs:masterfrom
kratos0718:fix-fetch-context-on-error
Open

kratos0718 wants to merge 3 commits into
aio-libs:masterfrom
kratos0718:fix-fetch-context-on-error

Conversation

@kratos0718

@kratos0718 kratos0718 commented Sep 27, 2026 •

Copy link
Copy Markdown

getone() and getmany() await inside SubscriptionState.fetch_context(), but the context manager decremented _fetch_count after a bare yield. If the await raises, most commonly CancelledError when the call is wrapped in asyncio.wait_for() and times out, the counter stays incremented. From then on fetcher_idle_time returns 0, so the heartbeat loop's max_poll_interval check never makes the consumer leave the group, and the rejoin path treats it as active.

I reproduced this against a local Kafka 3.8 broker. The consumer used max_poll_interval_ms=3000, made one asyncio.wait_for(consumer.getone(), timeout=1) call that timed out, then stopped polling for 8 seconds. On master it never sent LeaveGroup. With this change it leaves as expected.

The fix wraps the yield in try/finally. I added a unit test in tests/test_subscription_state.py that fails on master (idle time stays 0) and passes with the change. make lint is clean. I also added a CHANGES.rst entry.

getone() and getmany() await inside fetch_context(). If that await
raises, most commonly CancelledError from asyncio.wait_for() timing out,
the fetch counter was never decremented, so fetcher_idle_time reported 0
from then on and the max_poll_interval check never made the consumer
leave the group.
@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Fixes consumer group timeout handling on fetch errors.

No outstanding findings block merging.

Reviews (2) · Last reviewed commit: "Move changelog entry to 0.15.0"

Comment thread CHANGES.rst Resolved
@greptile-apps

This comment has been minimized.

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.

1 participant