From 0bf0ca0b85244ca3c78a78f6eb7ada396f8660aa Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Mar 2021 15:48:33 +0100 Subject: [PATCH 1/2] Erase background_tasks list in `Application.cancel_and_wait_for_background_tasks`. --- prompt_toolkit/application/application.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/prompt_toolkit/application/application.py b/prompt_toolkit/application/application.py index 4385d03c9..1a7edf722 100644 --- a/prompt_toolkit/application/application.py +++ b/prompt_toolkit/application/application.py @@ -5,6 +5,7 @@ import sys import threading import time +import warnings from asyncio import ( AbstractEventLoop, CancelledError, @@ -285,6 +286,8 @@ def __init__( self.loop: Optional[AbstractEventLoop] = None self.context: Optional[contextvars.Context] = None + self.background_tasks: List[Task[None]] = [] + #: Quoted insert. This flag is set if we go into quoted insert mode. self.quoted_insert = False @@ -425,11 +428,8 @@ def reset(self) -> None: # returning, and when we have multiple buffers, we clearly want the # content in the other buffers to remain unchanged between several # calls of `run`. (And the same is true for the focus stack.) - self.exit_style = "" - self.background_tasks: List[Task[None]] = [] - self.renderer.reset() self.key_processor.reset() self.layout.reset() @@ -937,15 +937,23 @@ async def cancel_and_wait_for_background_tasks(self) -> None: (If we had nurseries like Trio, this would be the `__aexit__` of a nursery.) """ - for task in self.background_tasks: + tasks_to_be_cancelled = self.background_tasks + self.background_tasks = [] + + for task in tasks_to_be_cancelled: task.cancel() - for task in self.background_tasks: + for task in tasks_to_be_cancelled: try: await task except CancelledError: pass + if self.background_tasks: + warnings.warn( + "New background tasks were created while prompt_toolkit application terminates." + ) + async def _poll_output_size(self) -> None: """ Coroutine for polling the terminal dimensions. From 0f62d9edb87aa82c800b54b8f531bba3de2f154a Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Mon, 22 Mar 2021 15:49:34 +0100 Subject: [PATCH 2/2] Don't allow creation of background tasks in DummyApplication. --- prompt_toolkit/application/dummy.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/prompt_toolkit/application/dummy.py b/prompt_toolkit/application/dummy.py index 1124f7026..40abe4310 100644 --- a/prompt_toolkit/application/dummy.py +++ b/prompt_toolkit/application/dummy.py @@ -46,3 +46,8 @@ async def run_system_command( def suspend_to_background(self, suspend_group: bool = True) -> None: raise NotImplementedError + + def create_background_task( + self, coroutine: "Awaitable[None]" + ) -> "asyncio.Task[None]": + raise NotImplementedError