python / python/cpython

ThreadPoolExecutor marks interpreter as shutdown before non daemon threads exit

Aperta
#148,280 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

@ZeroIntensity ci sta già lavorando.

Dal 9/4/2026.

3.15 stdlib triaged 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 the main thread exits, concurrent.futures.ThreadPoolExecutor mistakenly marks the interpreter as shutdown, regardless of if there are non daemon threads still executing, this causes any work submitted to the executor to fail with RuntimeError: cannot schedule new futures after interpreter shutdown.

PoC:

import time
import threading

from concurrent.futures import ThreadPoolExecutor

def work():
    print("Starting work")
    time.sleep(1)
    print("Work complete")

def worker_thread():
    # sleep a little to give the main thread time to exit
    time.sleep(1)

    executor = ThreadPoolExecutor()
    
    future = executor.submit(work)
    

def main():
    thread = threading.Thread(target=worker_thread)
    thread.start()

    # main thread exits now

if __name__ == "__main__":
    main()

Result

Exception in thread Thread-1 (worker_thread):
Traceback (most recent call last):
  File "/usr/lib/python3.14/threading.py", line 1081, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/python3.14/threading.py", line 1023, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/*****/*****/implementation/./bug.py", line 18, in worker_thread
    future = executor.submit(work)
  File "/usr/lib/python3.14/concurrent/futures/thread.py", line 207, in submit
    raise RuntimeError('cannot schedule new futures after '
                       'interpreter shutdown')
RuntimeError: cannot schedule new futures after interpreter shutdown

Interpreter is marked as shutdown here: https://github.com/python/cpython/blob/f46a17b19ff535591b75f5f3df5b48f7b4f939b4/Lib/concurrent/futures/thread.py#L23-L37
Expected Result:
All non daemon threads should be able to use ThreadPoolExecutor until they exit.

example was tested on 3.14.0

cc @ZeroIntensity

CPython versions tested on:

3.14

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.