From 679ad2490b7bfb440cbda122712380acff925dde Mon Sep 17 00:00:00 2001 From: Israel Fruchter Date: Mon, 27 Nov 2023 15:47:38 +0200 Subject: [PATCH] asyncioreactor: make sure task isn't deleted midway in push function, self._loop.create_task is called and it's return value is ignored. While the tests may pass now, this code is not correct and this example is called out in docs as a source of bugs, as python docs suggests. Ref: https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task --- cassandra/io/asyncioreactor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cassandra/io/asyncioreactor.py b/cassandra/io/asyncioreactor.py index fc02392511..4876b5be1e 100644 --- a/cassandra/io/asyncioreactor.py +++ b/cassandra/io/asyncioreactor.py @@ -106,6 +106,8 @@ def __init__(self, *args, **kwargs): ) self._send_options_message() + self._background_tasks = set() + @classmethod def initialize_reactor(cls): with cls._lock: @@ -176,7 +178,10 @@ def push(self, data): ) else: # avoid races/hangs by just scheduling this, not using threadsafe - self._loop.create_task(self._push_msg(chunks)) + task = self._loop.create_task(self._push_msg(chunks)) + + self._background_tasks.add(task) + task.add_done_callback(self._background_tasks.discard) async def _push_msg(self, chunks): # This lock ensures all chunks of a message are sequential in the Queue