python / python/cpython

Warn about future default of `thread_inherit_context=1`.

Offen
#154,549 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Einstiegspunkt threading.Thread und dem Konfigurationsflag thread_inherit_context und überprüfe anschließend den verlinkten PR gh-154558 sowie die referenzierte Discourse-Diskussion. Als erledigt gilt die Aufgabe, wenn betroffener Code vor der künftigen Änderung des Standardwerts gewarnt wird, ohne den Standardwert bereits zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.