Skip to content

Commit 2f07824

Browse files
Hold references to background tasks to avoid garbage collection (Fixes #1191)
1 parent 1f488b0 commit 2f07824

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/socketio/async_server.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from . import exceptions
88
from . import packet
99

10+
# this set is used to keep references to background tasks to prevent them from
11+
# being garbage collected mid-execution. Solution taken from
12+
# https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task
13+
task_reference_holder = set()
14+
1015

1116
class AsyncServer(base_server.BaseServer):
1217
"""A Socket.IO server for asyncio.
@@ -588,8 +593,11 @@ async def _handle_event(self, eio_sid, namespace, id, data):
588593
sid, namespace)
589594
return
590595
if self.async_handlers:
591-
self.start_background_task(self._handle_event_internal, self, sid,
592-
eio_sid, data, namespace, id)
596+
task = self.start_background_task(
597+
self._handle_event_internal, self, sid, eio_sid, data,
598+
namespace, id)
599+
task_reference_holder.add(task)
600+
task.add_done_callback(task_reference_holder.discard)
593601
else:
594602
await self._handle_event_internal(self, sid, eio_sid, data,
595603
namespace, id)

0 commit comments

Comments
 (0)