@@ -165,6 +165,16 @@ async def new_channel(self, prefix="specific."):
165165 await self ._subscribe_to_channel (channel )
166166 return channel
167167
168+ async def clean_channel (self , channel ):
169+ if channel in self .channels :
170+ del self .channels [channel ]
171+ try :
172+ shard = self ._get_shard (channel )
173+ await shard .unsubscribe (channel )
174+ except BaseException :
175+ logger .exception ("Unexpected exception while cleaning-up channel:" )
176+ # We don't re-raise here because we want the CancelledError to be the one re-raised.
177+
168178 async def receive (self , channel ):
169179 """
170180 Receive the first message that arrives on the channel.
@@ -186,14 +196,7 @@ async def receive(self, channel):
186196 # be named `delete_channel()`. If that were the case, we would do the
187197 # following cleanup from that new `delete_channel()` method, but, since
188198 # that's not how Django Channels works (yet), we do the cleanup below:
189- if channel in self .channels :
190- del self .channels [channel ]
191- try :
192- shard = self ._get_shard (channel )
193- await shard .unsubscribe (channel )
194- except BaseException :
195- logger .exception ("Unexpected exception while cleaning-up channel:" )
196- # We don't re-raise here because we want the CancelledError to be the one re-raised.
199+ await self .clean_channel (channel )
197200 raise
198201
199202 return self .channel_layer .deserialize (message )
0 commit comments