Skip to content

Commit b4c6228

Browse files
adamclaude
andcommitted
Fix unclosed aiohttp connector warning on shutdown
Store the shutdown task reference and await it in the finally block, ensuring bot.close() fully completes before the event loop exits. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3c37ab0 commit b4c6228

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

bot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,14 @@ async def webhook_handler(request: web.Request) -> web.Response:
290290
return web.Response(text="OK")
291291

292292

293+
_shutdown_task: asyncio.Task[None] | None = None
294+
295+
293296
def _schedule_shutdown() -> None:
294297
"""Schedule a clean shutdown via bot.close(), which unblocks bot.start()."""
295-
asyncio.get_running_loop().create_task(bot.close())
298+
global _shutdown_task
299+
if _shutdown_task is None:
300+
_shutdown_task = asyncio.get_running_loop().create_task(bot.close())
296301

297302

298303
async def shutdown_handler(request: web.Request) -> web.Response:
@@ -348,6 +353,10 @@ async def main() -> None:
348353
try:
349354
await bot.start(config.DISCORD_TOKEN)
350355
finally:
356+
if _shutdown_task is not None:
357+
await _shutdown_task
358+
if not bot.is_closed():
359+
await bot.close()
351360
await runner.cleanup()
352361

353362

0 commit comments

Comments
 (0)