Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-43419: fix contextvars behaviors in asyncio REPL #24773

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lanfon72
Copy link

@lanfon72 lanfon72 commented Mar 6, 2021

bug tracker

I just make the little enhance to propagate Context between MainThread and REPLThread, to let contextvars work properly in asyncio REPL environment.

Here is the new behavior after this PR merged:

asyncio REPL 3.8.6 (default, Oct 18 2020, 00:27:24) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from contextvars import ContextVar
>>> ctx = ContextVar('ctx')
>>> ctx.get(None)
>>> ctx.set(1)
<Token var=<ContextVar name='ctx' at 0x10167c8b0> at 0x10167bd00>
>>> ctx.get()
1
>>> async def coro_f():
...   return ctx.get()
... 
>>> await coro_f()
1
>>> def normal_f():
...   return ctx.get()
... 
>>> normal_f()
1
>>> exit()

https://bugs.python.org/issue43419

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@lanfon72

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@lanfon72
Copy link
Author

lanfon72 commented Mar 7, 2021

I think there still got a bug that REPLThread use loop.create_task to schedule a coroutine, which means it will implicit do copy_context() inside.
But when we await a coroutine, which means we use the same context instead of a copied version.

here is the expected behavior using aioconsole:

Python 3.8.6 (default, Oct 18 2020, 00:27:24) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
---
This console is running in an asyncio event loop.
It allows you to wait for coroutines using the '{0}' syntax.
Try: await asyncio.sleep(1, result=3)
---
>>> from contextvars import ContextVar
>>> ctx = ContextVar('ctx')
>>> ctx.set(1)
<Token var=<ContextVar name='ctx' at 0x10b077db0> at 0x10b0c1cc0>
>>> ctx.get()
1
>>> async def awtf():
...   ctx.set(2)
...   return ctx.get()
... 
>>> await awtf()
2
>>> ctx.get()
2
>>> 

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 14, 2021
@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants