Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/tools/test_daemon_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ def test_wedged_worker_does_not_block_interpreter_exit():
assert "main-done" in proc.stdout


def test_worker_signature_detection_matches_runtime():
"""_WORKER_USES_CTX must reflect the actual _worker signature."""
import inspect

from concurrent.futures.thread import _worker

from tools.daemon_pool import _WORKER_USES_CTX

param_count = len(inspect.signature(_worker).parameters)
if sys.version_info >= (3, 14):
assert param_count == 3, f"Expected 3 params on 3.14+, got {param_count}"
assert _WORKER_USES_CTX is True
else:
assert param_count == 4, f"Expected 4 params on <3.14, got {param_count}"
assert _WORKER_USES_CTX is False


def test_concurrent_submit_with_context_path():
"""Multiple concurrent submits must work under the ctx-based worker path."""
pool = DaemonThreadPoolExecutor(max_workers=3)
try:
futures = [pool.submit(lambda x: x ** 2, i) for i in range(10)]
results = sorted(f.result(timeout=10) for f in futures)
assert results == [i ** 2 for i in range(10)]
finally:
pool.shutdown(wait=True)


def _repo_root():
import pathlib

Expand Down
27 changes: 21 additions & 6 deletions tools/daemon_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@

from __future__ import annotations

import inspect
import threading
import weakref
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures.thread import _worker

__all__ = ["DaemonThreadPoolExecutor"]

# Python ≥ 3.14 changed _worker from 4 params to 3, replacing
# (initializer, initargs) with a WorkerContext object.
_WORKER_USES_CTX = len(inspect.signature(_worker).parameters) == 3


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.
if self._idle_semaphore.acquire(timeout=0):
return
Expand All @@ -49,15 +54,25 @@ 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 _WORKER_USES_CTX:
# Python >= 3.14: _worker(executor_ref, ctx, work_queue)
args = (
weakref.ref(self, weakref_cb),
self._create_worker_context(),
self._work_queue,
)
else:
# Python <= 3.13: _worker(executor_ref, work_queue, initializer, initargs)
args = (
weakref.ref(self, weakref_cb),
self._work_queue,
self._initializer,
self._initargs,
),
)
t = threading.Thread(
name=thread_name,
target=_worker,
args=args,
daemon=True,
)
t.start()
Expand Down
Loading