GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Combined PR to show `ContextVar` changes together by nascheme · Pull Request #154647 · python/cpython

Conversation

@nascheme

@nascheme nascheme commented Jul 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This is a draft PR and is not meant to be merged as it. It shows the result after merging the following changes:

  • GH-141148: Ensure decimal.getcontext() returns fresh copy. #151953
  • gh-154549: warning for thread_inherit_context change #154558
  • gh-154562: Add ContextVar(thread_inheritable=...). #154564
  • Future PR: change warnings and decimal to use ContextVar.thread_inheritable().

Related discussion is on Discourse.

Reasoning for each change:

  • ContextVar.thread_inheritable(): this new API is for libraries that want to convert global state to context-local state. It was intended that they should use ContextVar() since it acts like a thread-local and also switches context for asyncio tasks. However, ContextVar() has an issue in that new threads start with empty ContextVar bindings, they don't inherit the binding of the variable from the starting thread. This was an oversight but it has been how it works since contextvars was introduced. So changing it so threads do inherit is an incompatible change (effectively, this is turning the thread_inherit_context flag on by default). The ContextVar.thread_inheritable() API allows libraries to opt-in to what will be future behavior early. Suggest to merge for the 3.15 release.
  • deprecation warning: this warning will help identify code that will be affected by changing the default of thread_inherit_context to true. Ideally this warning should be in place for two releases before we change the default. Suggested to merge for the 3.16 release. Merging for 3.15 might be okay but I fear of people seeing new deprecation warnings that were not present in earlier 3.15 betas.
  • Make decimal.getcontext() return a fresh copy: this is required to avoid a bug if thread_inherit_context is true or if decimal starts using ContextVar.thread_inheritable() for its context. In that case, threads inherit the same mutable decimal.Context instance of the thread that spawned them. That's not thread safe and very likely not desired (e.g. a thread modifying the decimal context could affect the context of other threads). This bug exists in current Python if you explicitly set thread_inherit_context or if you use the free-threaded build, where that's set by default. Ideally this would be merged before 3.15 final since it's a bug.
  • Future PR, use ContextVar.thread_inheritable() for warnings and decimal: this change avoids deprecation warnings from these modules. Suggested to merge in 3.16. This changes decimal context slightly in the default (GIL-enabled) build (new threads start with a copy of the decimal context of the starter thread, rather than a copy of decimal.DefaultContext). IMHO, this is better but it is also an incompatible change.

In a future release (likely 3.18), we turn thread_inherit_context on by default. Then, ContextVar.thread_inheritable() and ContextVar() do the same thing. We can just leave thread_inheritable() as a soft-deprecated alias.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
nascheme added 5 commits July 24, 2026 12:09
@nascheme
pythongh-154549: warning for thread_inherit_context change
6acba47
On GIL-enabled builds, emit a DeprecationWarning when the default
implicitly empty threading.Thread() context causes a context variable
lookup to differ from the context that a future release will inherit by
default.
@nascheme
pythongh-154562: Add ContextVar.thread_inheritable().
b1795a7
Context variables created this way will automatically be inherited by
the context for new threads, regardless of the setting of
`thread_inherit_context`.
@nascheme
Add PyContextVar_NewThreadInheritable().
1f4b728
@nascheme
Ensure decimal.getcontext() returns fresh copy.
1e60975
We need a per-task copy of decimal.Context() so that mutations are
isolated between asyncio tasks and threads using
sys.flags.thread_inherit_context.  This is done by adding a "depth"
counter to PyContext and copying the decimal.Context() instance whenever
it doesn't match the depth of the current PyContext.
@nascheme
Fix for Android/iOS CI test failure.
1f146e6
@read-the-docs-community

read-the-docs-community Bot commented Jul 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33750081 | 📁 Comparing fbddda8 against main (998fc4a)

  🔍 Preview build  

6 files changed · ± 6 modified

± Modified

  • c-api/contextvars.html
  • howto/free-threading-python.html
  • library/contextvars.html
  • library/threading.html
  • using/cmdline.html
  • whatsnew/changelog.html
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nascheme
Use thread inheritable contextvars.
fbddda8
Change `warnings` and `decimal` to use thread inheritable context vars.
This means new threads will start with bindings for these variables from
the starter thread, rather than having empty bindings (no matter the
value of the thread_inherit_context flag).
@nascheme
nascheme force-pushed the ctx_thread_inherit_combined branch from bdbb491 to fbddda8 Compare July 24, 2026 20:47
@nascheme

nascheme commented Aug 1, 2026

Copy link
Copy Markdown
Member Author

Closing this since the PRs are going to need some refinement before they are ready for merging.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nascheme nascheme closed this Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently will be requested when the pull request is marked ready for review ericsnowcurrently is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity will be requested when the pull request is marked ready for review ZeroIntensity is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 will be requested when the pull request is marked ready for review FFY00 is a code owner
@1st1 1st1 Awaiting requested review from 1st1 1st1 will be requested when the pull request is marked ready for review 1st1 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@nascheme
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.