
    (Gj	                    `    d Z ddlmZ ddlZddlZddlmZ ddlmZ dgZ	 G d de          Z
dS )u  Shared daemon-thread ThreadPoolExecutor.

Stdlib ``ThreadPoolExecutor`` workers are non-daemon AND are registered in
``concurrent.futures.thread._threads_queues``, whose atexit hook
(``_python_exit``) joins every worker unconditionally — even after
``shutdown(wait=False)``.  A single wedged worker (tool blocked on network
I/O, hung provider daemon, stuck subagent) therefore blocks interpreter
exit forever.  This is the root cause of multi-minute CLI exits on long
sessions: every abandoned concurrent-tool batch leaves workers that the
exit hook insists on joining.

``DaemonThreadPoolExecutor`` spawns daemon workers and skips the
``_threads_queues`` registration, so:

  - ``_python_exit`` never joins them, and
  - the interpreter's non-daemon thread join at shutdown skips them.

Semantics are otherwise identical (initializer/initargs, work queue,
idle-thread reuse).  Use it for any pool whose work is best-effort or
independently interruptible and must never hold the process open:
concurrent tool execution, background memory sync, catalog fan-out,
subagent timeout wrappers.  Do NOT use it for work that must complete
before exit (durable writes) — those belong on foreground threads with
explicit bounded joins.
    )annotationsN)ThreadPoolExecutor)_workerDaemonThreadPoolExecutorc                      e Zd ZdZddZdS )r   zCThreadPoolExecutor variant whose workers do not block process exit.returnNonec                   | j                             d          rd S | j        fd}t          | j                  }|| j        k     rd| j        p| |fz  }t          j        |t          t          j        | |          | j        | j        | j        fd          }|                                 | j                            |           d S d S )Nr   )timeoutc                0    |                     d            d S )N)put)_qs     7/home/rurouni/.hermes/hermes-agent/tools/daemon_pool.py
weakref_cbzADaemonThreadPoolExecutor._adjust_thread_count.<locals>.weakref_cb.   s    EE$KKKKK    z%s_%dT)nametargetargsdaemon)_idle_semaphoreacquire_work_queuelen_threads_max_workers_thread_name_prefix	threadingThreadr   weakrefref_initializer	_initargsstartadd)selfr   num_threadsthread_namets        r   _adjust_thread_countz-DaemonThreadPoolExecutor._adjust_thread_count(   s     '''22 	F , 	 	 	 	 $-((***!T%=%E{$SSK  Kj11$%N	 
 
 
A GGIIIMa      +*r   N)r   r	   )__name__
__module____qualname____doc__r*    r   r   r   r   %   s.        MM! ! ! ! ! !r   )r.   
__future__r   r   r    concurrent.futuresr   concurrent.futures.threadr   __all__r   r/   r   r   <module>r4      s    4 # " " " " "      1 1 1 1 1 1 - - - - - -%
&! ! ! ! !1 ! ! ! ! !r   