Skip to content

new thread doesn't copy context of the parent thread #86981

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

Closed
uriyyo opened this issue Jan 3, 2021 · 4 comments
Closed

new thread doesn't copy context of the parent thread #86981

uriyyo opened this issue Jan 3, 2021 · 4 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@uriyyo
Copy link
Member

uriyyo commented Jan 3, 2021

BPO 42815
Nosy @gvanrossum, @jcea, @1st1, @uriyyo, @adriangb
PRs
  • bpo-42815: Fix issue when thread doesn't copy context of parent thread #24074
  • 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-01-03.11:43:34.701>
    labels = ['type-bug', 'library', '3.10']
    title = "new thread doesn't copy context of the parent thread"
    updated_at = <Date 2021-08-04.18:05:52.773>
    user = 'https://github.com/uriyyo'

    bugs.python.org fields:

    activity = <Date 2021-08-04.18:05:52.773>
    actor = 'adriangb'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-01-03.11:43:34.701>
    creator = 'uriyyo'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42815
    keywords = ['patch']
    message_count = 2.0
    messages = ['384262', '398642']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'jcea', 'yselivanov', 'uriyyo', 'adriangb']
    pr_nums = ['24074']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42815'
    versions = ['Python 3.10']

    @uriyyo
    Copy link
    Member Author

    uriyyo commented Jan 3, 2021

    New thread doesn't copy context of the parent thread.

    The minimal example to reproduce the issue:

    from threading import Thread
    from contextvars import ContextVar
    
    foo: ContextVar[str] = ContextVar("foo")
    
    
    def temp():
        print(foo.get())
    
    
    foo.set("bar")
    
    t = Thread(target=temp)
    t.start()
    t.join()
    

    Is it expected behavior?

    PEP-567 I didn't find anything regarding this case.

    @uriyyo uriyyo added 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 3, 2021
    @gvanrossum
    Copy link
    Member

    Trust me, it's too late to fix this in 3.10.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ryangalamb
    Copy link

    As I read PEP-567, it looks like contextvars were not intended to be automatically copied into new threads. One of the examples describes how to use a copy of the current thread's context vars in another thread: https://peps.python.org/pep-0567/#offloading-execution-to-other-threads

    Furthermore, threading.local() doesn't automatically copy its values into new threads, and (as I understand) contextvars is intended to be consistent with threading.local().

    It is a bit surprising that scheduling an asyncio task will copy the current context, while new threads get an empty context. But I expect the intention there was to be consistent with threading.local(). (When you schedule an asyncio task, it's still going to be in the same thread and have access to the same thread-local variables.)

    tl;dr I think this is working as intended, and we can close this issue. (But I agree that the behavior is bit surprising.)

    @gvanrossum
    Copy link
    Member

    Thanks for doing this research; I agree that we can close this.

    I don't want to speculate on the reasons for starting a new thread with an empty context; maybe it was an oversight, maybe it was being strict in the layering of dependencies (contexts depend on threads, so threads shouldn't depend on contexts), maybe it was something else. Maybe Yury could tell us, but he's got bigger things on his mind.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Development

    No branches or pull requests

    3 participants