This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: contextvars.Context.run w/ coroutines gives inconsistent behavior
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: adriangb, tiangolo, yselivanov
Priority: normal Keywords:

Created on 2021-08-04 18:03 by adriangb, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg398924 - (view) Author: Adrian Garcia Badaracco (adriangb) * Date: 2021-08-04 18:03
I recently tried to use `contextvars.Context.run` w/ coroutines, expecting the same behavior as with regular functions, but it seems that `contextvars.Context.run` does not work w/ coroutines.

I'm sorry if this is something obvious to do with how coroutines work under the hood, if so I'd appreciate some help in understanding why this is the expected behavior.

```python
import asyncio
import contextvars


ctxvar = contextvars.ContextVar("ctxvar", default="spam")


def func():
    assert ctxvar.get() == "spam"

async def coro():
    func()


async def main():
    ctx = contextvars.copy_context()
    ctxvar.set("ham")
    ctx.run(func)  # works
    await ctx.run(coro)  # breaks

asyncio.run(main())
```

Thanks!
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 88997
2022-02-17 15:40:17tiangolosetnosy: + tiangolo
2021-08-04 18:06:43adriangbsetnosy: + yselivanov
2021-08-04 18:03:14adriangbcreate