diff options
| author | Adrian Herrmann <adrian.herrmann@qt.io> | 2023-12-23 18:07:26 +0100 |
|---|---|---|
| committer | Adrian Herrmann <adrian.herrmann@qt.io> | 2024-01-07 16:21:29 +0100 |
| commit | b91596118fda9b586f19adcb1feee1db40abeb86 (patch) | |
| tree | 52f9ab13bc7c2ea1b43e4a430317f27c09bf6bdd /sources/pyside6/PySide6/QtAsyncio/tasks.py | |
| parent | 10a75de16b1d5e8f4acd1af121aaa56cbe4e65a4 (diff) | |
QtAsyncio: Fix cancelling waiting tasks
A task that is awaiting a future must also cancel this future in order
for the cancellation to be successful.
Pick-to: 6.6
Task-number: PYSIDE-769
Change-Id: I22a9132fc8506e7a007fe625bc9217f0760bdc6b
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6/QtAsyncio/tasks.py')
| -rw-r--r-- | sources/pyside6/PySide6/QtAsyncio/tasks.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/sources/pyside6/PySide6/QtAsyncio/tasks.py b/sources/pyside6/PySide6/QtAsyncio/tasks.py index c8e7da7e4..4f214c65a 100644 --- a/sources/pyside6/PySide6/QtAsyncio/tasks.py +++ b/sources/pyside6/PySide6/QtAsyncio/tasks.py @@ -27,6 +27,9 @@ class QAsyncioTask(futures.QAsyncioFuture): self._cancellation_requests = 0 + self._future_to_await: typing.Optional[asyncio.Future] = None + self._cancel_message: typing.Optional[str] = None + asyncio._register_task(self) # type: ignore[arg-type] def __repr__(self) -> str: @@ -66,6 +69,7 @@ class QAsyncioTask(futures.QAsyncioFuture): if self.done(): return result = None + self._future_to_await = None try: asyncio._enter_task(self._loop, self) # type: ignore[arg-type] @@ -83,7 +87,7 @@ class QAsyncioTask(futures.QAsyncioFuture): except StopIteration as e: self._state = futures.QAsyncioFuture.FutureState.DONE_WITH_RESULT self._result = e.value - except concurrent.futures.CancelledError as e: + except (concurrent.futures.CancelledError, asyncio.exceptions.CancelledError) as e: self._state = futures.QAsyncioFuture.FutureState.CANCELLED self._exception = e except BaseException as e: @@ -93,6 +97,7 @@ class QAsyncioTask(futures.QAsyncioFuture): if asyncio.futures.isfuture(result): result.add_done_callback( self._step, context=self._context) # type: ignore[arg-type] + self._future_to_await = result elif result is None: self._loop.call_soon(self._step, context=self._context) else: @@ -137,7 +142,8 @@ class QAsyncioTask(futures.QAsyncioFuture): return False self._cancel_message = msg self._handle.cancel() - self._state = futures.QAsyncioFuture.FutureState.CANCELLED + if self._future_to_await is not None: + self._future_to_await.cancel(msg) return True def uncancel(self) -> None: |
