Skip to content

Warn about future default of thread_inherit_context=1. #154549

Description

@nascheme

Feature or enhancement

Proposal:

For background discussion, see this Discourse thread.

The current behavior of threading.Thread not inheriting context variables from the starting thread is considered a bug. It is an oversight in the initial implementation of contextvars (confirmed in discussion with Yury). In a future Python release (version to be decided) we want to change that to happen by default (i.e. make the thread_inherit_context config flag default to true) . For the free-threaded build, thread_inherit_context already defaults to true, since that's required for sensible thread-safe behavior from the warnings module.

The reason to change is that libraries want to replace global state with context-local state (typically to be thread-safe or to be asyncio compatible). It is surprising and inconvenient if this context-local state is "lost" when a new thread is started. The Discourse thread has some examples of real-world experience with this.

This is an incompatible change and so we want to warn about this if we detect code that might be affected. If the warning first appears in Python 3.15, PEP 387 requires it to remain in Python 3.15 and 3.16; the default may change in Python 3.17 at the earliest.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/enabling-thread-inherit-context-and-context-aware-warnings-by-default-on-both-builds/108205

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions