python / python/cpython

Warn about future default of `thread_inherit_context=1`.

Open
#154,549 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-154558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the threading.Thread entry point and the thread_inherit_context configuration flag, then review the linked PR gh-154558 and the referenced Discourse discussion. Done means affected code is warned about the future default change without changing the default yet.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.