@@ -165,6 +165,16 @@ async def new_channel(self, prefix="specific."):
165
165
await self ._subscribe_to_channel (channel )
166
166
return channel
167
167
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
+
168
178
async def receive (self , channel ):
169
179
"""
170
180
Receive the first message that arrives on the channel.
@@ -186,14 +196,7 @@ async def receive(self, channel):
186
196
# be named `delete_channel()`. If that were the case, we would do the
187
197
# following cleanup from that new `delete_channel()` method, but, since
188
198
# 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 )
197
200
raise
198
201
199
202
return self .channel_layer .deserialize (message )
0 commit comments