From 5f07cb3d06352d1a13e21c9bcbff984960d8c4a9 Mon Sep 17 00:00:00 2001 From: darksidecat <58224121+darksidecat@users.noreply.github.com> Date: Sat, 23 Oct 2021 01:04:54 +0300 Subject: [PATCH] Close bots sessions after emit_shutdown (#734) * Close bots sessions after emit_shutdown * avoid unclosed connections if get exception in emit_shutdown * add patchnote --- CHANGES/734.bugfix | 1 + aiogram/dispatcher/dispatcher.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 CHANGES/734.bugfix diff --git a/CHANGES/734.bugfix b/CHANGES/734.bugfix new file mode 100644 index 00000000..94a5951f --- /dev/null +++ b/CHANGES/734.bugfix @@ -0,0 +1 @@ +Fixed bot session closing before emit shutdown diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index fa848547..2c78bace 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -393,10 +393,12 @@ class Dispatcher(Router): ) await asyncio.gather(*coro_list) finally: - for bot in bots: # Close sessions - await bot.session.close() loggers.dispatcher.info("Polling stopped") - await self.emit_shutdown(**workflow_data) + try: + await self.emit_shutdown(**workflow_data) + finally: + for bot in bots: # Close sessions + await bot.session.close() def run_polling( self,