python / python/cpython

Warn about future default of `thread_inherit_context=1`.

未关闭
#154,549 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 threading.Thread 入口点和 thread_inherit_context 配置标志开始,然后审查链接的 PR gh-154558 以及所引用的 Discourse 讨论。完成的标准是,在尚未更改默认值的情况下,让受影响的代码收到关于未来默认值变更的警告。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。