diff --git a/tools/daemon_pool.py b/tools/daemon_pool.py index 2fb5a61d0a24..5e44fff5d07e 100644 --- a/tools/daemon_pool.py +++ b/tools/daemon_pool.py @@ -26,6 +26,7 @@ from __future__ import annotations +import sys import threading import weakref from concurrent.futures import ThreadPoolExecutor @@ -38,8 +39,10 @@ class DaemonThreadPoolExecutor(ThreadPoolExecutor): """ThreadPoolExecutor variant whose workers do not block process exit.""" def _adjust_thread_count(self) -> None: - # Mirrors CPython's implementation (3.8–3.13) with two changes: + # Mirrors CPython's implementation with two changes: # daemon=True and no _threads_queues registration. + # Python 3.14 removed _initializer / _initargs, replacing them + # with _create_worker_context() and a 3-arg _worker signature. if self._idle_semaphore.acquire(timeout=0): return @@ -49,15 +52,23 @@ def weakref_cb(_, q=self._work_queue): num_threads = len(self._threads) if num_threads < self._max_workers: thread_name = "%s_%d" % (self._thread_name_prefix or self, num_threads) - t = threading.Thread( - name=thread_name, - target=_worker, - args=( + if sys.version_info >= (3, 14): + worker_args = ( + weakref.ref(self, weakref_cb), + self._create_worker_context(), + self._work_queue, + ) + else: + worker_args = ( weakref.ref(self, weakref_cb), self._work_queue, self._initializer, self._initargs, - ), + ) + t = threading.Thread( + name=thread_name, + target=_worker, + args=worker_args, daemon=True, ) t.start()