Skip to content

Commit

Permalink
PoC for handling systemexit beter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Mar 12, 2024
1 parent c0716dd commit 0c27c50
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions telegram/ext/_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,22 @@ async def stop(self) -> None:
_LOGGER.info("Application is stopping. This might take a moment.")

# Stop listening for new updates and handle all pending ones
await self.update_queue.put(_STOP_SIGNAL)
_LOGGER.debug("Waiting for update_queue to join")
await self.update_queue.join()
if self.__update_fetcher_task:
await self.__update_fetcher_task
if self.__update_fetcher_task.done():
try:
self.__update_fetcher_task.result()
except BaseException as exc:
_LOGGER.critical(
"Fetching updates was aborted due to %s. Suppressing "
"exception to ensure graceful shutdown.",
exc,
exc_info=True,
)
else:
await self.update_queue.put(_STOP_SIGNAL)
_LOGGER.debug("Waiting for update_queue to join")
await self.update_queue.join()
await self.__update_fetcher_task
_LOGGER.debug("Application stopped fetching of updates.")

if self._job_queue:
Expand Down Expand Up @@ -1207,10 +1218,16 @@ async def _update_fetcher(self) -> None:
"Fetching updates got a asyncio.CancelledError. Ignoring as this task may only"
"be closed via `Application.stop`."
)
finally:
while not self.update_queue.empty():
self.update_queue.get_nowait()
self.update_queue.task_done()

async def __process_update_wrapper(self, update: object) -> None:
await self._update_processor.process_update(update, self.process_update(update))
self.update_queue.task_done()
try:
await self._update_processor.process_update(update, self.process_update(update))
finally:
self.update_queue.task_done()

async def process_update(self, update: object) -> None:
"""Processes a single update and marks the update to be updated by the persistence later.
Expand Down

0 comments on commit 0c27c50

Please sign in to comment.