Skip to content

Commit

Permalink
馃悰 fix: cancel underlying tasks when receiving is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
babichjacob committed Feb 16, 2023
1 parent fce1bc9 commit d8c36a3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions oneshot_channel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"""


from asyncio import FIRST_COMPLETED, Event, Future, create_task, wait
from asyncio import FIRST_COMPLETED, CancelledError, Event, Future, create_task, wait
from dataclasses import dataclass
from typing import Generic, TypeVar

Expand Down Expand Up @@ -261,9 +261,14 @@ async def _recv(self) -> Result[T, RecvError]:

closed_event = create_task(self._closed.wait())
get_task = create_task(_await_future(self._future))
done, _pending = await wait(
[get_task, closed_event], return_when=FIRST_COMPLETED
)
try:
done, _pending = await wait(
[get_task, closed_event], return_when=FIRST_COMPLETED
)
except CancelledError:
closed_event.cancel()
get_task.cancel()
raise

if closed_event in done:
get_task.cancel()
Expand Down

0 comments on commit d8c36a3

Please sign in to comment.