@@ -151,6 +151,16 @@ async def new_channel(self, prefix="specific."):
151
151
await self ._subscribe_to_channel (channel )
152
152
return channel
153
153
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
+
154
164
async def receive (self , channel ):
155
165
"""
156
166
Receive the first message that arrives on the channel.
@@ -172,14 +182,7 @@ async def receive(self, channel):
172
182
# be named `delete_channel()`. If that were the case, we would do the
173
183
# following cleanup from that new `delete_channel()` method, but, since
174
184
# 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 )
183
186
raise
184
187
185
188
return self .channel_layer .deserialize (message )
0 commit comments