python / python/cpython

Callbacks pended by `Py_AddPendingCall` will never be invoked if the main thread doesn't release GIL

未关闭
#95,820 6 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@markshannon 已经在做这个了。

开始于 2022年8月23日。

3.10 3.11 3.12 interpreter-core type-bug
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

描述

According to the Py_AddPendingCall's document (https://docs.python.org/3/c-api/init.html#c.Py_AddPendingCall), a pended callback function should be invoked when:

It will be called asynchronously with respect to normally running Python code, but with both these conditions met:

- on a bytecode boundary;

- with the main thread holding the global interpreter lock.

So if the main thread is processing an endless loop like:

while True:
    pass

A callback pended from another thread by Py_AddPendingCall should still be able to be invoked.

It works in Python 3.8.13, but doesn't work in Python 3.9.13. In 3.9.13, the callback pended by Py_AddPendingCall will never be invoked in this case.

The root cause may be here: https://github.com/python/cpython/pull/19091/files#diff-c22186367cbe20233e843261998dc027ae5f1f8c0d2e778abfa454ae74cc59deL152-L153. After this change, pending a callback from a non-main thread will not set the ceval2->eval_breaker to be true. So when the main thread is trapped in an endless loop and keeps holding GIL, pending callbacks will never be invoked.

贡献指南

打开贡献指南

从这里开始

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

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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