Skip to content

Commit

Permalink
asyncioreactor: make sure task isn't deleted midway
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fruch committed Nov 30, 2023
1 parent 1cc6ccc commit 679ad24
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cassandra/io/asyncioreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 679ad24

Please sign in to comment.