python / python/cpython

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

Aperta
#153,005 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-subinterpreters type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con concurrent.interpreters.Queue.get() e Queue.put(), quindi confronta la gestione dei relativi timeout con quella di queue.Queue, come descritto nell’issue. Conferma il comportamento per i timeout frazionari, negativi e NaN e per le regolazioni dell’orologio; il lavoro è completo quando vengono preservate le attese frazionarie, viene sollevato ValueError dove specificato e viene utilizzata una deadline monotona. Esamina le PR collegate prima di iniziare, perché il lavoro è già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.