classification
Title: @asynccontextmanager doesn't work well with async generators
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Joshua Oreman, Valentin Lavrinenko, asvetlov, lukasz.langa, miss-islington, ncoghlan, ned.deily, yselivanov
Priority: Keywords: patch

Created on 2018-06-06 17:24 by Valentin Lavrinenko, last changed 2020-08-14 09:54 by asvetlov. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7467 merged yselivanov, 2018-06-07 03:26
PR 7506 closed miss-islington, 2018-06-08 00:31
PR 7507 merged yselivanov, 2018-06-08 00:36
PR 7514 merged yselivanov, 2018-06-08 02:36
PR 21878 merged miss-islington, 2020-08-14 09:23
Messages (12)
msg318853 - (view) Author: Valentin Lavrinenko (Valentin Lavrinenko) Date: 2018-06-06 17:24
```
@asynccontextmanager
async def ctx():
    yield


async def gen():
    async with ctx():
        yield 'hello'
        yield 'world'


async def main():
    async with ctx():
        async for value in gen():
            print(value)
            raise RuntimeError()
```

Running main() leads to `RuntimeError: generator didn't stop after throw()`. This happens because async gernerator's `.athrow()` method doesn't re-throw `GeneratorExit` exception; probably, this is wrong.
msg318854 - (view) Author: Valentin Lavrinenko (Valentin Lavrinenko) Date: 2018-06-06 17:27
Sorry, `async with ctx()` in main() is not needed.
msg318882 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-06-07 03:26
Would be nice to fix this in 3.7.0
msg318967 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-06-07 19:42
> Would be nice to fix this in 3.7.0

Please do!
msg318998 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-06-08 00:31
New changeset 52698c7ad9eae9feb35839fde17a7d1da8036a9b by Yury Selivanov in branch 'master':
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467)
https://github.com/python/cpython/commit/52698c7ad9eae9feb35839fde17a7d1da8036a9b
msg319005 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-06-08 01:32
New changeset 8de73d5a6914cfe55c23b0ad829cd2ba8954bc2e by Yury Selivanov in branch '3.6':
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467) (GH-7507)
https://github.com/python/cpython/commit/8de73d5a6914cfe55c23b0ad829cd2ba8954bc2e
msg319012 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-06-08 03:42
New changeset b0bb9a81f60ed248a44b4c8008c0549c3e9e741d by Yury Selivanov in branch '3.6':
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467) (#7514)
https://github.com/python/cpython/commit/b0bb9a81f60ed248a44b4c8008c0549c3e9e741d
msg375189 - (view) Author: Joshua Oreman (Joshua Oreman) Date: 2020-08-11 17:45
This doesn't appear to have been backported to 3.7, even though it's in 3.6.6 and 3.8.0a0.
msg375219 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-08-12 10:22
Indeed, the backport to 3.7 slipped through the cracks somehow; we should fix that. Thanks for bringing this up!
msg375388 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-08-14 09:44
New changeset cf79cbf4479e395bf7c4df2907f5a444639b4f6f by Miss Islington (bot) in branch '3.7':
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467) (GH-21878)
https://github.com/python/cpython/commit/cf79cbf4479e395bf7c4df2907f5a444639b4f6f
msg375389 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-08-14 09:53
I'm still not sure exactly what happened here but it looks like the backport to 3.7 (PR 7506) from the  original fix in master (pre-3.8) (PR 7467) failed but the backport to 3.6 (PR 7507) succeeded.  And then it was backported a second time to 3.6 (PR 7514) which also succeeded but had no effect since there were no intervening changes to those files. So it may be that PR 7514 was intended to be for 3.7 instead of 3.6.  In any case, a fresh backport from master to 3.7 (PR 21878) succeeded and tests pass, so into 3.7 it goes for release in 3.7.9.
msg375390 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-08-14 09:54
Thank you very much, Ned!
History
Date User Action Args
2020-08-14 09:54:49asvetlovsetmessages: + msg375390
2020-08-14 09:53:24ned.deilysetstatus: open -> closed
priority: release blocker ->
messages: + msg375389

resolution: fixed
stage: patch review -> resolved
2020-08-14 09:44:12ned.deilysetmessages: + msg375388
2020-08-14 09:23:11miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request21003
stage: backport needed -> patch review
2020-08-12 10:22:04ned.deilysetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg375219

stage: resolved -> backport needed
2020-08-11 17:45:43Joshua Oremansetnosy: + Joshua Oreman, lukasz.langa
messages: + msg375189
2018-06-08 03:42:50yselivanovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-06-08 03:42:23yselivanovsetmessages: + msg319012
2018-06-08 02:36:09yselivanovsetpull_requests: + pull_request7140
2018-06-08 01:32:46yselivanovsetmessages: + msg319005
2018-06-08 00:36:26yselivanovsetpull_requests: + pull_request7135
2018-06-08 00:31:35miss-islingtonsetpull_requests: + pull_request7134
2018-06-08 00:31:29yselivanovsetmessages: + msg318998
2018-06-07 19:42:33ned.deilysetmessages: + msg318967
2018-06-07 03:26:56yselivanovsetpriority: normal -> release blocker

type: behavior
components: + Interpreter Core, - asyncio
versions: + Python 3.6, Python 3.8
nosy: + ned.deily

messages: + msg318882
2018-06-07 03:26:17yselivanovsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7089
2018-06-06 18:16:25serhiy.storchakasetnosy: + ncoghlan
2018-06-06 17:27:13Valentin Lavrinenkosetmessages: + msg318854
2018-06-06 17:24:35Valentin Lavrinenkocreate