Thread.join(timeout) can block forever: ThreadHandle_join reads the clock twice through Py_MAX (3.13, 3.14)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 72/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- operating-systems
Direzione di ricerca
Leggi Modules/_threadmodule.c intorno a ThreadHandle_join (riga 517 per 3.13, riga 544 per 3.14) e confrontalo con l'hunk di main del commit ba91215ca6. Il bug è che Py_MAX valuta _PyDeadline_Get(deadline) due volte; fai in modo che venga letto una sola volta in timeout_ns e limitato a zero prima di PyEvent_WaitTimed. Controlla in Python/parking_lot.c come un timeout negativo significhi bloccare per sempre, poi esegui i test di threading su entrambi i branch; il lavoro è completato quando Thread.join con timeout non può mai passare un timeout negativo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 77.9k
- Fork
- 37.1k
- Merge medio
- 20h 34m
- PR unite (30g)
- 660
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/cpython
-
extension-modules topic-IO type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
platform: import _wmi on first useForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaOS-windows performance stdlib type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
python/cpython#161513 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
OS-windows performance stdlib topic-lazy-imports type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
python/cpython#161510 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
build OS-unsupported OS-windows type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 60/100
I maintainer di solito rispondono entro 1 giorno
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689Forse già presa @TheLouisMahdi l’ha presa 8 giorni fa. Apertadocs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
python/cpython#161078 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di python/cpython
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Project-N-E-K-O/N.E.K.O#3670 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
type:docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
jejjohnson/xtremax#123 ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
microsoft/agent-framework#10218 ·
I maintainer di solito rispondono entro 1 giorno
-
fidelity
Difficoltà 2/5 Mezza giornata Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno