python / python/cpython

concurrent.futures.ProcessPoolExecutor raises during shutdown

Aperta
#131,598 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-multiprocessing 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:
from concurrent.futures import ProcessPoolExecutor as Pool

def nope():
    pass

def mk_done(pool):
    def done(_fut):
        pool.shutdown(wait=True)
    return done

if __name__ == "__main__":
    pool = Pool()
    future = pool.submit(nope)
    future.add_done_callback(mk_done(pool))

ProcessPoolExecutor invokes future done callbacks from its manager thread. When one of those future callbacks invokes ProcessPoolExecutor.shutdown(), shut down fails with a RuntimeError: cannot join current thread because the implementation joins the manager thread without protecting against self-joins.

The relevant lines Lib/concurrent/futures/process.py:845-846 currently read:

if self._executor_manager_thread is not None and wait:
            self._executor_manager_thread.join()

but should probably be updated to:

t = self._executor_manager_thread
if t is not None and wait and threading.current_thread() != t:
            self._executor_manager_thread.join()

Invoking shutdown(False) avoids the problem but also makes it impossible to wait for pool completion. A more general solution would be to deprecate the wait parameter for shutdown, never waiting in that method, and add a separate method, say wait_for_shutdown, that uses a threading.Event to wake any waiting threads. Alas, correctly setting that Event is going to be a bit involved.

While I tested on 3.12, the bug is also present in 3.13 and current.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

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 eseguendo la riproduzione fornita di ProcessPoolExecutor, quindi esamina Lib/concurrent/futures/process.py intorno alle righe 845-846 e il punto di ingresso di shutdown. Aggiungi la copertura per shutdown(wait=True) da un callback di future e verifica che il callback venga completato senza un errore di self-join, mentre il completamento del pool rimanga ancora attendibile.

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
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.