Skip to content

Commit ad858b2

Browse files
committed
call clean_channel in cancel
1 parent 4fa69cc commit ad858b2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

channels/consumer.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ async def __call__(self, scope, receive, send):
4747
self.channel_receive = functools.partial(
4848
self.channel_layer.receive, self.channel_name
4949
)
50+
if getattr(self.channel_layer, "clean_channel", None) and callable(self.channel_layer.clean_channel):
51+
cancel_callback = functools.partial(self.channel_layer.clean_channel, self.channel_name)
52+
else:
53+
cancel_callback = None
5054
# Store send function
5155
if self._sync:
5256
self.base_send = async_to_sync(send)
@@ -56,7 +60,7 @@ async def __call__(self, scope, receive, send):
5660
try:
5761
if self.channel_layer is not None:
5862
await await_many_dispatch(
59-
[receive, self.channel_receive], self.dispatch
63+
[receive, self.channel_receive], self.dispatch, cancel_callback=cancel_callback
6064
)
6165
else:
6266
await await_many_dispatch([receive], self.dispatch)

channels/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def name_that_thing(thing):
2929
return repr(thing)
3030

3131

32-
async def await_many_dispatch(consumer_callables, dispatch):
32+
async def await_many_dispatch(consumer_callables, dispatch, cancel_callback=None):
3333
"""
3434
Given a set of consumer callables, awaits on them all and passes results
3535
from them to the dispatch awaitable as they come in.
@@ -56,4 +56,5 @@ async def await_many_dispatch(consumer_callables, dispatch):
5656
try:
5757
await task
5858
except asyncio.CancelledError:
59-
pass
59+
if cancel_callback:
60+
await cancel_callback()

0 commit comments

Comments
 (0)