python / python/cpython

concurrent.interpreters.Queue.get()/put() mishandle the timeout argument

オープン
#153,005 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

stdlib topic-subinterpreters type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

concurrent.interpreters.Queue.get() and Queue.put() mishandle their
timeout argument in three related ways.

  1. The value is converted with int(timeout), which truncates a floating-point
    timeout to whole seconds. A timeout in the interval [0, 1) becomes a
    non-blocking call, and e.g. timeout=1.9 waits for only about one second.
    queue.Queue, which this queue is meant to be compatible with, accepts a
    floating-point number of seconds.

  2. Because of the same int() conversion, a small negative float such as
    timeout=-0.5 is truncated to 0 and passes the timeout < 0 check
    instead of raising ValueError.

  3. The deadline is computed with time.time() (the wall clock), while
    queue.Queue uses time.monotonic(). The timeout can therefore over- or
    under-wait if the system clock is adjusted (NTP step, manual change) during
    the call.

Reproducer (3.14.5)

import time
from concurrent.interpreters import create_queue

q = create_queue(maxsize=1)
q.put(b"x")                      # fill it
start = time.perf_counter()
try:
    q.put(b"y", timeout=0.5)     # expected: block ~0.5s, then QueueFull
except Exception as exc:
    print(type(exc).__name__, f"{(time.perf_counter() - start) * 1000:.1f} ms")
# -> QueueFull 0.0 ms   (expected ~500 ms)

q2 = create_queue()
start = time.perf_counter()
try:
    q2.get(timeout=0.9)          # expected: block ~0.9s, then QueueEmpty
except Exception as exc:
    print(type(exc).__name__, f"{(time.perf_counter() - start) * 1000:.1f} ms")
# -> QueueEmpty 0.0 ms  (expected ~900 ms)

For comparison, queue.Queue().get(timeout=0.5) blocks for about 500 ms.

The fix is to use the timeout value as given, reject a negative or NaN timeout
with ValueError, and base the deadline on time.monotonic().

Linked PRs
  • gh-153006
  • gh-154156
  • gh-155967
  • gh-156018
  • gh-156019

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

concurrent.interpreters.Queue.get() と Queue.put() から始め、issue に記載されているとおり、それらのタイムアウト処理を queue.Queue と比較します。小数、負数、NaN のタイムアウト、およびクロックの調整に対する動作を確認します。完了条件は、小数の待機時間を維持し、指定された箇所で ValueError を発生させ、単調増加する deadline を使用することです。すでに作業が進行中なので、始める前にリンクされた PRs を確認してください。

索引モデルが issue の本文から書いたものです。

評価

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

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

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