3 \ @s dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZddlmZdddddgZejdkredddZy ejZWnek r,ddZYnXGdddejZ e!edrVddZ"nddl#Z#d dZ"Gd!d"d"ej$Z%Gd#d$d$ej&ej'Z(e!ed%rej)Z*nddl#Z#d&d'Z*Gd(d)d)e j+Z,Gd*ddZ-Gd+d,d,e-Z.Gd-dde.Z/Gd.dde.Z0Gd/d0d0ej1Z2e Z3e2Z4dS)1z2Selector event loop for Unix with signal handling.N) base_events)base_subprocess)compat) constants) coroutines)events)futures)selector_events) selectors) transports) coroutine)loggerSelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherDefaultEventLoopPolicyZwin32z+Signals are not really supported on WindowscCsdS)zDummy signal handler.N)signumframerr+/usr/lib64/python3.6/asyncio/unix_events.py_sighandler_noop%srcCs|S)Nr)pathrrr.srcseZdZdZd"fdd ZddZfddZd d Zd d Zd dZ ddZ ddZ d#ddZ d$ddZ ed%ddZddZeddddddZed&ddddd d!ZZS)'_UnixSelectorEventLoopzdUnix event loop. Adds signal handling and UNIX Domain Socket support to SelectorEventLoop. Ncstj|i|_dS)N)super__init___signal_handlers)selfselector) __class__rrr7s z_UnixSelectorEventLoop.__init__cCstjS)N)socketZ socketpair)rrrr _socketpair;sz"_UnixSelectorEventLoop._socketpaircs^tjtjs2xFt|jD]}|j|qWn(|jrZtjd|dt |d|jj dS)NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removal)source) rclosesys is_finalizinglistrremove_signal_handlerwarningswarnResourceWarningclear)rsig)r!rrr%>s z_UnixSelectorEventLoop.closecCs"x|D]}|sq|j|qWdS)N)_handle_signal)rdatarrrr_process_self_dataLs z)_UnixSelectorEventLoop._process_self_datac+GsHtj|stj|rtd|j||jytj|jj Wn2t t fk rt}zt t |WYdd}~XnXtj|||}||j|<ytj|ttj|dWnt k rB}zz|j|=|jsytjdWn4t t fk r}ztjd|WYdd}~XnX|jtjkr0t dj|nWYdd}~XnXdS)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()NFrzset_wakeup_fd(-1) failed: %szsig {} cannot be caught)rZ iscoroutineZiscoroutinefunction TypeError _check_signalZ _check_closedsignal set_wakeup_fdZ_csockfileno ValueErrorOSError RuntimeErrorstrrZHandlerr siginterruptrinfoerrnoEINVALformat)rr.callbackargsexchandleZnexcrrradd_signal_handlerSs0     z)_UnixSelectorEventLoop.add_signal_handlercCs8|jj|}|dkrdS|jr*|j|n |j|dS)z2Internal helper that is the actual signal handler.N)rgetZ _cancelledr)Z_add_callback_signalsafe)rr.rDrrrr/s   z%_UnixSelectorEventLoop._handle_signalc&Cs|j|y |j|=Wntk r*dSX|tjkr>tj}ntj}ytj||Wn@tk r}z$|jtj krt dj |nWYdd}~XnX|jsytj dWn2t tfk r}ztjd|WYdd}~XnXdS)zwRemove a handler for a signal. UNIX only. Return True if a signal handler was removed, False if not. Fzsig {} cannot be caughtNrzset_wakeup_fd(-1) failed: %sTr2)r4rKeyErrorr5SIGINTdefault_int_handlerSIG_DFLr9r>r?r:r@r6r8rr=)rr.ZhandlerrCrrrr)s(    z,_UnixSelectorEventLoop.remove_signal_handlercCsHt|tstdj|d|ko,tjknsDtdj|tjdS)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 {!r}rzsig {} out of range(1, {})N) isinstanceintr3r@r5NSIGr8)rr.rrrr4s  z$_UnixSelectorEventLoop._check_signalcCst|||||S)N)_UnixReadPipeTransport)rpipeprotocolwaiterextrarrr_make_read_pipe_transportsz0_UnixSelectorEventLoop._make_read_pipe_transportcCst|||||S)N)_UnixWritePipeTransport)rrOrPrQrRrrr_make_write_pipe_transportsz1_UnixSelectorEventLoop._make_write_pipe_transportc kstj} |j} t||||||||f| |d| } | j| j|j| y| EdHWn&tk r~} z | }WYdd} ~ XnXd}|dk r| j| j EdH|WdQRX| S)N)rQrR) rget_child_watcherZ create_future_UnixSubprocessTransportadd_child_handlerZget_pid_child_watcher_callback Exceptionr%Z_wait)rrPrBshellstdinstdoutstderrbufsizerRkwargswatcherrQtransprCerrrrr_make_subprocess_transports$     z1_UnixSelectorEventLoop._make_subprocess_transportcCs|j|j|dS)N)Zcall_soon_threadsafeZ_process_exited)rpid returncoderbrrrrYsz._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamec cs|r|dkr&tdn|dk r&td|dk r|dk r>tdtjtjtjd}y |jd|j||EdHWq|jYqXnB|dkrtd|jtjkstj |j  rtdj ||jd|j ||||EdH\}}||fS)Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with sslz3path and sock can not be specified at the same timerFzno path and sock were specifiedz2A UNIX Domain Stream Socket was expected, got {!r}) r8r"AF_UNIX SOCK_STREAM setblockingZ sock_connectr%familyr_is_stream_sockettyper@Z_create_connection_transport)rprotocol_factoryrrgrhri transportrPrrrcreate_unix_connections8    z-_UnixSelectorEventLoop.create_unix_connectiond)rhbacklogrgc !Cst|trtd|dk r0|dk r,tdt|}tjtjtj}|dd kry tj t j|j rnt j |WnBt k rYn0tk r}ztjd||WYdd}~XnXy|j|Wnjtk r}z8|j|jtjkrdj|}ttj|dnWYdd}~Xn|jYnXn>|dkrBtd|jtjks`tj|j rntdj|tj||g} |j||jd |j|||| | S) Nz*ssl argument must be an SSLContext or Nonez3path and sock can not be specified at the same timerz2Unable to check or remove stale UNIX socket %r: %rzAddress {!r} is already in usez-path was not specified, and no sock specifiedz2A UNIX Domain Stream Socket was expected, got {!r}F)rru)rKboolr3r8_fspathr"rjrkstatS_ISSOCKosst_moderemoveFileNotFoundErrorr9rerrorZbindr%r>Z EADDRINUSEr@rmrrnroZServerZlistenrlZ_start_serving) rrprrhrtrgrcrCmsgZserverrrrcreate_unix_serversP         z)_UnixSelectorEventLoop.create_unix_server)N)NN)NN)N)N)__name__ __module__ __qualname____doc__rr#r%r1rEr/r)r4rSrUr rdrYrrr __classcell__rr)r!rr1s, -      %r set_blockingcCstj|ddS)NF)rzr)fdrrr_set_nonblockingBsrcCs,tj|tj}|tjB}tj|tj|dS)N)fcntlZF_GETFLrz O_NONBLOCKZF_SETFL)rflagsrrrrGs cseZdZdZd fdd ZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ e jrhddZd!ddZddZddZZS)"rNiNcstj|||jd<||_||_|j|_||_d|_t j |jj }t j |pbt j|pbt j|s~d|_d|_d|_tdt|j|jj|jj||jj|jj|j|j|dk r|jjtj|ddS)NrOFz)Pipe transport is for pipes/sockets only.)rr_extra_loop_piper7_fileno _protocol_closingrzfstatr{rxS_ISFIFOryS_ISCHRr8r call_soonconnection_made _add_reader _read_readyr _set_result_unless_cancelled)rlooprOrPrQrRmode)r!rrrQs,          z_UnixReadPipeTransport.__init__cCs|jjg}|jdkr |jdn|jr0|jd|jd|jt|jdd}|jdk r|dk rtj ||jt j }|r|jdq|jdn |jdk r|jdn |jddd j |S) Nclosedclosingzfd=%s _selectorpollingidleopenz<%s> ) r!rrappendrrgetattrrr _test_selector_eventr Z EVENT_READjoin)rr=r rrrr__repr__ns$          z_UnixReadPipeTransport.__repr__cCsytj|j|j}WnDttfk r,Yntk rX}z|j|dWYdd}~Xn^X|rl|jj |nJ|j j rt j d|d|_|j j|j|j j|jj|j j|jddS)Nz"Fatal read error on pipe transportz%r was closed by peerT)rzreadrmax_sizeBlockingIOErrorInterruptedErrorr9 _fatal_errorrZ data_receivedr get_debugrr=r_remove_readerrZ eof_received_call_connection_lost)rr0rCrrrrs  z"_UnixReadPipeTransport._read_readycCs|jj|jdS)N)rrr)rrrr pause_readingsz$_UnixReadPipeTransport.pause_readingcCs|jj|j|jdS)N)rrrr)rrrrresume_readingsz%_UnixReadPipeTransport.resume_readingcCs ||_dS)N)r)rrPrrr set_protocolsz#_UnixReadPipeTransport.set_protocolcCs|jS)N)r)rrrr get_protocolsz#_UnixReadPipeTransport.get_protocolcCs|jS)N)r)rrrr is_closingsz!_UnixReadPipeTransport.is_closingcCs|js|jddS)N)r_close)rrrrr%sz_UnixReadPipeTransport.closecCs,|jdk r(tjd|t|d|jjdS)Nzunclosed transport %r)r$)rr*r+r,r%)rrrr__del__s  z_UnixReadPipeTransport.__del__Fatal error on pipe transportcCsZt|tr4|jtjkr4|jjrLtjd||ddn|jj||||j d|j |dS)Nz%r: %sT)exc_info)message exceptionrqrP) rKr9r>ZEIOrrrdebugcall_exception_handlerrr)rrCrrrrrs  z#_UnixReadPipeTransport._fatal_errorcCs(d|_|jj|j|jj|j|dS)NT)rrrrrr)rrCrrrrsz_UnixReadPipeTransport._closec Cs4z|jj|Wd|jjd|_d|_d|_XdS)N)rconnection_lostrr%r)rrCrrrrs  z,_UnixReadPipeTransport._call_connection_losti)NN)r)rrrrrrrrrrrrr%rPY34rrrrrrr)r!rrNMs rNcseZdZd%fdd ZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZejr|ddZddZd&dd Zd'd!d"Zd#d$ZZS)(rTNc stj||||jd<||_|j|_||_t|_d|_ d|_ t j |jj }tj|}tj|}tj|} |px|px| sd|_d|_d|_tdt|j|jj|jj|| s|rtjjd r|jj|jj|j|j|dk r|jjtj|ddS)NrOrFz?Pipe transport is only for pipes, sockets and character devicesaix)rrrrr7rr bytearray_buffer _conn_lostrrzrr{rxrrryr8rrrrr&platform startswithrrr r) rrrOrPrQrRrZis_charZis_fifoZ is_socket)r!rrrs2          z _UnixWritePipeTransport.__init__cCs|jjg}|jdkr |jdn|jr0|jd|jd|jt|jdd}|jdk r|dk rtj ||jt j }|r|jdn |jd|j }|jd|n |jdk r|jdn |jdd d j |S) Nrrzfd=%srrrz bufsize=%srz<%s>r)r!rrrrrrrr rr Z EVENT_WRITEget_write_buffer_sizer)rr=r rr_rrrrs(          z _UnixWritePipeTransport.__repr__cCs t|jS)N)lenr)rrrrrsz-_UnixWritePipeTransport.get_write_buffer_sizecCs6|jjrtjd||jr*|jtn|jdS)Nz%r was closed by peer)rrrr=rrBrokenPipeError)rrrrrs   z#_UnixWritePipeTransport._read_readycCst|trt|}|sdS|js&|jrN|jtjkrs&   z$_UnixWritePipeTransport._write_readycCsdS)NTr)rrrr can_write_eofXsz%_UnixWritePipeTransport.can_write_eofcCs8|jr dSd|_|js4|jj|j|jj|jddS)NT)rrrrrrr)rrrr write_eof[s z!_UnixWritePipeTransport.write_eofcCs ||_dS)N)r)rrPrrrrdsz$_UnixWritePipeTransport.set_protocolcCs|jS)N)r)rrrrrgsz$_UnixWritePipeTransport.get_protocolcCs|jS)N)r)rrrrrjsz"_UnixWritePipeTransport.is_closingcCs|jdk r|j r|jdS)N)rrr)rrrrr%msz_UnixWritePipeTransport.closecCs,|jdk r(tjd|t|d|jjdS)Nzunclosed transport %r)r$)rr*r+r,r%)rrrrrvs  z_UnixWritePipeTransport.__del__cCs|jddS)N)r)rrrrabort|sz_UnixWritePipeTransport.abortFatal error on pipe transportcCsPt|tjr*|jjrBtjd||ddn|jj||||jd|j |dS)Nz%r: %sT)r)rrrqrP) rKrZ_FATAL_ERROR_IGNORErrrrrrr)rrCrrrrrs   z$_UnixWritePipeTransport._fatal_errorcCsFd|_|jr|jj|j|jj|jj|j|jj|j|dS)NT) rrrrrr-rrr)rrCrrrrs  z_UnixWritePipeTransport._closec Cs4z|jj|Wd|jjd|_d|_d|_XdS)N)rrrr%r)rrCrrrrs  z-_UnixWritePipeTransport._call_connection_lost)NN)r)N)rrrrrrrrrrrrrrr%rrrrrrrrrr)r!rrTs$% !   rTset_inheritablecCsNttdd}tj|tj}|s4tj|tj||Bntj|tj||@dS)NZ FD_CLOEXECr)rrZF_GETFDZF_SETFD)rZ inheritableZ cloexec_flagoldrrr_set_inheritables  rc@seZdZddZdS)rWc Ksvd}|tjkr*|jj\}}t|jdtj|f||||d|d||_|dk rr|jt |j d|d|j_ dS)NF)r[r\r]r^Zuniversal_newlinesr_wb) buffering) subprocessPIPErr#rr7Popen_procr%rdetachr\) rrBr[r\r]r^r_r`Zstdin_wrrr_starts  z_UnixSubprocessTransport._startN)rrrrrrrrrWsrWc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)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. cGs tdS)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. N)NotImplementedError)rrerArBrrrrXs z&AbstractChildWatcher.add_child_handlercCs tdS)zRemoves the handler for process 'pid'. The function returns True if the handler was successfully removed, False if there was nothing to remove.N)r)rrerrrremove_child_handlersz)AbstractChildWatcher.remove_child_handlercCs tdS)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. N)r)rrrrr attach_loopsz AbstractChildWatcher.attach_loopcCs tdS)zlClose the watcher. This must be called to make sure that any underlying resource is freed. N)r)rrrrr%szAbstractChildWatcher.closecCs tdS)zdEnter the watcher's context and allow starting new processes This function must return selfN)r)rrrr __enter__szAbstractChildWatcher.__enter__cCs tdS)zExit the watcher's contextN)r)rabcrrr__exit__ szAbstractChildWatcher.__exit__N) rrrrrXrrr%rrrrrrrs  c@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)BaseChildWatchercCsd|_i|_dS)N)r _callbacks)rrrrrszBaseChildWatcher.__init__cCs|jddS)N)r)rrrrr%szBaseChildWatcher.closecCs tdS)N)r)r expected_pidrrr _do_waitpidszBaseChildWatcher._do_waitpidcCs tdS)N)r)rrrr_do_waitpid_allsz BaseChildWatcher._do_waitpid_allcCsf|jdk r$|dkr$|jr$tjdt|jdk r<|jjtj||_|dk rb|jtj|j |j dS)NzCA loop is being detached from a child watcher with pending handlers) rrr*r+RuntimeWarningr)r5SIGCHLDrE _sig_chldr)rrrrrrs zBaseChildWatcher.attach_loopcCsFy |jWn4tk r@}z|jjd|dWYdd}~XnXdS)Nz$Unknown exception in SIGCHLD handler)rr)rrZrr)rrCrrrr1s  zBaseChildWatcher._sig_chldcCs2tj|rtj| Stj|r*tj|S|SdS)N)rz WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUS)rstatusrrr_compute_returncode=s     z$BaseChildWatcher._compute_returncodeN) rrrrr%rrrrrrrrrrs rcsPeZdZdZfddZddZddZdd Zd d Zd d Z ddZ Z 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) cs|jjtjdS)N)rr-rr%)r)r!rrr%Vs zSafeChildWatcher.closecCs|S)Nr)rrrrrZszSafeChildWatcher.__enter__cCsdS)Nr)rrrrrrrr]szSafeChildWatcher.__exit__cGs.|jdkrtd||f|j|<|j|dS)NzICannot add child handler, the child watcher does not have a loop attached)rr:rr)rrerArBrrrrX`s  z"SafeChildWatcher.add_child_handlerc Cs&y |j|=dStk r dSXdS)NTF)rrG)rrerrrrks z%SafeChildWatcher.remove_child_handlercCs"xt|jD]}|j|q WdS)N)r(rr)rrerrrrrsz SafeChildWatcher._do_waitpid_allcCsytj|tj\}}Wn(tk r>|}d}tjd|Yn0X|dkrLdS|j|}|jjrntj d||y|j j |\}}Wn.t k r|jjrtjd|ddYnX|||f|dS)Nz8Unknown child process pid %d, will report returncode 255rz$process %s exited with returncode %sz'Child watcher got an unexpected pid: %rT)r) rzwaitpidWNOHANGChildProcessErrorrrrrrrrpoprG)rrrerrfrArBrrrrws*    zSafeChildWatcher._do_waitpid) rrrrr%rrrXrrrrrr)r!rrKs   csTeZdZdZfddZfddZddZdd Zd d Zd d Z ddZ Z 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). cs$tjtj|_i|_d|_dS)Nr)rr threadingZLock_lock_zombies_forks)r)r!rrrs  zFastChildWatcher.__init__cs"|jj|jjtjdS)N)rr-rrr%)r)r!rrr%s  zFastChildWatcher.closec Cs$|j|jd7_|SQRXdS)Nr)rr)rrrrrszFastChildWatcher.__enter__c CsV|j:|jd8_|js$|j r(dSt|j}|jjWdQRXtjd|dS)Nrz5Caught subprocesses termination from unknown pids: %s)rrrr;r-rr)rrrrZcollateral_victimsrrrrs zFastChildWatcher.__exit__cGsl|jdkrtd|j:y|jj|}Wn"tk rL||f|j|<dSXWdQRX|||f|dS)NzICannot add child handler, the child watcher does not have a loop attached)rr:rrrrGr)rrerArBrfrrrrXs z"FastChildWatcher.add_child_handlerc Cs&y |j|=dStk r dSXdS)NTF)rrG)rrerrrrs z%FastChildWatcher.remove_child_handlercCsxytjdtj\}}Wntk r,dSX|dkr:dS|j|}|jvy|jj|\}}WnBtk r|j r||j |<|j j rt jd||wd}YnX|j j rt jd||WdQRX|dkrt jd||q|||f|qWdS)Nrrz,unknown process %s exited with returncode %sz$process %s exited with returncode %sz8Caught subprocess termination from unknown pid: %d -> %dr2)rzrrrrrrrrGrrrrrrr)rrerrfrArBrrrrs6      z FastChildWatcher._do_waitpid_all) rrrrrr%rrrXrrrrr)r!rrs   csHeZdZdZeZfddZddZfddZdd Z d d Z Z S) _UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.cstjd|_dS)N)rr_watcher)r)r!rrr s z$_UnixDefaultEventLoopPolicy.__init__c CsHtj8|jdkr:t|_ttjtjr:|jj|j j WdQRXdS)N) rrrrrKrcurrent_thread _MainThreadr_localr)rrrr _init_watchers  z)_UnixDefaultEventLoopPolicy._init_watchercs6tj||jdk r2ttjtjr2|jj|dS)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)rset_event_looprrKrrrr)rr)r!rrrs  z*_UnixDefaultEventLoopPolicy.set_event_loopcCs|jdkr|j|jS)zzGet the watcher for child processes. If not yet set, a SafeChildWatcher object is automatically created. N)rr)rrrrrV&s z-_UnixDefaultEventLoopPolicy.get_child_watchercCs|jdk r|jj||_dS)z$Set the watcher for child processes.N)rr%)rrarrrset_child_watcher0s  z-_UnixDefaultEventLoopPolicy.set_child_watcher) rrrrrZ _loop_factoryrrrrVrrrr)r!rrs   r)5rr>rzr5r"rxrr&rr*rrrrrrr r r r r logr__all__r ImportErrorrfspathrwAttributeErrorZBaseSelectorEventLooprhasattrrrZ ReadTransportrNZ_FlowControlMixinZWriteTransportrTrrZBaseSubprocessTransportrWrrrrZBaseDefaultEventLoopPolicyrrrrrrrsn                O  F=On2