Kyle Stanley <aeros...@gmail.com> added the comment:

Upon further investigation, I've realized that the issue is just that the 
cancel() override for `_GatheringFuture` never sets its state to CANCELLED at 
any point (unlike its parent), and is instead going to always be set to 
FINISHED because of the `outer.set_exception()` 
(https://github.com/python/cpython/blob/b8f67c0923ac85468dfbfd43375e0bbfb6ca50ea/Lib/asyncio/tasks.py#L826
 or 
https://github.com/python/cpython/blob/b8f67c0923ac85468dfbfd43375e0bbfb6ca50ea/Lib/asyncio/tasks.py#L791,
 depending on the arg for *return_exceptions*). 

Since we are unable to set the state of `_GatheringFuture` (at least not 
without causing side effects), I'm starting to agree that it makes sense to 
override the behavior for `cancelled()`. However, it might make more sense to 
replace the `self._cancel_requested` check with  `isinstance(self.exception(), 
exceptions.CancelledError)`, as this more accurately indicates if and when the 
gather() was cancelled. I don't think we should rely on 
`self._cancel_requested` since it would be true before the future is actually 
cancelled and not always be correct since the gather() can be cancelled without 
setting `self._cancel_requested`.

Here's one case where relying on `self._cancel_requested` for 
future_gather.cancelled() wouldn't work, based on a slight modification of the 
reproducer example:

```
async def main():
    """Cancel a gather() future and child and return it."""
    task_child = ensure_future(sleep(1.0))
    future_gather = gather(task_child)

    task_child.cancel()
    try:
        await future_gather
    except asyncio.CancelledError:
        pass

    return future_gather, task_child
```

(Despite `await future_gather` raising a CancelError and effectively being 
cancelled, `future_gather.cancelled()` would return false since 
`self._cancel_requested` was never set to true.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40894>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to