"""Bounded daemon workers with a finite shutdown wait. Python cannot safely terminate a blocked thread. Owners cancel their browser resources on timeout and retain a failed group until those calls unwind. """ import logging import queue import threading from utils.resource_lifecycle import join_threads class TaskExecutor: def __init__(self, max_workers, name): self._slots = threading.BoundedSemaphore(max_workers) self._queue = queue.Queue(maxsize=max_workers) self._lock = threading.Lock() self._closed = threading.Event() self._threads = [] self._max_workers = max_workers self._name = name def _worker(self): while not self._closed.is_set() or not self._queue.empty(): try: fn, args = self._queue.get(timeout=0.05) except queue.Empty: continue try: fn(*args) except Exception: logging.getLogger(__name__).exception('Background browser job failed') finally: self._slots.release() self._queue.task_done() def submit(self, fn, *args): with self._lock: if self._closed.is_set() or not self._slots.acquire(blocking=False): return False try: if len(self._threads) < self._max_workers: thread = threading.Thread(target=self._worker, daemon=True, name=f'{self._name}-{len(self._threads)}') thread.start() self._threads.append(thread) self._queue.put_nowait((fn, args)) except Exception: self._slots.release() raise return True def shutdown(self, timeout=5): with self._lock: self._closed.set() return join_threads(self._threads, timeout)