[New-bugs-announce] [issue44834] contextvars.Context.run w/ coroutines gives inconsistent behavior

Adrian Garcia Badaracco report at bugs.python.org
Wed Aug 4 14:03:14 EDT 2021


New submission from Adrian Garcia Badaracco <adrian at adriangb.com>:

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!

----------
components: Library (Lib)
messages: 398924
nosy: adriangb
priority: normal
severity: normal
status: open
title: contextvars.Context.run w/ coroutines gives inconsistent behavior
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44834>
_______________________________________


More information about the New-bugs-announce mailing list