From bab11fcee344df488d2e7f444ea3711ce87669e3 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 1 Mar 2020 13:56:41 -0800 Subject: _GeneratorTask: throw CancelledError in cancelled coroutine (bug 711174) Throw asyncio.CancelledError in a cancelled coroutine, ensuring that the coroutine can handle this exception in order to perform any necessary cleanup (like close the log file for bug 711174). Note that the asyncio.CancelledError will only be thrown in the coroutine if there's an opportunity (yield) before the generator raises StopIteration. Also fix the AsynchronousTask exit listener handling for compatibility with this new behavior. Fixes: 8074127bbc21 ("SpawnProcess: add _main coroutine") Bug: https://bugs.gentoo.org/711174 Signed-off-by: Zac Medico --- lib/_emerge/AsynchronousTask.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib/_emerge') diff --git a/lib/_emerge/AsynchronousTask.py b/lib/_emerge/AsynchronousTask.py index 1e9e177cb..580eef050 100644 --- a/lib/_emerge/AsynchronousTask.py +++ b/lib/_emerge/AsynchronousTask.py @@ -64,7 +64,7 @@ class AsynchronousTask(SlotObject): @returns: Future, result is self.returncode """ waiter = self.scheduler.create_future() - exit_listener = lambda self: waiter.set_result(self.returncode) + exit_listener = lambda self: waiter.cancelled() or waiter.set_result(self.returncode) self.addExitListener(exit_listener) waiter.add_done_callback(lambda waiter: self.removeExitListener(exit_listener) if waiter.cancelled() else None) @@ -180,9 +180,15 @@ class AsynchronousTask(SlotObject): def removeExitListener(self, f): if self._exit_listeners is None: if self._exit_listener_stack is not None: - self._exit_listener_stack.remove(f) + try: + self._exit_listener_stack.remove(f) + except ValueError: + pass return - self._exit_listeners.remove(f) + try: + self._exit_listeners.remove(f) + except ValueError: + pass def _wait_hook(self): """ -- cgit v1.2.3-18-g5258