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

contextvars does not work properly in asyncio REPL. #87585

Open
lanfon72 mannequin opened this issue Mar 6, 2021 · 4 comments
Open

contextvars does not work properly in asyncio REPL. #87585

lanfon72 mannequin opened this issue Mar 6, 2021 · 4 comments
Labels
3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@lanfon72
Copy link
Mannequin

lanfon72 mannequin commented Mar 6, 2021

BPO 43419
Nosy @asvetlov, @1st1, @lanfon72
PRs
  • bpo-43419: fix contextvars behaviors in asyncio REPL #24773
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-03-06.14:09:53.246>
    labels = ['type-bug', '3.8', '3.9', 'expert-asyncio']
    title = 'contextvars does not work properly in asyncio REPL.'
    updated_at = <Date 2021-03-06.15:06:01.651>
    user = 'https://github.com/lanfon72'

    bugs.python.org fields:

    activity = <Date 2021-03-06.15:06:01.651>
    actor = 'lanfon72'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2021-03-06.14:09:53.246>
    creator = 'lanfon72'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43419
    keywords = ['patch']
    message_count = 1.0
    messages = ['388199']
    nosy_count = 3.0
    nosy_names = ['asvetlov', 'yselivanov', 'lanfon72']
    pr_nums = ['24773']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43419'
    versions = ['Python 3.8', 'Python 3.9']

    @lanfon72
    Copy link
    Mannequin Author

    lanfon72 mannequin commented Mar 6, 2021

    Demonstration (via python -m asyncio):

    asyncio REPL 3.9.0 (default, Oct 18 2020, 00:21:26) 
    [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.set(1)
    <Token var=<ContextVar name='ctx' at 0x1021bbc70> at 0x1021bf800>
    >>> ctx.get()
    Traceback (most recent call last):
      File "/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/concurrent/futures/_base.py", line 440, in result
        return self.__get_result()
      File "/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/concurrent/futures/_base.py", line 389, in __get_result
        raise self._exception
      File "/Users/lanfon/.pyenv/versions/3.9.0/lib/python3.9/asyncio/__main__.py", line 34, in callback
        coro = func()
      File "<console>", line 1, in <module>
    LookupError: <ContextVar name='ctx' at 0x1021bbc70>
    >>> exit()

    It also got problem inside the functions when the context is referenced in global scope.

    @lanfon72 lanfon72 mannequin added 3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels Mar 6, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel
    Copy link
    Member

    iritkatriel commented May 18, 2022

    3.9 is no longer maintained and I can't reproduce this on 3.10 and 3.11:

    % python3.10 -m asyncio
    asyncio REPL 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] 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.set(1)
    <Token var=<ContextVar name='ctx' at 0x10f704fe0> at 0x10f702000>
    >>> 
    

    I don't know what "other problems" you were seeing, but if you are still seeing them in 3.10+ please open reopen this or create a new issue with full details.

    @wicol
    Copy link

    wicol commented Jun 20, 2023

    I'm getting the same thing with 3.11:

    $ docker run --rm -it python:3.11-slim python -m asyncio
    asyncio REPL 3.11.4 (main, Jun 14 2023, 18:43:41) [GCC 12.2.0] on linux
    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.set(1)
    <Token var=<ContextVar name='ctx' at 0xffff900bb560> at 0xffff90051880>
    >>> ctx.get()
    Traceback (most recent call last):
      File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
        return self.__get_result()
               ^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
        raise self._exception
      File "/usr/local/lib/python3.11/asyncio/__main__.py", line 34, in callback
        coro = func()
               ^^^^^^
      File "<console>", line 1, in <module>
    LookupError: <ContextVar name='ctx' at 0xffff900bb560>
    >>>
    

    Oh and I'm on ARM (M1).

    @iritkatriel iritkatriel reopened this Jun 20, 2023
    @terryjreedy
    Copy link
    Member

    Irit, you left off ctc.get(), which is what fails. I get same failure on Win10 local main compiled 10 hours ago.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants