+
    j1                         R t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIHtH	t	H
t
 ]! 4       tRR/tR tR tR tR	 t ! R
 R4      tR# )a  
Control Socket Server

Runs in the arbiter process and accepts commands via Unix socket.
Uses asyncio in a background thread to handle client connections.

Fork Safety:
    This server uses os.register_at_fork() to properly handle fork() calls.
    Before fork: the asyncio thread is stopped to prevent lock issues.
    After fork in parent: the server is restarted.
    After fork in child: references are cleared (workers don't need the control server).
N)CommandHandlers)ControlProtocolmake_responsemake_error_responsefork_handlers_registeredFc                     \         R,          '       d   R# R\         R&   \        P                  ! \        \        \
        R7       R# )z,Register fork handlers once at module level.r   NT)beforeafter_in_parentafter_in_child)_module_stateosregister_at_fork_before_fork_after_fork_parent_after_fork_child     H/var/www/idalgo/venv/lib/python3.14/site-packages/gunicorn/ctl/server.py_register_fork_handlersr   %   s3    /0004M,-*(r   c                 R    \        \        4       F  p V P                  4        K  	  R# )z7Called before fork() - stop all active control servers.N)list_active_servers_stop_for_forkservers    r   r   r   2   s    ' (r   c                 R    \        \        4       F  p V P                  4        K  	  R# )z<Called in parent after fork() - restart all control servers.N)r   r   _restart_after_forkr   s    r   r   r   8   s    '""$ (r   c                 .    \         P                  4        R# )z2Called in child after fork() - cleanup references.N)r   clearr   r   r   r   r   >   s     r   c                      a  ] tR t^Et o RtRR ltR tR tR tR t	R t
R tR	 tR
 tV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )ControlSocketServera/  
Control socket server running in arbiter process.

The server runs an asyncio event loop in a background thread,
accepting connections and dispatching commands to handlers.

Fork safety is handled via os.register_at_fork() - the server
automatically stops before fork and restarts after in the parent.
c                    Wn         W n        W0n        \        V4      V n        RV n        RV n        RV n        RV n        RV n	        \        4        R# )z
Initialize control socket server.

Args:
    arbiter: The Gunicorn arbiter instance
    socket_path: Path for the Unix socket
    socket_mode: Permission mode for socket (default 0o600)
NF)arbitersocket_pathsocket_moder   handlers_server_loop_thread_running_was_running_before_forkr   )selfr"   r#   r$   s   &&&&r   __init__ControlSocketServer.__init__P   sM     &&'0
(-% 	 !r   c                    V P                   '       d   R# RV n         \        P                  ! V P                  RR7      V n        V P                  P                  4        \        P                  V 4       R# )z:Start server in background thread with asyncio event loop.NTtargetdaemon)r)   	threadingThread	_run_loopr(   startr   addr+   s   &r   r5   ControlSocketServer.startg   sM    === ''t~~dK 	D!r   c                *   \         P                  V 4       V P                  '       g   R# RV n        V P                  '       d8   V P                  '       d&   V P                  P                  V P                  4       V P                  '       d$   V P                  P                  RR7       RV n        \        P                  P                  V P                  4      '       d$    \        P                  ! V P                  4       R# R#   \         d     R# i ; i)zStop server and cleanup socket.NF       @timeout)r   discardr)   r'   r&   call_soon_threadsafe	_shutdownr(   joinr   pathexistsr#   unlinkOSErrorr7   s   &r   stopControlSocketServer.stops   s     	%}}}:::$,,,JJ++DNN;<<<LLc*DL 77>>$**++		$**+ ,  s    D DDc                   V P                   '       g
   RV n        R# RV n        RV n         V P                  '       d9   V P                  '       d'    V P                  P	                  V P
                  4       V P                  '       d$   V P                  P                  RR7       RV n        RV n        RV n        R#   \         d     LRi ; i)z1Stop server before fork (called by fork handler).FNTr:   r;   )	r)   r*   r'   r&   r>   r?   RuntimeErrorr(   r@   r7   s   &r   r   "ControlSocketServer._stop_for_fork   s    }}},1D)(,%:::$,,,

//?
 <<<LLc*DL
   s   %B9 9CCc                    V P                   '       g   R# RV n         RV n        \        P                  ! V P                  RR7      V n        V P
                  P                  4        R# )z=Restart server in parent after fork (called by fork handler).NFTr/   )r*   r)   r2   r3   r4   r(   r5   r7   s   &r   r   'ControlSocketServer._restart_after_fork   sH    ,,,(-% ''t~~dKr   c                b    V P                   '       d   V P                   P                  4        R# R# )z0Shutdown server (called from event loop thread).N)r&   closer7   s   &r   r?   ControlSocketServer._shutdown   s     <<<LL  r   c                B    \         P                  ! V P                  4       4       R#   \         dl   pT P                  '       dO   T P
                  P                  '       d-   T P
                  P                  P                  RT4        Rp?R#  Rp?R#  Rp?R# Rp?ii ; i)z0Run the asyncio event loop in background thread.zControl server error: %sN)asynciorun_serve	Exceptionr)   r"   logerror)r+   es   & r   r4   ControlSocketServer._run_loop   si    	FKK& 	F}}}!1!1!1  &&'A1EE "2}	Fs   $( B-B!&BBc                  "   \         P                  ! 4       V n        \        P                  P                  V P                  4      pV'       d>   \        P                  P                  V4      '       g   \        P                  ! VRR7       \        P                  P                  V P                  4      '       d!   \        P                  ! V P                  4       \         P                  ! V P                  V P                  R7      G Rj  xL
 V n        \        P                  ! V P                  V P                  4       V P                  P                   '       d1   V P                  P                   P#                  RV P                  4        V P                  ;_uu_4       GRj  xL
  V P                  P%                  4       G Rj  xL
  RRR4      GRj  xL
  \        P                  P                  T P                  4      '       d$    \        P                  ! T P                  4       R# R#  EL% L Lm L_  + GRj  xL 
 '       g   i     Lv; i  \         P&                   d     Li ; i  \(         d     R# i ; i  \        P                  P                  T P                  4      '       d4    \        P                  ! T P                  4       i   \(         d     i i ; ii ; i5i)zMain async server loop.i  )mode)rA   NzControl socket listening at %s)rP   get_running_loopr'   r   rA   dirnamer#   rB   makedirsrC   start_unix_server_handle_clientr&   chmodr$   r"   rT   infoserve_foreverCancelledErrorrD   )r+   
socket_dirs   & r   rR   ControlSocketServer._serve   s    --/
 WW__T%5%56
bggnnZ88KK
/ 77>>$**++IId&&' %66!!
 
 	!!4#3#34<<LL!!"B"&"2"24
	||||ll00222 $|
 ww~~d..//IId../ 0%
 $2 $|||%% 		   ww~~d..//IId../  0s  A0K'3AK':AK'	H.
AK'0K'I #H1$I 'H7H3H7
I H5I /K'
 I. *K'1I 3H75I 7I	=I >
I		I	I J  I I+(J  *I++J  .I=9K'<I==K' 0K$1 KK$K K$K  K$$K'c                L  "    V P                   '       do    \        P                  ! \        P                  ! V4      RR7      G Rj  xL
 pT P                  T4      G Rj  xL
 p\        P                  ! Y$4      G Rj  xL
  K   VP                  4         VP                  4       G Rj  xL
  R#  Li  \        P
                   d     KE  \        P                   d     K\  \         d     Ki  i ; i L Lv  \         dM   pT P                  P                  '       d'   T P                  P                  P                  RT4        Rp?LRp?ii ; i L  \         d     R# i ; i  TP                  4         TP                  4       G Rj  xL 
  i   \         d     i i ; i; i5i)zd
Handle client connection.

Args:
    reader: asyncio StreamReader
    writer: asyncio StreamWriter
g     r@r;   NzControl client error: %s)r)   rP   wait_forr   read_message_asyncTimeoutErrorIncompleteReadErrorrS   	_dispatchwrite_message_asyncr"   rT   debugrM   wait_closed)r+   readerwritermessageresponserV   s   &&&   r   r^   "ControlSocketServer._handle_client   s\    	---$+$4$4'::6B %% G "&!88 &99&KKK)  4 LLN((***5 ++ 22   
 9 L 	F||  &&'A1E	F +  LLN((*** s0  F$C6 /B2 B0B2 C6 C2 C6 =C4>C6 E$ F$E )E*E .F$0B2 2C/C6 E$ 
C/C/C6 E$ !C/*C/+C6 ,E$ .C//C6 4C6 6EAEE$ EE$ E E!F$ E!!F$$F!6F	F
FF!FF!FF!!F$c                &   < V ^8  d   QhRS[ RS[ /# )   rp   return)dict)format__classdict__s   "r   __annotate__ ControlSocketServer.__annotate__	  s     #K #Kt #K #Kr   c                  "   VP                  R^ 4      pVP                  RR4      P                  4       pVP                  R. 4      pV'       g   \        VR4      #  \        P                  ! V4      pV'       d   VP                  R V 4       4       V'       g   \        VR4      # V P                  V4      p\        W&4      #   \         d   p\        TRT 24      u Rp?# Rp?i\         d[   pT P                  P                  '       d&   T P                  P                  P                  R	4       \        TR
T 24      u Rp?# Rp?ii ; i5i)zu
Dispatch command to appropriate handler.

Args:
    message: Request message dict

Returns:
    Response dictionary
idcommand argsEmpty commandc              3   8   "   T F  p\        V4      x  K  	  R # 5i)N)str).0as   & r   	<genexpr>0ControlSocketServer._dispatch.<locals>.<genexpr>  s     2TSVVTs   zInvalid argument: NzCommand errorzCommand failed: )getstripr   shlexsplitextend_execute_commandr   
ValueErrorrS   r"   rT   	exception)r+   rp   
request_idr}   r   partsresultrV   s   &&      r   rj   ControlSocketServer._dispatch	  s     [[q)
++i,224{{62&&z?CC	KKK(E2T22*:GG **51F 44 	M&z5Gs3KLL 	K||  **?;&z5EaS3IJJ	Ksm   AEC :C C %E&C EE	C!E	E!E	.E	/AE>E	?EE		Ec                &   < V ^8  d   QhRS[ RS[/# )rt   r   ru   r   rv   )rw   rx   s   "r   ry   rz   .  s     !8 !8d !8t !8r   c                D   V'       g   \        R4      hV^ ,          P                  4       pVR,          pVR8X  d   V P                  V4      # VR8X  d   V P                  V4      # VR8X  d   V P	                  V4      # VR8X  d   V P
                  P                  4       # VR8X  d   V P
                  P                  4       # VR8X  d/   V'       d
   V^ ,          MR	pV P
                  P                  V4      # VR
8X  d   V P
                  P                  4       # \        RV 24      h)z
Execute a parsed command.

Args:
    parts: Command parts (e.g., ["show", "workers"])

Returns:
    Handler result dictionary
r      NNshowworkerdirtyreloadreopenshutdowngracefulhelpzUnknown command: )
r   lower_handle_show_handle_worker_handle_dirtyr%   r   r   r   r   )r+   r   cmdrestrY   s   &&   r   r   $ControlSocketServer._execute_command.  s     _--AhnnRy &=$$T**H_&&t,,G^%%d++H_==''))H_==''))J"47
D==))$//F]==%%''0677r   c                &   < V ^8  d   QhRS[ RS[/# rt   r   ru   r   )rw   rx   s   "r   ry   rz   Q  s     ? ? ?$ ?r   c                   V'       g   \        R4      hV^ ,          P                  4       pVR8X  d   V P                  P                  4       # VR8X  d   V P                  P	                  4       # VR8X  d   V P                  P                  4       # VR8X  d   V P                  P                  4       # VR8X  d   V P                  P                  4       # VR8X  d   V P                  P                  4       # \        RV 24      h)	zHandle 'show' commands.z>Missing show target (all|workers|dirty|config|stats|listeners)allworkersr   configstats	listenerszUnknown show target: )	r   r   r%   show_allshow_workers
show_dirtyshow_config
show_statsshow_listeners)r+   r   r0   s   && r   r    ControlSocketServer._handle_showQ  s    ]^^aU?==))++y ==--//w==++--x==,,..w==++--{"==//114VH=>>r   c                &   < V ^8  d   QhRS[ RS[/# r   r   )rw   rx   s   "r   ry   rz   g  s     A A4 AD Ar   c                
   V'       g   \        R4      hV^ ,          P                  4       pVR,          pVR8X  d8   V'       d   \        V^ ,          4      M^pV P                  P	                  V4      # VR8X  d8   V'       d   \        V^ ,          4      M^pV P                  P                  V4      # VR8X  dA   V'       g   \        R4      h\        V^ ,          4      pV P                  P                  V4      # \        RV 24      h)zHandle 'worker' commands.z'Missing worker action (add|remove|kill)r   r6   removekillzMissing PID for worker killzUnknown worker action: )r   r   intr%   
worker_addworker_removeworker_kill)r+   r   actionaction_argscountpids   &&    r   r   "ControlSocketServer._handle_workerg  s    FGGa2hU?+6CA'AE==++E22x+6CA'AE==..u55v !>??k!n%C==,,S116vh?@@r   c                &   < V ^8  d   QhRS[ RS[/# r   r   )rw   rx   s   "r   ry   rz   }  s     @ @$ @4 @r   c                |   V'       g   \        R4      hV^ ,          P                  4       pVR,          pVR8X  d8   V'       d   \        V^ ,          4      M^pV P                  P	                  V4      # VR8X  d8   V'       d   \        V^ ,          4      M^pV P                  P                  V4      # \        RV 24      h)zHandle 'dirty' commands.z!Missing dirty action (add|remove)r   r6   r   zUnknown dirty action: )r   r   r   r%   	dirty_adddirty_remove)r+   r   r   r   r   s   &&   r   r   !ControlSocketServer._handle_dirty}  s    @AAa2hU?+6CA'AE==**511x+6CA'AE==--e445fX>??r   )	r'   r)   r&   r(   r*   r"   r%   r$   r#   N)i  )__name__
__module____qualname____firstlineno____doc__r,   r5   rE   r   r   r?   r4   rR   r^   rj   r   r   r   r   __static_attributes____classdictcell__)rx   s   @r   r    r    E   st     ".
"2.!
F$L'R#K #KJ!8 !8F? ?,A A,@ @r   r    )r   rP   r   r   r2   gunicorn.ctl.handlersr   gunicorn.ctl.protocolr   r   r   setr   r   r   r   r   r   r    r   r   r   <module>r      sZ   
  	   1  %+U3
 %G@ G@r   