fdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZdZe j6dk(reddZdZGddej>Z GddejBZ"GddejFejHZ%GddejLZ'GddZ(Gdde(Z)Gd d!e(Z*Gd"d#e*Z+Gd$d%e*Z,Gd&d'e(Z-Gd(d)e(Z.d*Z/Gd+d,ej`Z1e Z2e1Z3y)-z2Selector event loop for Unix with signal handling.N) base_events)base_subprocess) constants) coroutines)events) exceptions)futures)selector_events)tasks) transports)logger)SelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherPidfdChildWatcherMultiLoopChildWatcherThreadedChildWatcherDefaultEventLoopPolicywin32z+Signals are not really supported on Windowscy)zDummy signal handler.N)signumframes */usr/lib/python3.12/asyncio/unix_events.py_sighandler_noopr*scP tj|S#t$r|cYSwxYwN)oswaitstatus_to_exitcode ValueError)statuss rr"r"/s.((00  s  %%ceZdZdZdfd ZfdZdZdZdZdZ d Z dd Z dd Z dd Z d Z ddddddddZ dddddddddZdZdZdZdZxZS)_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Nc2t||i|_yr )super__init___signal_handlers)selfselector __class__s rr)z_UnixSelectorEventLoop.__init__?s " "rc0t|tjs,t |j D]}|j |y|j r;tjd|dt||j jyy)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removalsource) r(closesys is_finalizinglistr*remove_signal_handlerwarningswarnResourceWarningclear)r+sigr-s rr1z_UnixSelectorEventLoop.closeCs    "D112 0**3/ 0$$ 1$:HI.%) + %%++- %rc:|D]}|s|j|yr )_handle_signal)r+datars r_process_self_dataz)_UnixSelectorEventLoop._process_self_dataQs% (F    '  (rcRtj|stj|r td|j ||j  t j|jjtj|||d}||j |< t j |t"t j$|dy#ttf$r}tt|d}~wwxYw#t$r}|j |=|j sI t jdn2#ttf$r }t'j(d|Yd}~nd}~wwxYw|j*t*j,k(rtd|dd}~wwxYw)zAdd a handler for a signal. UNIX only. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. z3coroutines cannot be used with add_signal_handler()NFset_wakeup_fd(-1) failed: %ssig  cannot be caught)r iscoroutineiscoroutinefunction TypeError _check_signal _check_closedsignal set_wakeup_fd_csockfilenor#OSError RuntimeErrorstrrHandler*r siginterruptrinfoerrnoEINVAL)r+r:callbackargsexchandlenexcs radd_signal_handlerz)_UnixSelectorEventLoop.add_signal_handlerXsq  " "8 ,..x889 9 3  )  !3!3!5 6xtT:%+c"  MM#/ 0   U +G$ )s3x( ( ) %%c*((F((,"G,FKK >EEFyyELL("T#.?#@AA sZ-C-0D D-DD F&F!,EF!E1E,'F!,E110F!!F&c|jj|}|y|jr|j|y|j |y)z2Internal helper that is the actual signal handler.N)r*get _cancelledr5_add_callback_signalsafe)r+r:rXs rr<z%_UnixSelectorEventLoop._handle_signalsE&&**3/ >      & &s +  ) )& 1rc|j| |j|=|tjk(rtj }ntj } tj|||js tjdyy#t$rYywxYw#t$r2}|jtjk(rtd|dd}~wwxYw#ttf$r }tjd|Yd}~yd}~wwxYw)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. FrBrCNr@rAT)rGr*KeyErrorrISIGINTdefault_int_handlerSIG_DFLrMrSrTrNrJr#rrR)r+r:handlerrWs rr5z,_UnixSelectorEventLoop.remove_signal_handlers 3 %%c* &-- 00GnnG  MM#w '$$ A$$R(-   yyELL("T#.?#@AA  ( A :C@@ AsA BB8C BB C'-CCD +DD ct|tstd||tjvrt d|y)zInternal helper to validate a signal. Raise ValueError if the signal number is invalid or uncatchable. Raise RuntimeError if there is a problem setting up the handler. zsig must be an int, not zinvalid signal number N) isinstanceintrFrI valid_signalsr#)r+r:s rrGz$_UnixSelectorEventLoop._check_signalsJ #s#6sg>? ? f**, ,5cU;< < -rc t|||||Sr )_UnixReadPipeTransportr+pipeprotocolwaiterextras r_make_read_pipe_transportz0_UnixSelectorEventLoop._make_read_pipe_transports%dD(FEJJrc t|||||Sr )_UnixWritePipeTransportrks r_make_write_pipe_transportz1_UnixSelectorEventLoop._make_write_pipe_transports&tT8VUKKrc lKtj5tjdtt j } ddd 5| j s td|j} t||||||||f| |d| } | j| j|j|  | d{ ddd| S#1swYxYw7#ttf$rt$r+| j!| j#d{7wxYw#1swY SxYww)NignorezRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rnro)r6catch_warnings simplefilterDeprecationWarningrget_child_watcher is_activerN create_future_UnixSubprocessTransportadd_child_handlerget_pid_child_watcher_callback SystemExitKeyboardInterrupt BaseExceptionr1_wait) r+rmrVshellstdinstdoutstderrbufsizerokwargswatcherrntransps r_make_subprocess_transportz1_UnixSelectorEventLoop._make_subprocess_transports; $ $ & 1  ! !(,> ?..0G 1 $$& #$GHH'')F-dHdE,16676396/56F  % %fnn&6$($@$@& J  ! 0 9 1 1( 12    lln$$ ' 0 seD4/C D4A-D'>C!CC! D4CD4C!!;D$DD$$D''D1,D4c<|j|j|yr )call_soon_threadsafe_process_exited)r+pid returncoders rrz._UnixSelectorEventLoop._child_watcher_callbacks !!&"8"8*Er)sslsockserver_hostnamessl_handshake_timeoutssl_shutdown_timeoutcK|t|tsJ|r |2td| td| td| td|| tdtj|}t j t j t jd} |jd|j||d{nf| td|jt j k7s|jt jk7rtd ||jd|j|||||| d{\}} || fS7#|jxYw7#w) Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with ssl1ssl_handshake_timeout is only meaningful with ssl0ssl_shutdown_timeout is only meaningful with ssl3path and sock can not be specified at the same timerFzno path and sock were specified.A UNIX Domain Stream Socket was expected, got )rr)rfrOr#r!fspathsocketAF_UNIX SOCK_STREAM setblocking sock_connectr1familytype_create_connection_transport) r+protocol_factorypathrrrrr transportrms rcreate_unix_connectionz-_UnixSelectorEventLoop.create_unix_connections &*_c*JJJ & EGG* !NOO$0 GII#/ FHH   IKK99T?D==1C1CQGD   '''d333 | !BCC v~~-II!3!33 DTHMOO   U #$($E$E "C"7!5%F%77 8(""%4  7s=B"E7%&E E EBE7E5 E7EE22E7dT)rbacklogrrr start_servingc Kt|tr td| |s td| |s td|| tdt j |}t j t jt j}|ddvrH tjt j|jrt j| |j#|nU| td |j*t jk7s|j,t jk7rtd ||j/d t1j2||g|||||} |r-| j5t7j8dd{| S#t$rYt$r!} tj d|| Yd} ~ d} ~ wwxYw#t$rT} |j%| j&t&j(k(r!d|d } tt&j(| dd} ~ w|j%xYw7w) Nz*ssl argument must be an SSLContext or Nonerrrr)rz2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedrF)rfboolrFr#r!rrrrstatS_ISSOCKst_moderemoveFileNotFoundErrorrMrerrorbindr1rS EADDRINUSErrrrServer_start_servingr sleep) r+rrrrrrrrerrrWmsgservers rcreate_unix_serverz)_UnixSelectorEventLoop.create_unix_servers7 c4 HI I ,SCE E +CBD D   IKK99T?D==1C1CDDAwk)6}}RWWT]%:%:; $  $| CEE v~~-II!3!33 DTHMOO ##D4&2B$'2G$8:   ! ! #++a.  S)6LL"*+/666  99 0 00%TH,>?C!%"2"2C8dB  & !siBIAF'"G3B-I I!I' G0I2G:GIGI I 'AH66I  Ic K tj |j } tj|j}|r|n|}|sy|j} |j| d|||||d| d{S#t$rtjdwxYw#tt jf$r}tjdd}~wwxYw#t$rtjdwxYw7~w)Nzos.sendfile() is not availableznot a regular filer) r!sendfileAttributeErrorr SendfileNotAvailableErrorrLioUnsupportedOperationfstatst_sizerMr{_sock_sendfile_native_impl) r+rfileoffsetcountrLrfsize blocksizefuts r_sock_sendfile_nativez,_UnixSelectorEventLoop._sock_sendfile_nativebs 2 KK M[[]F MHHV$,,E#E   " ''T4(.y! Ey% 26602 2 2  7 78 M667KL L M M667KL L MsVC<BB"C6C<;C:<C<BC<"C;CCC<C77C<c |j} ||j||jr|j|||y|r/||z }|dkr%|j||||j |y t j | |||} | dk(r%|j||||j |y|| z }|| z }||j|||j| |j|| |||||| y#ttf$r;||j|||j| |j|| |||||| Yyt$r} |Q| jtjk(r4t| t ur#t!dtj} | | _| } |dk(r:t%j&d} |j||||j)| n)|j||||j)| Yd} ~ yYd} ~ yd} ~ wt*t,f$rt.$r.} |j||||j)| Yd} ~ yd} ~ wwxYw)Nrzsocket is not connectedzos.sendfile call failed)rL remove_writer cancelled_sock_sendfile_update_filepos set_resultr!r_sock_add_cancellation_callback add_writerrBlockingIOErrorInterruptedErrorrMrSENOTCONNrConnectionError __cause__r r set_exceptionrrr)r+r registered_fdrrLrrr total_sentfdsentrWnew_excrs rrz1_UnixSelectorEventLoop._sock_sendfile_native_implysT [[]  $   } - ==?  . .vvz J   *IA~2266:Nz*1 F;;r669=DJqy2266:Nz*$d"  (88dCD$C$CS "D& &y*F[ !12 B$44S$? OOB ? ?f"E9j B ')II/I_4 *-u~~?$'!Q !::-/2266:N!!#&2266:N!!#&&'-.   #  . .vvz J   c " " #s,:C??AIIB6HI+$IIcZ|dkDr&tj||tjyyNr)r!lseekSEEK_SET)r+rLrrs rrz4_UnixSelectorEventLoop._sock_sendfile_update_fileposs" > HHVVR[[ 1 rc6fd}|j|y)Ncv|jr(j}|dk7rj|yyy)Nr@)rrLr)rrr+rs rcbzB_UnixSelectorEventLoop._sock_add_cancellation_callback..cbs6}}[[]8&&r*r)add_done_callback)r+rrrs` ` rrz6_UnixSelectorEventLoop._sock_add_cancellation_callbacks + b!rr NN)__name__ __module__ __qualname____doc__r)r1r>rZr<r5rGrprsrrrrrrrr __classcell__r-s@rr&r&9s # .(+Z2@ =@D(,KAE)-L 04BF*.0#4 "&!% 0#f*.Gs"&!% GR.DFL2"rr&ceZdZdZdfd ZdZdZdZdZdZ dZ d Z d Z d Z d Zej fd ZddZdZdZxZS)rjic4t||||jd<||_||_|j |_||_d|_d|_ tj|j j}tj|sJtj|s5tj |s d|_d|_d|_t#dtj$|j d|jj'|jj(||jj'|j*|j |j,|,|jj't.j0|dyy)NrlFz)Pipe transport is for pipes/sockets only.)r(r)_extra_loop_piperL_fileno _protocol_closing_pausedr!rrrS_ISFIFOrS_ISCHRr# set_blocking call_soonconnection_made _add_reader _read_readyr _set_result_unless_cancelled)r+looprlrmrnromoder-s rr)z_UnixReadPipeTransport.__init__s. " F  {{} !  xx %-- d# d# T"DJDL!DNHI I  e, T^^;;TB T--!\\4+;+; =   JJ !E!E!' / rc^|jsy|jj||yr ) is_readingrr)r+rrUs rrz"_UnixReadPipeTransport._add_readers#  r8,rc:|j xr |j Sr )rrr+s rrz!_UnixReadPipeTransport.is_readings<<5 $55rct|jjg}|j|jdn|jr|jd|jd|j t |jdd}|jW|Utj||j tj}|r|jdnA|jdn/|j|jdn|jddjd j|S) Nclosedclosingfd= _selectorpollingidleopen<{}> )r-rrappendrrgetattrrr _test_selector_event selectors EVENT_READformatjoin)r+rRr,r s r__repr__z_UnixReadPipeTransport.__repr__s''( ::  KK ! ]] KK " c$,,()4::{D9 :: !h&:%::$,, (<(<>G I& F# ZZ # KK  KK !}}SXXd^,,rch tj|j|j}|r|jj |y|j jrtjd|d|_ |j j|j|j j|jj|j j|jdy#tt f$rYyt"$r}|j%|dYd}~yd}~wwxYw)N%r was closed by peerTz"Fatal read error on pipe transport)r!readrmax_sizer data_receivedr get_debugrrRr_remove_readerr eof_received_call_connection_lostrrrM _fatal_error)r+r=rWs rrz"_UnixReadPipeTransport._read_ready s G774<<7D ,,T2::'')KK 7> $  ))$,,7 $$T^^%@%@A $$T%?%?F !12   I   c#G H H Is*C<<D1 D1D,,D1c|jsyd|_|jj|j|jj rt jd|yy)NTz%r pauses reading)rrrrrrrdebugrs r pause_readingz$_UnixReadPipeTransport.pause_readingsP   !!$,,/ ::   ! LL,d 3 "rc|js |jsyd|_|jj|j|j |jj rtjd|yy)NFz%r resumes reading) rrrrrrrrr#rs rresume_readingz%_UnixReadPipeTransport.resume_reading%s[ ==   t||T-=-=> ::   ! LL-t 4 "rc||_yr rr+rms r set_protocolz#_UnixReadPipeTransport.set_protocol- !rc|jSr r(rs r get_protocolz#_UnixReadPipeTransport.get_protocol0 ~~rc|jSr rrs r is_closingz!_UnixReadPipeTransport.is_closing3 }}rc@|js|jdyyr )r_closers rr1z_UnixReadPipeTransport.close6s}} KK rcv|j-|d|t||jjyyNzunclosed transport r/rr8r1r+_warns r__del__z_UnixReadPipeTransport.__del__:5 :: ! 'x0/$ O JJ    "rc<t|trQ|jtjk(r4|jj rDt jd||dn*|jj||||jd|j|yNz%r: %sTexc_info)message exceptionrrm) rfrMrSEIOrrrr#call_exception_handlerrr4r+rWr@s rr!z#_UnixReadPipeTransport._fatal_error?sr sG $eii)?zz##% XtWtD JJ - -" ! NN /  Crcd|_|jj|j|jj |j |yNT)rrrrrr r+rWs rr4z_UnixReadPipeTransport._closeMs9  !!$,,/ T77=rc |jj||jjd|_d|_d|_y#|jjd|_d|_d|_wxYwr rconnection_lostrr1rrGs rr z,_UnixReadPipeTransport._call_connection_lostRg  NN * *3 / JJ   DJ!DNDJ JJ   DJ!DNDJ A 1A>rzFatal error on pipe transport)rrrrr)rrrrr$r&r*r-r1r1r6r7r:r!r4r rrs@rrjrjs]H/<- 6-*G$45"%MM > rrjceZdZdfd ZdZdZdZdZdZdZ dZ d Z d Z d Z d Zej fd ZdZddZddZdZxZS)rrct |||||jd<||_|j |_||_t|_d|_ d|_ tj|j j}tj|}tj |}tj"|} |s$|s"| s d|_d|_d|_t%dtj&|j d|j(j+|j j,|| s!|rdt.j0j3dsE|j(j+|j(j4|j |j6|,|j(j+t8j:|dyy)NrlrFz?Pipe transport is only for pipes, sockets and character devicesaix)r(r)rrrLrr bytearray_buffer _conn_lostrr!rrrrrrr#rrrrr2platform startswithrrr r) r+rrlrmrnroris_charis_fifo is_socketr-s rr)z _UnixWritePipeTransport.__init___si %" F {{} ! {  xx %--,,t$--%MM$' 7iDJDL!DNDE E  e, T^^;;TB )@)@)G JJ !7!7!%t/?/? A   JJ !E!E!' / rc|jjg}|j|jdn|jr|jd|jd|j t |jdd}|j{|ytj||j tj}|r|jdn|jd|j}|jd|n/|j|jdn|jdd jd j|S) Nrrr r r r zbufsize=r rr)r-rrrrrrrr rr EVENT_WRITEget_write_buffer_sizerr)r+rRr,r rs rrz _UnixWritePipeTransport.__repr__s ''( ::  KK ! ]] KK " c$,,()4::{D9 :: !h&:%::$,, (=(=?G I& F#002G KK(7), - ZZ # KK  KK !}}SXXd^,,rc,t|jSr )lenrRrs rr[z-_UnixWritePipeTransport.get_write_buffer_sizes4<<  rc|jjrtjd||jr|j t y|j y)Nr)rrrrRrRr4BrokenPipeErrorrs rrz#_UnixWritePipeTransport._read_readys@ ::   ! KK/ 6 << KK) * KKMrcZt|tttfsJt |t|tr t|}|sy|j s |j rH|j tjk\rtjd|xj dz c_y|jss tj|j|}|t+|k(ry|dkDrt||d}|j,j/|j|j0|xj|z c_ |j3y#tt f$rd}Yt"t$f$rt&$r1}|xj dz c_|j)|dYd}~yd}~wwxYw)Nz=pipe closed by peer or os.write(pipe, data) raised exception.rr#Fatal write error on pipe transport)rfbytesrQ memoryviewreprrSrr!LOG_THRESHOLD_FOR_CONNLOST_WRITESrwarningrRr!writerrrrrrr!r]r _add_writer _write_ready_maybe_pause_protocol)r+r=nrWs rrgz_UnixWritePipeTransport.writesW$ : >?KdK? dI &d#D  ??dmm)"M"MM HI OOq O || HHT\\40CI~Q!$'+ JJ " "4<<1B1B C   ""$$%56  12   1$!!#'LM s7 EF*"F*9'F%%F*c4|jsJd tj|j|j}|t |jk(r|jj |j j|j|j|jr6|j j|j|jdy|dkDr|jd|=yy#ttf$rYyttf$rt $rp}|jj |xj"dz c_|j j|j|j%|dYd}~yd}~wwxYw)NzData should not be emptyrrra)rRr!rgrr]r9r_remove_writer_maybe_resume_protocolrrr rrrrrrSr!)r+rkrWs rriz$_UnixWritePipeTransport._write_readys@||777| %t||4AC %% ""$ ))$,,7++-==JJ--dll;..t4QLL!$) !12  -.   J LL   OOq O JJ % %dll 3   c#H I I  Js*C??FF'A&FFcyrFrrs r can_write_eofz%_UnixWritePipeTransport.can_write_eofrc|jry|jsJd|_|jsL|jj |j |jj |jdyyrF)rrrRrrrrr rs r write_eofz!_UnixWritePipeTransport.write_eofs[ == zzz || JJ % %dll 3 JJ !;!;T Brc||_yr r(r)s rr*z$_UnixWritePipeTransport.set_protocolr+rc|jSr r(rs rr-z$_UnixWritePipeTransport.get_protocolr.rc|jSr r0rs rr1z"_UnixWritePipeTransport.is_closingr2rcX|j|js|jyyyr )rrrsrs rr1z_UnixWritePipeTransport.closes$ :: !$-- NN +8 !rcv|j-|d|t||jjyyr6r7r8s rr:z_UnixWritePipeTransport.__del__r;rc&|jdyr )r4rs rabortz_UnixWritePipeTransport.aborts Drct|tr4|jjrDt j d||dn*|jj ||||jd|j|yr=) rfrMrrrr#rCrr4rDs rr!z$_UnixWritePipeTransport._fatal_error sc c7 #zz##% XtWtD JJ - -" ! NN /  Crc>d|_|jr%|jj|j|jj |jj |j|jj|j|yrF) rrRrrmrr9rrr rGs rr4z_UnixWritePipeTransport._closesf << JJ % %dll 3  !!$,,/ T77=rc |jj||jjd|_d|_d|_y#|jjd|_d|_d|_wxYwr rIrGs rr z-_UnixWritePipeTransport._call_connection_lostrKrLrrMr )rrrr)rr[rrgrirprsr*r-r1r1r6r7r:rzr!r4r rrs@rrrrr\sd#/J-0!!%F%8C" %MM  >rrrceZdZdZy)r|c d}|tjk(r6tjj drt j \}} tj|f||||d|d||_|=|jt|jd||j_ d}|!|j|jyy#|!|j|jwwxYw)NrPF)rrrruniversal_newlinesrwb) buffering) subprocessPIPEr2rTrUr socketpairPopen_procr1r detachr) r+rVrrrrrrstdin_ws r_startz_UnixSubprocessTransport._start+s JOO # (?(?(F $..0NE7 #))E!vf#('E=CEDJ" #'(8$'#R  "  #w"  #s A!C%C7N)rrrrrrrr|r|)s rr|cBeZdZdZd dZdZdZdZdZdZ d Z d Z y) raHAbstract base class for monitoring child processes. Objects derived from this class monitor a collection of subprocesses and report their termination or interruption by a signal. New callbacks are registered with .add_child_handler(). Starting a new process must be done within a 'with' block to allow the watcher to suspend its activity until the new process if fully registered (this is needed to prevent a race condition in some implementations). Example: with watcher: proc = subprocess.Popen("sleep 1") watcher.add_child_handler(proc.pid, callback) Notes: Implementations of this class must be thread-safe. Since child watcher objects may catch the SIGCHLD signal and call waitpid(-1), there should be only one active object per process. Nc\|jtk7rtjdddyy)NrP{name!r} is deprecated as of Python 3.12 and will be removed in Python {remove}.r)rrr6 _deprecated)clss r__init_subclass__z&AbstractChildWatcher.__init_subclass__Xs, >>X %  !7;%, . &rct)aRegister a new child handler. Arrange for callback(pid, returncode, *args) to be called when process 'pid' terminates. Specifying another callback for the same process replaces the previous handler. Note: callback() must be thread-safe. NotImplementedErrorr+rrUrVs rr}z&AbstractChildWatcher.add_child_handler_s "##rct)zRemoves the handler for process 'pid'. The function returns True if the handler was successfully removed, False if there was nothing to remove.rr+rs rremove_child_handlerz)AbstractChildWatcher.remove_child_handlerjs "##rct)zAttach the watcher to an event loop. If the watcher was previously attached to an event loop, then it is first detached before attaching to the new loop. Note: loop may be None. rr+rs r attach_loopz AbstractChildWatcher.attach_looprs "##rct)zlClose the watcher. This must be called to make sure that any underlying resource is freed. rrs rr1zAbstractChildWatcher.close|s "##rct)zReturn ``True`` if the watcher is active and is used by the event loop. Return True if the watcher is installed and ready to handle process exit notifications. rrs rrzzAbstractChildWatcher.is_actives "##rct)zdEnter the watcher's context and allow starting new processes This function must return selfrrs r __enter__zAbstractChildWatcher.__enter__s "##rct)zExit the watcher's contextrr+abcs r__exit__zAbstractChildWatcher.__exit__s !##r)returnN) rrrrrr}rrr1rzrrrrrrrAs/,. $$$$$$ $rrc@eZdZdZdZdZdZdZdZdZ dZ d Z y ) ra6Child watcher implementation using Linux's pid file descriptors. This child watcher polls process file descriptors (pidfds) to await child process termination. In some respects, PidfdChildWatcher is a "Goldilocks" child watcher implementation. It doesn't require signals or threads, doesn't interfere with any processes launched outside the event loop, and scales linearly with the number of subprocesses launched by the event loop. The main disadvantage is that pidfds are specific to Linux, and only work on recent (5.3+) kernels. c|Sr rrs rrzPidfdChildWatcher.__enter__ rcyr r)r+exc_type exc_value exc_tracebacks rrzPidfdChildWatcher.__exit__ rcyrFrrs rrzzPidfdChildWatcher.is_activerqrcyr rrs rr1zPidfdChildWatcher.closerrcyr rrs rrzPidfdChildWatcher.attach_looprrctj}tj|}|j ||j ||||yr )rget_running_loopr! pidfd_openr_do_wait)r+rrUrVrpidfds rr}z#PidfdChildWatcher.add_child_handlers:&&( c"  sE8TJrc$tj}|j| tj|d\}}t |}tj||||g|y#t $rd}tjd|YCwxYw)NrzJchild process pid %d exit status already read: will report returncode 255) rrrr!waitpidr"ChildProcessErrorrrfr1) r+rrrUrVr_r$rs rrzPidfdChildWatcher._do_waits&&( E" 8 3*IAv07J j(4(! J NN.   sA++!BBcyrFrrs rrz&PidfdChildWatcher.remove_child_handlerrN) rrrrrrrzr1rr}rrrrrrrs0    K )&rrc6eZdZdZdZdZdZdZdZdZ y) BaseChildWatcherc d|_i|_yr )r _callbacksrs rr)zBaseChildWatcher.__init__s rc&|jdyr )rrs rr1zBaseChildWatcher.closes rcV|jduxr|jjSr )r is_runningrs rrzzBaseChildWatcher.is_actives#zz%A$***?*?*AArctr r)r+ expected_pids r _do_waitpidzBaseChildWatcher._do_waitpid !##rctr rrs r_do_waitpid_allz BaseChildWatcher._do_waitpid_allrrc|t|tjsJ|j(|&|jrt j dt|j)|jjtj||_|;|jtj|j|jyy)NzCA loop is being detached from a child watcher with pending handlers)rfrAbstractEventLooprrr6r7RuntimeWarningr5rISIGCHLDrZ _sig_chldrrs rrzBaseChildWatcher.attach_loops|z$0H0HIII :: !dlt MM= :: ! JJ , ,V^^ <    # #FNNDNN C  " rc |jy#ttf$rt$r(}|jj d|dYd}~yd}~wwxYw)N$Unknown exception in SIGCHLD handler)r@rA)rrrrrrCrGs rrzBaseChildWatcher._sig_chldsX   "-.    JJ - -A /    sAAAN) rrrr)r1rzrrrrrrrrrs&B$$#( rrcPeZdZdZfdZfdZdZdZdZdZ dZ d Z xZ S) rad'Safe' child watcher implementation. This implementation avoids disrupting other code spawning processes by polling explicitly each process in the SIGCHLD handler instead of calling os.waitpid(-1). This is a safe solution but it has a significant overhead when handling a big number of children (O(n) each time SIGCHLD is raised) cRt|tjdddy)Nrrrr)r(r)r6rr+r-s rr)zSafeChildWatcher.__init__s' /;%, .rcV|jjt| yr )rr9r(r1rs rr1zSafeChildWatcher.closes   rc|Sr rrs rrzSafeChildWatcher.__enter__rrcyr rrs rrzSafeChildWatcher.__exit__rrcH||f|j|<|j|yr )rrrs rr}z"SafeChildWatcher.add_child_handler"s% ($/ rc> |j|=y#t$rYywxYwNTFrr`rs rrz%SafeChildWatcher.remove_child_handler(( $    cZt|jD]}|j|yr r4rrrs rrz SafeChildWatcher._do_waitpid_all/s(( "C   S ! "rc|dkDsJ tj|tj\}}|dk(ryt|}|jj rt jd|| |jj|\}}|||g|y#t$r|}d}t jd|YOwxYw#t$r7|jj rt jd|dYyYywxYw)Nr$process %s exited with returncode %sr8Unknown child process pid %d, will report returncode 255'Child watcher got an unexpected pid: %rTr>) r!rWNOHANGr"rrrr#rrfrpopr`)r+rrr$rrUrVs rrzSafeChildWatcher._do_waitpid4sa 7**\2::>KCax/7Jzz##% C):7 -!__005NHd S* ,t ,7! CJ NNJ   ( 3zz##%H"T3& 3s#'B4C#CC;DD) rrrrr)r1rrr}rrrrrs@rrrs0.  " -rrcJeZdZdZfdZfdZdZdZdZdZ dZ xZ S) raW'Fast' child watcher implementation. This implementation reaps every terminated processes by calling os.waitpid(-1) directly, possibly breaking other code spawning processes and waiting for their termination. There is no noticeable overhead when handling a big number of children (O(1) each time a child terminates). ct|tj|_i|_d|_tjdddy)Nrrrrr) r(r) threadingLock_lock_zombies_forksr6rrs rr)zFastChildWatcher.__init__asC ^^%   /;%, .rc|jj|jjt|yr )rr9rr(r1rs rr1zFastChildWatcher.closeks,    rct|j5|xjdz c_|cdddS#1swYyxYw)Nr)rrrs rrzFastChildWatcher.__enter__ps/ ZZ  KK1 K   s.7c>|j5|xjdzc_|js |js dddyt|j}|jj dddt j dy#1swY xYw)Nrz5Caught subprocesses termination from unknown pids: %s)rrrrOr9rrf)r+rrrcollateral_victimss rrzFastChildWatcher.__exit__vs{ ZZ " KK1 K{{$--  " " "%T]]!3  MM   ! "  C   " "s/B/BBc |jsJd|j5 |jj|} ddd||g|y#t$r||f|j |<YdddywxYw#1swY |j|=y#t$rYywxYwrrrs rrz%FastChildWatcher.remove_child_handlerrrc tjdtj\}}|dk(ryt|}|j 5 |j j|\}}|jjrtjd|| dddtjd||n |||g#t$rYywxYw#t$r\|jrK||j|<|jjrtjd||Yddd4d}YwxYw#1swYxYw)Nr@rrz,unknown process %s exited with returncode %sz8Caught subprocess termination from unknown pid: %d -> %d)r!rrr"rrrrrrrr#r`rrrf)r+rr$rrUrVs rrz FastChildWatcher._do_waitpid_allsC < jjRZZ8 V !83F;  66%)__%8%8%=NHdzz++- %K%(*6! 6& #Z1j040K%   ${{-7 c*:://1"LL*>),j:! 6 6 $H $ 6 6sN'CD= C'2D= CCAD:*D=5D:7D=9D::D==E) rrrrr)r1rrr}rrrrs@rrrWs+.    )(1rrcReZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zy )ra~A watcher that doesn't require running loop in the main thread. This implementation registers a SIGCHLD signal handler on instantiation (which may conflict with other code that install own handler for this signal). The solution is safe but it has a significant overhead when handling a big number of processes (*O(n)* each time a SIGCHLD is received). cPi|_d|_tjdddy)Nrrrr)r_saved_sighandlerr6rrs rr)zMultiLoopChildWatcher.__init__s*!%4;%, .rc|jduSr )rrs rrzzMultiLoopChildWatcher.is_actives%%T11rcZ|jj|jytjtj }||j k7rtjdd|_ytjtj |jd|_y)Nz+SIGCHLD handler was changed by outside code) rr9rrI getsignalrrrrf)r+rds rr1zMultiLoopChildWatcher.closesz   ! ! ) ""6>>2 dnn $ NNH I"& MM&..$*@*@ A!%rc|Sr rrs rrzMultiLoopChildWatcher.__enter__rrcyr rr+rexc_valexc_tbs rrzMultiLoopChildWatcher.__exit__rrcrtj}|||f|j|<|j|yr )rrrr)r+rrUrVrs rr}z'MultiLoopChildWatcher.add_child_handlers5&&( $h5 rc> |j|=y#t$rYywxYwrrrs rrz*MultiLoopChildWatcher.remove_child_handlerrrc8|jytjtj|j|_|j*t j dtj |_tjtjdy)NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)rrIrrrrfrcrQrs rrz!MultiLoopChildWatcher.attach_loopso  ! ! - !'v~~t~~!N  ! ! ) NNJ K%+^^D " FNNE2rcZt|jD]}|j|yr rrs rrz%MultiLoopChildWatcher._do_waitpid_alls(( "C   S ! "rc8|dkDsJ tj|tj\}}|dk(ryt|}d} |jj|\}}}|jrt j d||y|r'|jrt jd|||j|||g|y#t$r|}d}t j d|d}YwxYw#t$rt j d|d YywxYw) NrTrrF%Loop %r that handles pid %r is closedrrr>)r!rrr"rrrfrr is_closedrr#rr`) r+rrr$r debug_logrrUrVs rrz!MultiLoopChildWatcher._do_waitpidsa **\2::>KCax/7JI L#'??#6#6s#; D(D~~FcR!1LL!G!-z;)))(CKdK=! CJ NNJ I $ / NND / /s#'C C5 %C21C25!DDc |jy#ttf$rt$rt j ddYywxYw)NrTr>)rrrrrrf)r+rrs rrzMultiLoopChildWatcher._sig_chld<sE R  "-.   R NNAD Q Rs/AAN)rrrrr)rzr1rrr}rrrrrrrrrrsA $.2 & 3""#LJRrrcdeZdZdZdZdZdZdZdZe jfdZ dZ d Z d Zd Zy ) raAThreaded child watcher implementation. The watcher uses a thread per process for waiting for the process finish. It doesn't require subscription on POSIX signal but a thread creation is not free. The watcher has O(1) complexity, its performance doesn't depend on amount of spawn processes. cFtjd|_i|_yr) itertoolsr _pid_counter_threadsrs rr)zThreadedChildWatcher.__init__Rs%OOA. rcyrFrrs rrzzThreadedChildWatcher.is_activeVrqrcyr rrs rr1zThreadedChildWatcher.closeYrrc|Sr rrs rrzThreadedChildWatcher.__enter__\rrcyr rrs rrzThreadedChildWatcher.__exit___rrct|jjDcgc]}|jr|}}|r||jdt |yycc}w)Nz0 has registered but not finished child processesr/)r4rvaluesis_aliver-r8)r+r9threadthreadss rr:zThreadedChildWatcher.__del__bsd(,T]]-A-A-C(D)foo'))  T^^$$TU!  )sA!ctj}tj|jdt |j ||||fd}||j|<|jy)Nzasyncio-waitpid-T)targetnamerVdaemon) rrrThreadrnextrrstart)r+rrUrVrrs rr}z&ThreadedChildWatcher.add_child_handlerjsf&&(!!)9)9)9$t?P?P:Q9R'S(,c8T'B)-/$ c rcyrFrrs rrz)ThreadedChildWatcher.remove_child_handlersrrcyr rrs rrz ThreadedChildWatcher.attach_loopyrrc|dkDsJ tj|d\}}t|}|jrt j d|| |jrt jd||n|j|||g||jj|y#t $r|}d}t jd|Y~wxYw)Nrrrrr) r!rr"rrr#rrfrrrr)r+rrrUrVrr$rs rrz ThreadedChildWatcher._do_waitpid|sa 7**\15KC07J~~ C):7 >>  NNBD# N %D % %hZ G$ G ,''! CJ NNJ   sB..#CCN)rrrrr)rzr1rrr6r7r:r}rrrrrrrrEsB   %MM  (rrcttdsy tj}tjtj|dy#t $rYywxYw)NrFrT)hasattrr!getpidr1rrM)rs r can_use_pidfdr&sO 2| $iik sA&'  s=A AAcBeZdZdZeZfdZdZfdZdZ dZ xZ S)_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.c0t|d|_yr )r(r)_watcherrs rr)z$_UnixDefaultEventLoopPolicy.__init__s  rctj5|j)trt |_nt |_dddy#1swYyxYwr )rrr*r&rrrs r _init_watcherz)_UnixDefaultEventLoopPolicy._init_watchersA \\ ;}}$ ?$5$7DM$8$:DM  ; ; ;s 6AAct|||jEtjtj ur|jj |yyy)zSet the event loop. As a side effect, if a child watcher was set before, then calling .set_event_loop() from the main thread will call .attach_loop(loop) on the child watcher. N)r(set_event_loopr*rcurrent_thread main_threadr)r+rr-s rr.z*_UnixDefaultEventLoopPolicy.set_event_loopsS t$ MM %((*i.C.C.EE MM % %d +F &rc|j|jtjddd|jS)z~Get the watcher for child processes. If not yet set, a ThreadedChildWatcher object is automatically created. ryrrr)r*r,r6rrs rryz-_UnixDefaultEventLoopPolicy.get_child_watchers@ ==    0:BI K}}rc|t|tsJ|j|jj||_t j dddy)z$Set the watcher for child processes.Nset_child_watcherrrr)rfrr*r1r6r)r+rs rr3z-_UnixDefaultEventLoopPolicy.set_child_watchersT*W6J"KKK == $ MM   ! 0:BI Kr) rrrrr& _loop_factoryr)r,r.ryr3rrs@rr(r(s%D*M; ,  Krr()4rrSrr r!rrIrrrr2rr6rrrrrr r r r r logr__all__rT ImportErrorrr"BaseSelectorEventLoopr& ReadTransportrj_FlowControlMixinWriteTransportrrBaseSubprocessTransportr|rrrrrrrr&BaseDefaultEventLoopPolicyr(rrrrrr?se8     <<7 C DD P"_BBP"f MZ55M`Jj::(77JZ FF 0S$S$l7,7t2+2jN-'N-bj1'j1Z~R0~RBO(/O(b 6K&"C"C6Kr+4r