2
0
mirror of https://github.com/xcat2/confluent.git synced 2026-10-20 14:45:45 +00:00

Hold the async HTTP handler task until it finishes

run_handler scheduled the coroutine that serves an async HTTP request and
dropped the returned task. The event loop only keeps a weak reference, so the
task could be collected while still pending, leaving the request unanswered
and "Task was destroyed but it is pending!" in the log.

The session already outlives the request in _asyncsessions, so it holds the
task in a set and discards it from a done callback.
This commit is contained in:
Markus Hilger
2026-08-10 23:18:24 +02:00
parent 95d6c96363
commit 9e41fdc598
+6 -1
View File
@@ -66,6 +66,7 @@ class AsyncSession(object):
self.wshandler = wshandler
self.termrelations = []
self.consoles = set([])
self.handlertasks = set()
async def add(self, requestid, rsp):
await self.wshandler(messages.AsyncMessage((requestid, rsp)))
@@ -107,7 +108,11 @@ async def run_handler(hdlr, req):
raise exc.InvalidArgumentException(
'Invalid Session ID or missing request id')
cloop = asyncio.get_running_loop()
cloop.create_task(asyncsession.run_handler(hdlr, requestid))
# The loop only keeps a weak reference, so the session holds the task until
# it finishes, otherwise the request can be collected while still pending
hdlrtask = cloop.create_task(asyncsession.run_handler(hdlr, requestid))
asyncsession.handlertasks.add(hdlrtask)
hdlrtask.add_done_callback(asyncsession.handlertasks.discard)
return requestid