@@ -151,6 +151,16 @@ async def new_channel(self, prefix="specific."):
151151 await self ._subscribe_to_channel (channel )
152152 return channel
153153
154+ async def clean_channel (self , channel ):
155+ if channel in self .channels :
156+ del self .channels [channel ]
157+ try :
158+ shard = self ._get_shard (channel )
159+ await shard .unsubscribe (channel )
160+ except BaseException :
161+ logger .exception ("Unexpected exception while cleaning-up channel:" )
162+ # We don't re-raise here because we want the CancelledError to be the one re-raised.
163+
154164 async def receive (self , channel ):
155165 """
156166 Receive the first message that arrives on the channel.
@@ -172,14 +182,7 @@ async def receive(self, channel):
172182 # be named `delete_channel()`. If that were the case, we would do the
173183 # following cleanup from that new `delete_channel()` method, but, since
174184 # that's not how Django Channels works (yet), we do the cleanup below:
175- if channel in self .channels :
176- del self .channels [channel ]
177- try :
178- shard = self ._get_shard (channel )
179- await shard .unsubscribe (channel )
180- except BaseException :
181- logger .exception ("Unexpected exception while cleaning-up channel:" )
182- # We don't re-raise here because we want the CancelledError to be the one re-raised.
185+ await self .clean_channel (channel )
183186 raise
184187
185188 return self .channel_layer .deserialize (message )
0 commit comments