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

オープン 初心者向け
#161,712 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, python

調査の方向性

Modules/_threadmodule.cのThreadHandle_join付近(3.13では517行目、3.14では544行目)を読み、commit ba91215ca6のmainのhunkと比較してください。バグは、Py_MAXが_PyDeadline_Get(deadline)を2回評価することです。値を一度だけ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分
マージ済み PR(30日)
660

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。