Thread.join(timeout) can block forever: ThreadHandle_join reads the clock twice through Py_MAX (3.13, 3.14)

未关闭 适合新手
#161,712 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, python

调研方向

阅读Modules/_threadmodule.c中ThreadHandle_join附近的代码(3.13第517行,3.14第544行),并与提交ba91215ca6中main的代码块进行比较。问题在于Py_MAX会对_PyDeadline_Get(deadline)求值两次;修改为只将其读取一次到timeout_ns,并在PyEvent_WaitTimed之前将其限制为零。查看Python/parking_lot.c,了解负超时意味着永久阻塞,然后在两个分支上运行threading测试;当带超时的Thread.join绝不可能传入负超时时,即为完成。

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

描述

extension-modules type-bug

Bug report

ThreadHandle_join in Modules/_threadmodule.c recomputes the time left of a timed join as

// _PyDeadline_Get will return a negative value if the deadline has
// been exceeded.
timeout_ns = Py_MAX(_PyDeadline_Get(deadline), 0);

(3.13 branch, line 517; 3.14 branch, line 544; also in v3.13.16 and v3.14.0.)

Py_MAX(x, y) is (((x) > (y)) ? (x) : (y)) (Include/pymacro.h), so it evaluates _PyDeadline_Get(deadline) twice. The first read is compared with 0 and the second is the one returned. If the deadline passes between the two reads, the first is positive, the second is negative, and a negative timeout_ns is returned. The loop then calls PyEvent_WaitTimed(..., timeout_ns, ...) with it, and _PySemaphore_PlatformWait in Python/parking_lot.c treats timeout < 0 as "no timeout" (INFINITE on Windows; the untimed sem_wait/pthread_cond_wait branch on POSIX). The if (timeout_ns) check after it does not catch a negative value. Thread.join(timeout) then never returns.

_PyMutex_LockTimed in Python/lock.c clamps correctly ("Avoid negative values because those mean block forever"), so only the timed Thread.join is exposed.

Why it needs a wait that ends early. The window is the interval between the two clock reads, after a wait that ended a little before the deadline. On Windows, wait timeouts are counted in timer ticks (15.6 ms by default) while the deadline uses the high-resolution clock, so a wait often ends a fraction of a tick early and sends the loop around again. A program that joins in a loop with a short timeout gives it many chances.

Observed. A pyqtgraph window's GUI thread (Windows 11, Python 3.13.16 from conda-forge) polled worker.join(0.02) fifty times a second. It hung repeatedly, within 0.5 to 5.5 hours of starting. Caught live, the hung process's main thread used no CPU in ten seconds, its Python stack was in Thread.join, and its native stack, read with dbghelp and the matching python313.pdb, was ThreadHandle_join -> _PyParkingLot_Park -> _PySemaphore_Wait -> WaitForSingleObjectEx, with the timeout argument 0xffffffff (INFINITE). Its parking-lot wait entry was queued on the worker thread's handle with is_unparking 0, so it was in its first wait, the one that takes the join's own timeout, and that wait had been given none. The worker thread was alive and running throughout.

I could not provoke it in a synthetic test (22 million join(1e-6 .. 0.02) calls from 8 to 16 threads on macOS, 3.13.5, no overrun beyond 40 ms), since the window is microseconds wide.

Fix. main already reads the clock once, as part of gh-157495 (ba91215ca6, "Avoid Py_MIN()/Py_MAX() side effects", GH-157591):

timeout_ns = _PyDeadline_Get(deadline);
timeout_ns = Py_MAX(timeout_ns, 0);

That commit was a cleanup of several files and was not marked as a bug fix, so it was not backported. Could the _threadmodule.c hunk be backported to 3.13 and 3.14? It is one line and changes nothing else.

Your environment

  • CPython versions tested: 3.13.16 (conda-forge, Windows 11); the lines are the same on the 3.14 branch
  • Operating system: Windows 11
主要语言
Python
星标
77.9k
派生
37.1k
平均合并
20 小时 34 分钟
30 天内合并 PR
660

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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