Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed CancelledError being raised from various .close() methods #1000

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1000.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix CancelledError being raised from various .close() methods
5 changes: 3 additions & 2 deletions aiokafka/consumer/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ async def close(self):

for x in self._pending_tasks:
x.cancel()
await x
with contextlib.suppress(asyncio.CancelledError):
await x

def _notify(self, future):
if future is not None and not future.done():
Expand Down Expand Up @@ -512,7 +513,7 @@ def on_done(fut, self=self):
# cancellation
if not task.done():
task.cancel()
await task
await task
self._pending_tasks.clear()
self._records.clear()

Expand Down
8 changes: 6 additions & 2 deletions aiokafka/consumer/group_coordinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import collections
import contextlib
import copy
import logging
import time
Expand Down Expand Up @@ -180,7 +181,9 @@ def _group_subscription(self):

async def close(self):
self._reset_committed_task.cancel()
await self._reset_committed_task
with contextlib.suppress(asyncio.CancelledError):
await self._reset_committed_task

self._reset_committed_task = None

def check_errors(self):
Expand Down Expand Up @@ -759,7 +762,8 @@ async def _stop_heartbeat_task(self):
if self._heartbeat_task is not None:
if not self._heartbeat_task.done():
self._heartbeat_task.cancel()
await self._heartbeat_task
with contextlib.suppress(asyncio.CancelledError):
await self._heartbeat_task
self._heartbeat_task = None

async def _heartbeat_routine(self):
Expand Down
4 changes: 3 additions & 1 deletion aiokafka/producer/sender.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import collections
import contextlib
import logging
import time

Expand Down Expand Up @@ -96,7 +97,8 @@ def sender_task(self):
async def close(self):
if self._sender_task is not None and not self._sender_task.done():
self._sender_task.cancel()
await self._sender_task
with contextlib.suppress(asyncio.CancelledError):
await self._sender_task

async def _sender_routine(self):
"""Background task, that sends pending batches to leader nodes for
Expand Down