python / python/cpython

Deadlock when shutting down ThreadPoolExecutor from inside OS Signal handler

Aperta
#121,649 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug report

Bug description:

When running the code below and sending a SIGTERM to the process results in what looks like a deadlock on the ThreadPoolExecutor._shutdown_lock. I guess the issue is that the SIGTERM is being handled in main thread while in the submit function where the _shutdown_lock is already locked. When I try calling shutdown inside the signal handler it waits forever.

TBH I'm not sure if this is a bug or expected behavior but I wanted to file this anyways. Thanks!

from concurrent.futures import ThreadPoolExecutor
import signal


def main():
    with ThreadPoolExecutor(max_workers=2) as executor:

        def __exit(signal, frame):
            executor.shutdown(wait=True, cancel_futures=True)

        signal.signal(signal.SIGTERM, __exit)

        while True:
            executor.submit(lambda: 1 + 1)


if __name__ == "__main__":
    main()

AFTER sending SIGTERM to the process, this is what pystack shows:

(v) rbost@fedora:~/code/CheckDuplicate$ pystack remote 3431749
Traceback for thread 3431751 (python) [] (most recent call last):
    (Python) File "/usr/lib64/python3.12/threading.py", line 1030, in _bootstrap
        self._bootstrap_inner()
    (Python) File "/usr/lib64/python3.12/threading.py", line 1073, in _bootstrap_inner
        self.run()
    (Python) File "/usr/lib64/python3.12/threading.py", line 1010, in run
        self._target(*self._args, **self._kwargs)
    (Python) File "/usr/lib64/python3.12/concurrent/futures/thread.py", line 89, in _worker
        work_item = work_queue.get(block=True)

Traceback for thread 3431750 (python) [] (most recent call last):
    (Python) File "/usr/lib64/python3.12/threading.py", line 1030, in _bootstrap
        self._bootstrap_inner()
    (Python) File "/usr/lib64/python3.12/threading.py", line 1073, in _bootstrap_inner
        self.run()
    (Python) File "/usr/lib64/python3.12/threading.py", line 1010, in run
        self._target(*self._args, **self._kwargs)
    (Python) File "/usr/lib64/python3.12/concurrent/futures/thread.py", line 89, in _worker
        work_item = work_queue.get(block=True)

Traceback for thread 3431749 (python) [] (most recent call last):
    (Python) File "/home/rbost/code/CheckDuplicate/test.py", line 18, in <module>
        main()
    (Python) File "/home/rbost/code/CheckDuplicate/test.py", line 14, in main
        executor.submit(lambda: 1 + 1)
    (Python) File "/usr/lib64/python3.12/concurrent/futures/thread.py", line 175, in submit
        f = _base.Future()
    (Python) File "/usr/lib64/python3.12/concurrent/futures/_base.py", line 328, in __init__
        def __init__(self):
    (Python) File "/home/rbost/code/CheckDuplicate/test.py", line 9, in __exit
        executor.shutdown(wait=True, cancel_futures=True)
    (Python) File "/usr/lib64/python3.12/concurrent/futures/thread.py", line 220, in shutdown
        with self._shutdown_lock:
CPython versions tested on:

3.12

Operating systems tested on:

Linux

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

Riproduci l’esempio SIGTERM fornito, quindi esamina concurrent/futures/thread.py nelle vicinanze di ThreadPoolExecutor.submit(), shutdown() e _shutdown_lock. L’issue non definisce il comportamento previsto dell’handler dei segnali né indica un test; per procedere è necessario concordare tale comportamento e aggiungere un test di regressione che dimostri il completamento senza il deadlock segnalato.

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

Valutazione

Stack tecnologico
python
Ambito
backend, operating-systems
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.