
     i                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
Jr  S SKJr  S SKJrJrJr  S SKJrJr   " S S5      rg)    N)
HaltServerAppImportError)Pidfile)socksystemdutil)__version__SERVER_SOFTWAREc                      \ rS rSrSrSrSr0 r/ r0 r	\
R                  rSR                  5        V VVVs/ s H  n[        [        SU-  5      PM     snnnn r\" S \" \
5       5       5      rS rS	 rS
 r\" \\5      rS rS rS rS r
S rS rS r\rS rS r S r!S r"S r#S r$S r%S r&S r'S r(S r)S4S jr*S5S jr+S6S  jr,S! r-S" r.S# r/S$ r0S% r1S& r2S' r3S( r4S) r5S* r6S+ r7S, r8S- r9S. r:S/ r;S0 r<S1 r=S2 r>S3r?gs  snnnn f )7Arbiter   z
Arbiter maintain the workers processes alive. It launches or
kills them if needed. It also manages application reloading
via SIGHUP/USR2.
      z+HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCHzSIG%sc              #      #    U  H>  nUS S S:X  d  M  US   S:w  d  M  [        [        U5      USS  R                  5       4v   M@     g 7f)Nr   SIG_)getattrsignallower).0names     B/var/www/ias/venv/lib/python3.13/site-packages/gunicorn/arbiter.py	<genexpr>Arbiter.<genexpr>0   sO      >Id8u 	2!%aC 	2	QR 01ks   AA*Ac                    [         [        R                  S'   S U l        S U l        S U l        [        R                  " 5       U l        U R                  U5        S U l
        SU l        SU l        SU l        SU l        SU l        SU l        S U l        S U l        S U l        S SSSS.U l        [*        R,                  " 5       n[.        R0                  S S  nUR3                  S[.        R4                  5        UU[.        R4                  S.U l        g )Nr
   Fr   Master)
start_timeworkers_spawnedworkers_killedreloads)argscwdr   )r
   osenviron_num_workers _last_logged_active_worker_countlogqueueSimpleQueue	SIG_QUEUEsetuppidfiler   
worker_age
reexec_pid
master_pidmaster_namedirty_arbiter_piddirty_arbiterdirty_pidfile_control_server_statsr   getcwdsysargvinsert
executable	START_CTX)selfappr"   r!   s       r   __init__Arbiter.__init__5   s    (7

$% 04- **,

3# "#!!  $  	
 kkmxx{As~~& ~~
    c                     U R                   $ N)r%   r<   s    r   _get_num_workersArbiter._get_num_workersd   s       r@   c                 `    U R                   nXl         U R                  R                  XU5        g rB   )r%   cfgnworkers_changed)r<   value	old_values      r   _set_num_workersArbiter._set_num_workersg   s'    %%	!!!$y9r@   c                    Xl         UR                  U l        U R                  c*  U R                  R                  UR                  5      U l        S[        R
                  ;   a  U R                  R                  5         U R                  R                  U l        U R                  R                  U l        U R                  R                  U l
        U R                  R                  U l        U R                  R                  U l        U R                  R                  SR                  SR                  S [!        U R                  R"                  R%                  5       S S9 5       5      5      5        U R                  R&                  (       a@  U R                  R&                  R%                  5        H  u  p#U[        R
                  U'   M     U R                  R(                  (       a  U R                   R+                  5         g g )NGUNICORN_PIDzCurrent configuration:
{0}
c              3   \   #    U  H"  u  pS R                  XR                  5      v   M$     g7f)z
  {0}: {1}N)formatrI   )r   configrI   s      r   r    Arbiter.setup.<locals>.<genexpr>   s2      ;: "F ##FKK88:s   *,c                     U S   $ N    )settings    r   <lambda>Arbiter.setup.<locals>.<lambda>   s    gajr@   key)r=   rG   r'   logger_classr#   r$   reopen_filesworker_classaddressworkersnum_workerstimeout	proc_namedebugrQ   joinsortedsettingsitemsenvpreload_appwsgi)r<   r=   kvs       r   r+   Arbiter.setupm   sY   7788xx,,SWW5DH RZZ'HH!!# HH11xx''88++xx''++4;;II ; $((++1138:; ;< 	= 88<<**, !

1 - 88HHMMO  r@   c                    U R                   R                  S[        5        [        R                  " 5       U R                  S'   S[
        R                  ;   aH  [        [
        R                  R                  S5      5      U l	        U R                  S-   U l
        SU l        [
        R                  " 5       U l        U R                  R                  b`  U R                  R                  nU R                  S:w  a  US-  n[!        U5      U l        U R                  R#                  U R                  5        U R                  R%                  U 5        U R'                  5         U R(                  (       Gd  Sn[*        R,                  " 5       nU(       a3  SU l        [/        [*        R0                  [*        R0                  U-   5      nObU R                  (       aQ  / n[
        R                  R3                  S	5      R5                  S
5       H  nUR7                  [        U5      5        M     U R                  R8                  (       a  [;        [<        S5      (       d1  [>        R@                  " U R                  U R                   U5      U l        S
RC                  U R(                   Vs/ s H  n[E        U5      PM     sn5      nU R                   RG                  S5        U R                   R                  SX`R                  5        U R                   R                  SU R                  RH                  5        [*        RJ                  " SU R                   5        [;        U RL                  S5      (       a0  U RL                  RO                  U R                  U R                   5        U R                  RP                  S:  a+  U R                  RR                  (       a  U RU                  5         U R                  RW                  U 5        gs  snf )zKInitialize the arbiter. Start listening and set pidfile if needed.
        zStarting gunicorn %sr   rN   z.2zMaster.2Nr   TGUNICORN_FD,SO_REUSEPORTzArbiter bootedzListening at: %s (%s)zUsing worker: %sz&READY=1
STATUS=Gunicorn arbiter bootedcheck_config),r'   infor	   timer5   r#   r$   intgetr/   rd   r0   getpidpidrG   r,   r   createon_startinginit_signals	LISTENERSr   
listen_fdsrangeSD_LISTEN_FDS_STARTpopsplitappend
reuse_porthasattrsocketr   create_socketsrf   strre   worker_class_str	sd_notifyr_   rt   dirty_workers
dirty_appsspawn_dirty_arbiter
when_ready)r<   pidnamefdsr   fdlnrlisteners_strs          r   startArbiter.start   s    	,k: %)IIKL!RZZ'!"**.."@ADO!^^d2DN)D99;88'hh&&G!#4"7+DLLL)T"~~~C ++-J#G77#77*DF **..7==cBBJJs2w' C HH''GFN,K,K!%!4!4TXXtxx!Mdnn!Ens#c(n!EF'(-}hhG($((*C*CDCTXXN 4$$n55**488TXX> 88!!A%$((*=*=$$&
 	D!# "Fs   O4c                     U R                   R                  5         U R                   H#  n[        R                  " XR                  5        M%     [        R                  " [        R                  U R
                  5        g)zjInitialize master signal handling. Most of the signals
are queued. Child signals only wake up the master.
N)r'   close_on_execSIGNALSr   SIGCHLDsignal_chld)r<   ss     r   r}   Arbiter.init_signals   sL    
 	  AMM![[) fnnd&6&67r@   c                 :    U R                   R                  U5        g)z3Signal handler - NO LOGGING, just queue the signal.Nr*   
put_nowaitr<   sigframes      r   r   Arbiter.signal       !!#&r@   c                    U R                  5         [        R                  " SU R                  -  5         U R	                  5         U R                  5          U R                  5         U R                  SS9 H  nXR                  ;  a  U R                  R                  SU5        M0  U R                  R                  U5      n[        U SU-  S5      nU(       d  U R                  R                  SU5        M  U[        R                  :X  a  U R                  R                   OU R                  R                  nU" S	U5        U" 5         M     U R#                  5         U R	                  5         U R%                  5         GM&  ! [&        [(        4 a    U R+                  5          g[,         a.  nU R+                  UR.                  UR0                  S
9   SnAgSnAf[2         a    e [4         ak    U R                  R                  SSS9  U R7                  S5        U R8                  b  U R8                  R;                  5         [<        R>                  " S5         gf = f)zMain master loop.master [%s]T      ?)rc   zIgnoring unknown signal: %sz	handle_%sNzUnhandled signal: %szHandling signal: %s)reasonexit_statusz Unhandled exception in main loopexc_infoF) r   r   _setproctitlerd   manage_workers_start_control_servermaybe_promote_masterwait_for_signals	SIG_NAMESr'   ru   rx   r   errorr   r   re   murder_workersmanage_dirty_arbiterStopIterationKeyboardInterrupthaltr   r   r   
SystemExit	Exceptionstopr,   unlinkr7   exit)r<   r   signamehandler	log_levelinsts         r   runArbiter.run   s   

=4>>9:)	! &&())+  000=C..0&CSI "nn005G%dK',A4HG"'=wG 252GTXX]]I3W=I > ##%##%))++ , 01 	IIK 	HIIT[[d6F6FIG 	 	HHNN=$(  *IIe||'##%HHRL	s%   EE< < I	I'$GA?IIc                 :    U R                   R                  U5        g)z;SIGCHLD signal handler - NO LOGGING, just queue the signal.Nr   r   s      r   r   Arbiter.signal_chld	  r   r@   c                 D    U R                  5         U R                  5         g)z6SIGCHLD handling - called from main loop, safe to log.N)reap_workersreap_dirty_arbiterrC   s    r   handle_chldArbiter.handle_chld  s    !r@   c                     U R                   R                  SU R                  5        U R                  5         U R                  (       a   U R                  [        R                  5        gg)zHUP handling.
- Reload configuration
- Start the new worker processes with a new configuration
- Gracefully shutdown the old worker processes
zHang up: %sN)r'   ru   r0   reloadr1   kill_dirty_arbiterr   SIGHUPrC   s    r   
handle_hupArbiter.handle_hup  sE     	mT%5%56!!##FMM2 "r@   c                     [         e)zSIGTERM handling)r   rC   s    r   handle_termArbiter.handle_term#  s    r@   c                 0    U R                  S5        [        e)zSIGINT handlingFr   r   rC   s    r   
handle_intArbiter.handle_int'      		%r@   c                 0    U R                  S5        [        e)zSIGQUIT handlingFr   rC   s    r   handle_quitArbiter.handle_quit,  r   r@   c                 N    U =R                   S-  sl         U R                  5         g)z:SIGTTIN handling.
Increases the number of workers by one.
rV   Nrb   r   rC   s    r   handle_ttinArbiter.handle_ttin1  s!    
 	Ar@   c                 p    U R                   S::  a  gU =R                   S-  sl         U R                  5         g)z:SIGTTOU handling.
Decreases the number of workers by one.
rV   Nr   rC   s    r   handle_ttouArbiter.handle_ttou9  s2    
 q Ar@   c                     U R                   R                  5         U R                  [        R                  5        U R
                  (       a   U R                  [        R                  5        gg)z=SIGUSR1 handling.
Kill all workers by sending them a SIGUSR1
N)r'   r^   kill_workersr   SIGUSR1r1   r   rC   s    r   handle_usr1Arbiter.handle_usr1C  sF    
 	&..)!!##FNN3 "r@   c                 $    U R                  5         g)zSIGUSR2 handling.
Creates a new arbiter/worker set as a fork of the current
arbiter without affecting old workers. Use this to do live
deployment with the ability to backout a change.
N)reexecrC   s    r   handle_usr2Arbiter.handle_usr2N  s     	r@   c                     U R                   R                  (       aB  U R                  R                  S5        SU l        U R                  [        R                  5        gU R                  R                  S5        g)zSIGWINCH handlingzgraceful stop of workersr   z SIGWINCH ignored. Not daemonizedN)	rG   daemonr'   ru   rb   r   r   SIGTERMre   rC   s    r   handle_winchArbiter.handle_winchW  sI    88??HHMM45 Dfnn-HHNN=>r@   c                    U R                   S:X  a  g U R                   [        R                  " 5       :w  a  U R                  R	                  S5        SU l        SU l         U R                  R                  U l        [        R                  S	 U R                  b/  U R                  R                  U R                  R                  5        [        R                  " SU R                  -  5        g g )Nr   zMaster has been promoted.r   rN   r   )r/   r#   getppidr'   ru   r0   rG   rd   r$   r,   renamer   r   rC   s    r   r   Arbiter.maybe_promote_master`  s    ??a??bjjl*HHMM56'DDO!XX//DN

>*||'##DHH$4$45}t~~=> +r@   c                 N    U R                   R                  U R                  5        g)z Wake up the arbiter's main loop.N)r*   r   WAKEUP_REQUESTrC   s    r   wakeupArbiter.wakeupq  s    !!$"5"56r@   Nc                    U R                  5         U R                  5         US:X  a  U R                  R                  OU R                  R                  nU" SU R
                  5        Ub	  U" SU5        U R                  b  U R                  R                  5         U R                  R                  U 5        [        R                  " U5        g)zhalt arbiter r   zShutting down: %sNz
Reason: %s)_stop_control_serverr   r'   ru   r   r0   r,   r   rG   on_exitr7   r   )r<   r   r   log_funcs       r   r   Arbiter.haltu  s     	!!#		$/1$4488==$((..$d&6&67\6*<<#LL!r@   c                    / n U R                   R                  SUS9nX0R                  :w  a  UR                  U5          U R                   R	                  5       nX0R                  :w  a  UR                  U5        M=  ! [
        R                   a     U$ f = f! [
        R                   a     U$ [         a    [        R                  " 5          U$ f = f)zMWait for signals with timeout.
Returns a list of signals that were received.
T)blockrc   )
r*   rx   r   r   
get_nowaitr(   Emptyr   r7   r   )r<   rc   signalsr   s       r   r   Arbiter.wait_for_signals  s    
 	..$$4$AC)))s#..335C111s+	 
 {{ 
 {{ 	  ! 	HHJ	s;   ;B  :A< :B <BB BB C.CCc                 (   U R                   U R                  s=:H  =(       a    S:H  Os  =(       a3    U R                  (       + =(       a    U R                  R                  (       + n[
        R                  " U R                  U5        / U l        [        R                  nU(       d  [        R                  n[        R                  " 5       U R                  R                  -   nU R                  (       a  U R                  U5        U R                  U5        U(       + nU R                   (       d  U R                  (       GaX  [        R                  " 5       U:  Ga>  U(       d   U R"                  R%                  5       nU[        R&                  [        R                  4;   a  U R(                  R+                  S5        SnU R                  [        R                  5        U R                  (       a  U R                  [        R                  5        [        R                  " 5       S-   nU R/                  5         U R1                  5         [        R2                  " S5        U R                   (       d  U R                  (       a  [        R                  " 5       U:  a  GM>  U R                  [        R4                  5        U R                  (       a  U R                  [        R4                  5        U R/                  5         U R1                  5         g! [,         a     Nf = f)zStop workers

:attr graceful: boolean, If True (the default) workers will be
killed gracefully  (ie. trying to wait for the current connection)
r   zQuick shutdown requestedT       @皙?N)r.   r/   r   rG   r   r   close_socketsr~   r   r   SIGQUITrv   graceful_timeoutr1   r   r   WORKERSr*   r  SIGINTr'   ru   r   r   r   sleepSIGKILL)r<   gracefulr   r   limitquick_shutdownpending_sigs          r   r   Arbiter.stop  s     OOt33!3 (LL (HH''' 	
 	4>>62nn..C		dhh777 !!##C( 	#%||t555499;;N!"&..";";"=K"v}}fnn&EE&@A)-))&..911 33FNNC $		c 1 ##%JJsO# ||t55499;;N& 	&..)!!##FNN3! ! s   CL 
LLc                 j   U R                   S:w  a  U R                  R                  S5        gU R                  S:w  a  U R                  R                  S5        g[        R
                  " 5       n[        R                  " 5       U l         U R                   S:w  a  gU R                  R                  U 5        U R                  R                  R                  5       n[        U5      US'   U R                  (       aC  [        [        R
                  " 5       5      US'   [        [        U R                  5      5      US'   O%SR                  S	 U R                   5       5      US
'   [        R                   " U R"                  S   5        [        R$                  " U R"                  S   U R"                  S   U5        g)z)Relaunch the master and workers.
        r   z"USR2 signal ignored. Child exists.Nz#USR2 signal ignored. Parent exists.rN   
LISTEN_PID
LISTEN_FDSrr   c              3   T   #    U  H  n[        UR                  5       5      v   M      g 7frB   )r   fileno)r   r   s     r   r   !Arbiter.reexec.<locals>.<genexpr>  s!      .=-;cCJJL!!^s   &(rq   r"   r!   )r.   r'   warningr/   r#   ry   forkrG   pre_execenv_origcopyr   r   lenr~   rf   chdirr;   execvpe)r<   r/   r$   s      r   r   Arbiter.reexec  s:    ??aHHAB??aHHBCYY[
'')??a$((##((*"%j/<<$'		$4GL!$'DNN(;$<GL!%(XX .=-1^^.= &=GM" 	&' 	

4>>!$dnnV&<gFr@   c                 L   U R                   S==   S-  ss'   U R                  R                  nU R                  R                   HZ  nX R                  R                  ;   a,  U R                  R                  U   [
        R                  U'   MH   [
        R                  U	 M\     U R                  R                  5         U R                  U R                  5        U R                  R                  5         XR                  R                  :w  a  U R                   H  nUR                  5         M     [        R                   " U R                  U R                  5      U l        SR#                  U R                   Vs/ s H  n[%        U5      PM     sn5      nU R                  R'                  SU5        U R                  R)                  U 5        U R*                  b  U R*                  R-                  5         U R                  R*                  bI  [/        U R                  R*                  5      U l        U R*                  R1                  U R2                  5        [4        R6                  " SU R8                  -  5        U R:                  n[=        U R                  R>                  5       H  nU RA                  5         M     U RC                  5         [D        RF                  " 5       U R                  RH                  -   n[D        RF                  " 5       U:  a  U RJ                  (       d  g [M        S U RJ                  RO                  5        5       5      nX:  a  g U RQ                  5         [D        RR                  " S5        [D        RF                  " 5       U:  a  M  g g ! [         a     GMY  f = fs  snf )Nr    rV   rr   zListening at: %sr   c              3   8   #    U  H  oR                   v   M     g 7frB   age)r   ws     r   r   !Arbiter.reload.<locals>.<genexpr>:  s     >(=1(=s   r  )*r5   rG   r`   rj   r  r#   r$   KeyErrorr=   r   r+   r'   r^   r~   closer   r   rf   r   ru   	on_reloadr,   r   r   r{   rz   r   r   rd   r-   r   ra   spawn_workerr   rv   	monotonicr
  r  minvaluesr   r  )	r<   old_addressrm   r   r   last_worker_ager   deadlineoldests	            r   r   Arbiter.reload  s   I!#hh&& AHH%%% !% 1 1! 4

1

1  	

488 	 ((***~~		 & "00488DDNHH$..%I.3c#h.%IJMHHMM,m< 	4  <<#LL! 88'"488#3#34DLLL) 	=4>>9: // txx''(A ) 	 >>#dhh&?&??nn)<<>(;(;(=>>F'JJsO nn)_   " &Js   N6N!
NNc                 "   U R                   (       d  g[        U R                  R                  5       5      nU H  u  p# [        R
                  " 5       UR                  R                  5       -
  U R                   ::  a  MF   UR                  (       dE  U R                  R                  SU5        SUl
        U R                  U[        R                  5        M  U R                  U[        R                   5        M     g! [        [        4 a     M  f = f)z!Kill unused/idle workers
        NzWORKER TIMEOUT (pid:%s)T)rc   listr  ri   rv   r-  tmplast_updateOSError
ValueErrorabortedr'   criticalkill_workerr   SIGABRTr  )r<   ra   rz   workers       r   r   Arbiter.murder_workers@  s     ||t||))+,$MS>>#fjj&<&<&>>$,,N O
 >>!!";SA!%  fnn5  fnn5 % Z( s   >C::DDc                      [         R                  " S[         R                  5      u  pU(       d  gU R                  U:X  a	  SU l        GOSn[         R                  " U5      (       a  [         R
                  " U5      nO[         R                  " U5      (       a  [         R                  " U5      n [        R                  " U5      R                  nSR                  X5      nU[        R                  :X  a!  US-  nU R                  R                  U5        OKU[        R                   :X  a  U R                  R#                  U5        OU R                  R%                  U5        Ub"  US:w  a  U R                  R                  SX5        X0R&                  :X  a  Sn[)        XpR&                  5      eX0R*                  :X  a  S	n[)        XpR*                  5      eU R,                  R/                  US5      nU(       d  GM  UR0                  R3                  5         U R4                  R7                  X5        GM.  ! [         a    SR                  U5      n GN~f = f! [8         a)  n	U	R:                  [:        R<                  :w  a  e  Sn	A	gSn	A	ff = f)
z/Reap workers to avoid zombie processes
        r   r   Nz	signal {}zWorker (pid:{}) was sent {}!z Perhaps out of memory?z$Worker (pid:%s) exited with code %s.zWorker failed to boot.zApp failed to load.)r#   waitpidWNOHANGr.   	WIFEXITEDWEXITSTATUSWIFSIGNALEDWTERMSIGr   Signalsr   r:  rQ   r  r'   r   r   ru   r  WORKER_BOOT_ERRORr   APP_LOAD_ERRORr  r   r7  r*  rG   
child_exitr9  errnoECHILD)
r<   wpidstatusexitcoder   sig_namemsgr   r?  es
             r   r   Arbiter.reap_workersU  s   4	!zz"bjj9??d*&'DO
  $H||F++#%>>&#9// kk&1?'-~~c':'?'?H =CC , &..0#<<C HHNN3/ FNN2 HHMM#. !HH,,S1+A'M'+7  #9#99!9(1G1GHH#6#66!6(1D1DEE!\\--dD9F! JJ$$&HH''5a "  * ?'2'9'9#'>H?@  	ww%,,& '	sB   /I A>I 1 H1 E I 1II II 
JJJc                    [        U R                  5      U R                  :  a  U R                  5         U R                  R	                  5       n[        US S9n[        U5      U R                  :  aN  UR                  S5      u  p#U R                  U[        R                  5        [        U5      U R                  :  a  MN  [        U5      nU R                  U:w  a3  X@l
        U R                  R                  SR                  U5      SUSS.S9  U R                  R                  (       aQ  [!        S	 U R"                   5       5      nUS:  a.  U R                  R                  S
R                  U5      SUSS.S9  ggg)zCMaintain the number of workers by spawning or killing
as required.
c                      U S   R                   $ rU   r%  )r'  s    r   rY   (Arbiter.manage_workers.<locals>.<lambda>  s    !r@   r[   r   z{0} workerszgunicorn.workersgauge)metricrI   mtype)extrac              3   V   #    U  H  nUR                   " 5       =(       d    S v   M!     g7f)r   N)get_backlog)r   r   s     r   r   )Arbiter.manage_workers.<locals>.<genexpr>  s(      6&4d **,11&4s   ')zsocket backlog: {0}zgunicorn.backlog	histogramN)r  r  rb   spawn_workersri   rg   r   r=  r   r   r&   r'   re   rQ   rG   enable_backlog_metricsumr~   )r<   ra   rz   r   active_worker_countbacklogs         r   r   Arbiter.manage_workers  sO   
 t||t/// ,,$$&&89'lT---{{1~HSS&..1 'lT--- "'l004GG4G1HHNN=//0CD,>+>+2"4  5
 88)) 6&*nn6 6G !|4;;GD0B/6/:&<  = 	 *r@   c           	      	   U =R                   S-  sl         U R                  U R                   U R                  U R                  U R                  U R
                  S-  U R                  U R                  5      nU R                  R                  X5        [        R                  " 5       nUS:w  a-  X!l        XR                  U'   U R                  S==   S-  ss'   U$ U R                  R                  5        H  nUR                  R                  5         M     [        R                   " 5       Ul         ["        R$                  " SU R&                  -  5        U R                  R)                  SUR                  5        U R                  R*                  (       a0  [,        R.                  " U R                  U R                  5      Ul        U R                  R3                  X5        UR5                  5         [6        R8                  " S5        U R                  R)                  SUR                  5         UR                  R                  5         U R                  RQ                  X5        g ! [:         a    e [<         a}  nU R                  R?                  SSS	9  [A        S
U-  [6        RB                  S9  [6        RB                  RE                  5         [6        R8                  " U RF                  5         S nANS nAf[H         a  nU R                  RK                  S5        [A        S
U-  [6        RB                  S9  [6        RB                  RE                  5         URL                  (       d   [6        R8                  " U RN                  5        [6        R8                  " S5         S nAGNS nAff = f! [H         a2    U R                  RS                  S[T        RV                  " 5       5         g f = f! U R                  R)                  SUR                  5         UR                  R                  5         U R                  RQ                  X5        f ! [H         a2    U R                  RS                  S[T        RV                  " 5       5         f f = f= f)NrV   r  r   r   zworker [%s]zBooting worker with pid: %sz'Exception while loading the applicationTr   z%s)filezException in worker processr   zWorker exiting (pid: %s)z Exception during worker exit:
%s),r-   r_   rz   r~   r=   rc   rG   r'   pre_forkr#   r  r  r5   r/  r7  r*  ry   r   r   rd   ru   r   r   r   sockets	post_forkinit_processr7   r   r   r   re   printstderrflushrJ  r   	exceptionbootedrI  worker_exitr  	traceback
format_exc)r<   r?  rz   siblingrS  s        r   r,  Arbiter.spawn_worker  s   1""4??DHHdnn#'88T\\C-?#'88TXX7 	$'ggi!8J &LLKK)*a/*J ||**,GKK - YY[
	9}t~~=>HHMM7Dxx""!%!4!4TXXtxx!HHHt,!HHQK" HHMM4fjjA9

  "$$T2'  	 	*HHNND$(  *$(,JJHHT(()) 	HH<=$(,JJ==//0HHRLL	  9  !D!*!5!5!799	 HHMM4fjjA9

  "$$T2 9  !D!*!5!5!799su   /CI! +5N# !N 5A3K-(O" -N :BNO" N  O" #9OO"'Q?
5Q ?Q? 9Q<9Q?;Q<<Q?c                     [        U R                  [        U R                  5      -
  5       H?  nU R	                  5         [
        R                  " S[        R                  " 5       -  5        MA     g)ziSpawn new workers as needed.

This is where a worker process leaves the main loop
of the master process.
r  N)r   rb   r  r  r,  rv   r  random)r<   r   s     r   r`  Arbiter.spawn_workers  sJ     t''#dll*;;<AJJsV]]_,- =r@   c                 ~    [        U R                  R                  5       5      nU H  nU R                  X15        M     g)zFKill all workers with the signal `sig`
:attr sig: `signal.SIG*` value
N)r6  r  keysr=  )r<   r   worker_pidsrz   s       r   r   Arbiter.kill_workers  s3    
 4<<,,./CS& r@   c                     [         R                  " X5        U[        R                  [        R                  4;   a  U R
                  S==   S-  ss'   gg! [         a  nUR                  [        R                  :X  ap   U R                  R                  U5      nUR                  R                  5         U R                  R                  X5         SnAg! [        [        4 a      SnAgf = fe SnAff = f)zJKill a worker

:attr pid: int, worker pid
:attr sig: `signal.SIG*` value
 r   rV   N)r#   killr   r   r  r5   r9  rL  ESRCHr  r   r7  r*  rG   rq  r)  )r<   rz   r   rS  r?  s        r   r=  Arbiter.kill_worker  s    	GGCv~~v~~66,-2- 7 		ww%++%!\\--c2FJJ$$&HH((6 '* 		s7   AA 
C3C.>ACC*#C.)C**C..C3c                     SSK nU R                  R                  SS5      R                  SS5      n[        R                  R                  UR                  5       SU S35      $ )a	  Get the well-known PID file path for orphan detection.

Uses self.proc_name (not self.cfg.proc_name) so that during USR2
the new master gets a different PID file path ("myapp.2" vs "myapp").
This prevents the old dirty arbiter from removing the new one's PID file.
r   N/r    zgunicorn-dirty-z.pid)tempfilerd   replacer#   pathrf   
gettempdir)r<   r  	safe_names      r   _get_dirty_pidfile_pathArbiter._get_dirty_pidfile_path  sQ     	NN**34<<S#F	ww||H//1_YKt3TUUr@   c                 N   U R                   S:w  a  gU R                  5       n[        R                  R	                  U5      (       d  g [        U5       n[        UR                  5       R                  5       5      nSSS5        [        R                  " WS5        U R                  R                  SU5        [        R                  " U[        R                  5        [        S5       H1  n[        R                   " S5         [        R                  " US5        M3     [        R                  " U[        R$                  5          [        R*                  " U5        g! , (       d  f       N= f! ["         a       M7  f = f! [&        [(        ["        4 a     NRf = f! ["         a     gf = f)zuKill any orphaned dirty arbiter from a previous crash.

Only runs on fresh start (master_pid == 0), not during USR2.
r   Nz(Killing orphaned dirty arbiter (pid: %s)
   r  )r/   r  r#   r  existsopenrw   readstripr~  r'   r  r   r   r   rv   r  r9  r  r:  IOErrorr   )r<   r,   fold_pidr   s        r   _cleanup_orphaned_dirty_arbiter'Arbiter._cleanup_orphaned_dirty_arbiter  s1    ??a..0ww~~g&&	g!affhnn./  GGGQHHGQGGGV^^,2Y

3GGGQ'  0
	IIg-    GW- 		  		sa   E< (E;BE<  E*)E< F 
E'#E< *
E94E< 8E99E< <FF
F$#F$c                    SSK JnJn  U R                  (       a  gU R	                  5         U R                  5       U l        U" U R                  U R                  U R                  S9U l	        U R                  R                  n[        R                  " 5       nUS:w  a?  X@l        U" U5        U[        R                  S'   U R                  R                  SXC5        U$  U R                  R                  5         [         R"                  " S5        g! [$         a    e [&         a4    U R                  R)                  S5        [         R"                  " S5         gf = f)	z~Spawn the dirty arbiter process.

The dirty arbiter manages a separate pool of workers for
long-running, blocking operations.
r   )DirtyArbiterset_dirty_socket_pathN)r,   GUNICORN_DIRTY_SOCKETz%Spawned dirty arbiter (pid: %s) at %sz"Exception in dirty arbiter processr   )gunicorn.dirtyr  r  r1   r  r  r3   rG   r'   r2   socket_pathr#   r  r$   ru   r   r7   r   r   r   ro  )r<   r  r  r  rz   s        r   r   Arbiter.spawn_dirty_arbiterF  s    	G!! 	,,. "99;)HHdhh&&
 ((44ggi!8%("!+.2=BJJ./HHMMA,J	""$HHQK 	 	HHCDHHRL	s   0D AEEc                     U R                   (       d  g [        R                  " U R                   U5        g! [         a;  nUR                  [        R
                  :X  a  SU l         SU l         SnAg SnAgSnAff = f)zDSend a signal to the dirty arbiter.

:attr sig: `signal.SIG*` value
Nr   )r1   r#   r~  r9  rL  r  r2   )r<   r   rS  s      r   r   Arbiter.kill_dirty_arbitert  sa     %%	*GGD**C0 	*ww%++%)*&%)"" &	*s   !6 
A; ,A66A;c                    U R                   (       d  g [        R                  " U R                   [        R                  5      u  pU(       d  g[        R                  " U5      (       aV  [        R
                  " U5      nUS:w  a  U R                  R                  SX5        OjU R                  R                  SU5        OM[        R                  " U5      (       a2  [        R                  " U5      nU R                  R                  SX5        SU l         SU l        g! [         a;  nUR                  [        R                  :X  a  SU l         SU l         SnAg SnAgSnAff = f)z9Reap the dirty arbiter process if it has exited.
        Nr   z*Dirty arbiter (pid:%s) exited with code %szDirty arbiter (pid:%s) exitedz*Dirty arbiter (pid:%s) killed by signal %s)r1   r#   rB  rC  rD  rE  r'   r   ru   rF  rG  r  r2   r9  rL  rM  )r<   rN  rO  rP  r   rS  s         r   r   Arbiter.reap_dirty_arbiter  s    %%	*::d&<&<bjjILD||F##>>&1q=HHNN#O#'3 HHMM"A4H''kk&)  !M!%, &'D"!%D 	*ww%,,&)*&%)"" '	*s   8D CD 
E$,EEc                     U R                   (       a  gU R                  R                  S:  aH  U R                  R                  (       a,  U R                  R                  S5        U R                  5         ggg)zDMaintain the dirty arbiter process by respawning if needed.
        Nr   zSpawning dirty arbiter...)r1   rG   r   r   r'   ru   r   rC   s    r   r   Arbiter.manage_dirty_arbiter  sS     !!88!!A%$((*=*=HHMM56$$& +>%r@   c                     U R                   R                  n[        R                  R	                  U5      (       d3  [        R                  R                  [        R                  " 5       U5      nU$ )z<Get the control socket path, making relative paths absolute.)rG   control_socketr#   r  isabsrf   r   r6   )r<   r  s     r   _get_control_socket_path Arbiter._get_control_socket_path  sD    hh--ww}}[))'',,t{{}kBKr@   c                    U R                   R                  (       a  U R                  R                  S5        gSSKJn  U R                  5       nU R                   R                  n U" XU5      U l        U R                  R                  5         g! [         a-  nU R                  R                  SU5        SU l         SnAgSnAff = f)zoStart the control socket server.

The server runs in a background thread and accepts commands
via Unix socket.
zControl socket disabledNr   )ControlSocketServerz"Failed to start control socket: %s)rG   control_socket_disabler'   re   gunicorn.ctl.serverr  r  control_socket_moder4   r   r   r  )r<   r  r  socket_moderS  s        r   r   Arbiter._start_control_server  s     88**HHNN45 	<335hh22	(#6;$D    &&( 	(HHA1E#'D  	(s   %(B 
C#C  Cc                     U R                   (       a#   U R                   R                  5         SU l         gg! [         a&  nU R                  R	                  SU5         SnAN4SnAff = f)z(Stop the control socket server.
        z!Error stopping control server: %sN)r4   r   r   r'   re   )r<   rS  s     r   r   Arbiter._stop_control_server  s^     G$$))+ $(D     GBAFFGs   6 
A& A!!A&)r~   r*   r;   r4   r&   r%   r5   r`   r=   rG   r2   r1   r3   r'   r0   r/   rb   rz   r,   rd   r.   r   rc   r-   r_   )Nr   )r   )T)@__name__
__module____qualname____firstlineno____doc__rI  rJ  r;   r~   r  r   NSIGr   r   r   r   dictdirr   r>   rD   rK   propertyrb   r+   r   r}   r   r   r   
handle_cldr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,  r`  r   r=  r  r  r   r   r   r   r  r   r   __static_attributes__)r   xr   r   s   0000r   r   r      s     NIIG [[N FKKMOM1 vw{+MOG >A&k I
-
^!: +-=>K@:"x
8'.`'" J3

	4??"7"24"l GDGR6*8t=@29h
.'6	V&P,\* *>	'(4	(KOs   C>
r   )rL  r#   r(   rw  r   r7   rv   rr  r   gunicorn.errorsr   r   gunicorn.pidfiler   gunicornr   r   r   r	   r
   r   rW   r@   r   <module>r     s=     	    
    6 $ ( ( 1
E( E(r@   