Skip to content

Commit 679ad24

Browse files
committed
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
1 parent 1cc6ccc commit 679ad24

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

cassandra/io/asyncioreactor.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def __init__(self, *args, **kwargs):
106106
)
107107
self._send_options_message()
108108

109+
self._background_tasks = set()
110+
109111
@classmethod
110112
def initialize_reactor(cls):
111113
with cls._lock:
@@ -176,7 +178,10 @@ def push(self, data):
176178
)
177179
else:
178180
# avoid races/hangs by just scheduling this, not using threadsafe
179-
self._loop.create_task(self._push_msg(chunks))
181+
task = self._loop.create_task(self._push_msg(chunks))
182+
183+
self._background_tasks.add(task)
184+
task.add_done_callback(self._background_tasks.discard)
180185

181186
async def _push_msg(self, chunks):
182187
# This lock ensures all chunks of a message are sequential in the Queue

0 commit comments

Comments
 (0)