Free-threading: `signal.pthread_kill` deadlocks child threads
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Context
The Dask threaded scheduler (e.g. dask.array or dask.dataframe without dask.distributed) blocks the main thread when the user calls compute() and internally spawns a concurrent.futures.ThreadPoolExecutor to schedule tasks. If a user hits Ctrl+C, they expect the program to terminate soon (at most waiting for the tasks that are halfway through execution to terminate).
Bug report
This sequence frequently deadlocks on Linux x64 free-threaded:
- main thread calls
threading.Thread.start - another thread calls
signal.pthread_kill(main_thread, signal.SIGINT), which lands afterstart()has returned but before the thread'stargetfunction has started executing - main thread handles the resulting
KeyboardInterrupt - main thread calls
.join()on the child thread started at point 1. - frequently,
.join()deadlocks. A second SIGINT (e.g. from Ctrl+C) unblocks it.
Reproducer
import signal
import threading
USE_BARRIER = False
NTHREADS = 20
def demo():
main_thread = threading.get_ident()
barrier = threading.Barrier(NTHREADS) if USE_BARRIER else None
thread_can_die = threading.Event()
def f() -> None:
if barrier:
barrier.wait()
thread_can_die.wait()
def interrupt() -> None:
if barrier:
barrier.wait()
signal.pthread_kill(main_thread, signal.SIGINT)
threads = []
for _ in range(NTHREADS - 1):
t = threading.Thread(target=f)
t.start()
threads.append(t)
try:
t = threading.Thread(target=interrupt)
t.start()
threads.append(t)
for t in threads:
t.join()
except KeyboardInterrupt:
pass
else:
assert False, "Expected KeyboardInterrupt"
thread_can_die.set()
for t in threads:
t.join()
if __name__ == "__main__":
i = 0
while True:
print(i, flush=True)
demo()
i += 1
On these setups, the above program hangs within a handful of cycles:
- Linux x64 3.14.5t
- Linux x64 git tip #7a468a101268d2b13105f94ae027df8b502d0c87 (built with
Tools/pixi-packages/freethreading)
Cannot reproduce on
- Linux x64 3.14.5
Setting USE_BARRIER=True, which ensures that all child threads are already fully started when the SIGINT lands on the main thread, makes the issue disappear.
CPython versions tested on:
CPython main branch, 3.14
Operating systems tested on:
Linux
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führe zunächst den bereitgestellten Reproducer unter Linux x64 free-threaded CPython aus und verfolge dann die Wechselwirkung zwischen threading.Thread.start(), threading.Thread.join() und signal.pthread_kill(). Die Aufgabe ist erfüllt, wenn der Reproducer nicht mehr deadlockt, wenn SIGINT eintrifft, bevor das Target eines Kind-Threads beginnt, und das erwartete KeyboardInterrupt-Verhalten intakt bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- operating-systems
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100