python / python/cpython

Deadlock when shutting down ThreadPoolExecutor from inside OS Signal handler

Offen
#121,649 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das bereitgestellte SIGTERM-Beispiel und untersuche anschließend concurrent/futures/thread.py im Umfeld von ThreadPoolExecutor.submit(), shutdown() und _shutdown_lock. Das Issue definiert weder das erwartete Verhalten des Signal-Handlers noch benennt es einen Test; für den Fortschritt muss dieses Verhalten abgestimmt und ein Regressionstest hinzugefügt werden, der den Abschluss ohne den gemeldeten Deadlock demonstriert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend, operating-systems
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.