python / python/cpython

ThreadPoolExecutor marks interpreter as shutdown before non daemon threads exit

未关闭
#148,280 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@ZeroIntensity 已经在做这个了。

开始于 2026年4月9日。

3.15 stdlib triaged type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。