Skip to content

Commit 77a301f

Browse files
Ensure PubSub layer closes connection pool before loop shutdown.
Co-authored-by: Carlton Gibson <[email protected]>
1 parent a993f3f commit 77a301f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

channels_redis/pubsub.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ async def flush(self):
302302
pass
303303
self._receive_task = None
304304
if self._redis is not None:
305-
await self._redis.close()
305+
# The pool was created just for this client, so make sure it is closed,
306+
# otherwise it will schedule the connection to be closed inside the
307+
# __del__ method, which doesn't have a loop running anymore.
308+
await self._redis.close(close_connection_pool=True)
306309
self._redis = None
307310
self._pubsub = None
308311
self._subscribed_to = set()

tests/test_pubsub.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ async def other_channel_layer():
3232
await channel_layer.flush()
3333

3434

35+
def test_layer_close():
36+
"""
37+
If the channel layer does not close properly there will be a "Task was destroyed but it is pending!" warning at
38+
process exit.
39+
"""
40+
41+
async def do_something_with_layer():
42+
channel_layer = RedisPubSubChannelLayer(hosts=TEST_HOSTS)
43+
await channel_layer.send(
44+
"TestChannel", {"type": "test.message", "text": "Ahoy-hoy!"}
45+
)
46+
47+
async_to_sync(do_something_with_layer)()
48+
49+
3550
@pytest.mark.asyncio
3651
async def test_send_receive(channel_layer):
3752
"""

0 commit comments

Comments
 (0)