o
    f                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ dZe
jdkreddd Zdd ZG dd dejZ G dd dej!Z"G dd dej#ej$Z%G dd dej&Z'G dd dZ(G d d! d!e(Z)G d"d# d#e(Z*G d$d% d%e*Z+G d&d' d'e*Z,G d(d) d)e(Z-G d*d+ d+e(Z.G d,d- d-ej/Z0e Z1e0Z2dS ).z2Selector event loop for Unix with signal handling.    N   )base_events)base_subprocess)	constants)
coroutines)events)
exceptions)futures)selector_events)tasks)
transports)logger)SelectorEventLoopAbstractChildWatcherSafeChildWatcherFastChildWatcherPidfdChildWatcherMultiLoopChildWatcherThreadedChildWatcherDefaultEventLoopPolicywin32z+Signals are not really supported on Windowsc                 C      dS )zDummy signal handler.N )signumframer   r   */usr/lib/python3.10/asyncio/unix_events.py_sighandler_noop*   s   r   c                 C   s$   zt | W S  ty   |  Y S w N)oswaitstatus_to_exitcode
ValueError)statusr   r   r   r   /   s
   r   c                       s   e Zd ZdZd) fdd	Z fddZdd Zd	d
 Zdd Zdd Z	dd Z
		d*ddZ		d*ddZ	d)ddZdd Z	d)dddddddZ	d)dddddddd Zd!d" Zd#d$ Zd%d& Zd'd( Z  ZS )+_UnixSelectorEventLoopzdUnix event loop.

    Adds signal handling and UNIX Domain Socket support to SelectorEventLoop.
    Nc                    s   t  | i | _d S r   )super__init___signal_handlers)selfselector	__class__r   r   r$   ?      
z_UnixSelectorEventLoop.__init__c                    s`   t    t st| jD ]}| | qd S | jr.tjd| dt	| d | j
  d S d S )NzClosing the loop z@ on interpreter shutdown stage, skipping signal handlers removalsource)r#   closesysis_finalizinglistr%   remove_signal_handlerwarningswarnResourceWarningclearr&   sigr(   r   r   r-   C   s   
z_UnixSelectorEventLoop.closec                 C   s   |D ]
}|sq|  | qd S r   )_handle_signal)r&   datar   r   r   r   _process_self_dataQ   s
   z)_UnixSelectorEventLoop._process_self_datac                 G   s.  t |s
t |rtd| | |   z
t| j	  W n t
tfy5 } ztt|d}~ww t||| d}|| j|< zt|t t|d W dS  ty } z8| j|= | jsztd W n t
tfy } ztd| W Y d}~nd}~ww |jtjkrtd| d d}~ww )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()NFset_wakeup_fd(-1) failed: %ssig  cannot be caught)r   iscoroutineiscoroutinefunction	TypeError_check_signal_check_closedsignalset_wakeup_fd_csockfilenor    OSErrorRuntimeErrorstrr   Handler%   r   siginterruptr   infoerrnoEINVAL)r&   r7   callbackargsexchandlenexcr   r   r   add_signal_handlerX   s>   


z)_UnixSelectorEventLoop.add_signal_handlerc                 C   s:   | j |}|du rdS |jr| | dS | | dS )z2Internal helper that is the actual signal handler.N)r%   get
_cancelledr1   _add_callback_signalsafe)r&   r7   rS   r   r   r   r8      s   z%_UnixSelectorEventLoop._handle_signalc              
   C   s   |  | z| j|= W n
 ty   Y dS w |tjkrtj}ntj}zt|| W n tyE } z|jtj	kr@t
d| d d}~ww | jsmztd W dS  ttfyl } ztd| W Y d}~dS d}~ww dS )zwRemove a handler for a signal.  UNIX only.

        Return True if a signal handler was removed, False if not.
        Fr=   r>   Nr;   r<   T)rB   r%   KeyErrorrD   SIGINTdefault_int_handlerSIG_DFLrH   rN   rO   rI   rE   r    r   rM   )r&   r7   handlerrR   r   r   r   r1      s6   

z,_UnixSelectorEventLoop.remove_signal_handlerc                 C   s6   t |tstd||t vrtd| dS )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.
        zsig must be an int, not zinvalid signal number N)
isinstanceintrA   rD   valid_signalsr    r6   r   r   r   rB      s
   
z$_UnixSelectorEventLoop._check_signalc                 C      t | ||||S r   )_UnixReadPipeTransportr&   pipeprotocolwaiterextrar   r   r   _make_read_pipe_transport      z0_UnixSelectorEventLoop._make_read_pipe_transportc                 C   ra   r   )_UnixWritePipeTransportrc   r   r   r   _make_write_pipe_transport   ri   z1_UnixSelectorEventLoop._make_write_pipe_transportc	              
      s   t  U}
|
 std|  }t| |||||||f||d|	}|
| | j| z|I d H  W n t	t
fy?     tyQ   |  | I d H   w W d    |S 1 s]w   Y  |S )NzRasyncio.get_child_watcher() is not activated, subprocess support is not installed.)rf   rg   )r   get_child_watcher	is_activerI   create_future_UnixSubprocessTransportadd_child_handlerget_pid_child_watcher_callback
SystemExitKeyboardInterruptBaseExceptionr-   _wait)r&   re   rQ   shellstdinstdoutstderrbufsizerg   kwargswatcherrf   transpr   r   r   _make_subprocess_transport   s:   



z1_UnixSelectorEventLoop._make_subprocess_transportc                 C   s   |  | j|j| d S r   )call_soon_threadsafe	call_soon_process_exited)r&   pid
returncoder~   r   r   r   rr      s   z._UnixSelectorEventLoop._child_watcher_callback)sslsockserver_hostnamessl_handshake_timeoutc          	         s  |d u st |tsJ |r|d u rtdn|d urtd|d ur'td|d urZ|d ur3tdt|}ttjtjd}z|d | 	||I d H  W n(   |
   |d u rbtd|jtjksn|jtjkrutd||d | j|||||d	I d H \}}||fS )
Nz/you have to pass server_hostname when using sslz+server_hostname is only meaningful with ssl1ssl_handshake_timeout is only meaningful with ssl3path and sock can not be specified at the same timer   Fzno path and sock were specified.A UNIX Domain Stream Socket was expected, got )r   )r^   rJ   r    r   fspathsocketAF_UNIXSOCK_STREAMsetblockingsock_connectr-   familytype_create_connection_transport)	r&   protocol_factorypathr   r   r   r   	transportre   r   r   r   create_unix_connection   sP   


z-_UnixSelectorEventLoop.create_unix_connectiond   T)r   backlogr   r   start_servingc             
      s  t |tr
td|d ur|std|d ur|d ur tdt|}ttjtj}|d dvrezt	
t	|jrBt| W n! tyL   Y n tyd } ztd|| W Y d }~nd }~ww z|| W nF ty }	 z|  |	jtjkrd|d}
ttj|
d  d }	~	w   |   |d u rtd	|jtjks|jtjkrtd
||d t| |g||||}|r|  tdI d H  |S )Nz*ssl argument must be an SSLContext or Noner   r   r   )r    z2Unable to check or remove stale UNIX socket %r: %rzAddress z is already in usez-path was not specified, and no sock specifiedr   F)r^   boolrA   r    r   r   r   r   r   statS_ISSOCKst_moderemoveFileNotFoundErrorrH   r   errorbindr-   rN   
EADDRINUSEr   r   r   r   Server_start_servingr   sleep)r&   r   r   r   r   r   r   r   errrR   msgserverr   r   r   create_unix_server  sp   



z)_UnixSelectorEventLoop.create_unix_serverc           
   
      s   zt j W n ty   tdw z| }W n ttjfy- } ztdd }~ww zt |j	}W n t
yB   tdw |rG|n|}|sMdS |  }	| |	d |||||d |	I d H S )Nzos.sendfile() is not availableznot a regular filer   )r   sendfileAttributeErrorr   SendfileNotAvailableErrorrG   ioUnsupportedOperationfstatst_sizerH   rn   _sock_sendfile_native_impl)
r&   r   fileoffsetcountrG   r   fsize	blocksizefutr   r   r   _sock_sendfile_nativeU  s6   



z,_UnixSelectorEventLoop._sock_sendfile_nativec	                 C   s*  |  }	|d ur| | | r| ||| d S |r2|| }|dkr2| ||| || d S z
t|	|||}
W n ttfya   |d u rO| 	|| | 
|	| j||	||||||
 Y d S  ty } zK|d ur|jtjkrt|turtdtj}||_|}|dkrtd}| ||| || n| ||| || W Y d }~d S W Y d }~d S d }~w ttfy     ty } z| ||| || W Y d }~d S d }~ww |
dkr| ||| || d S ||
7 }||
7 }|d u r| 	|| | 
|	| j||	||||||
 d S )Nr   zsocket is not connectedzos.sendfile call failed)rG   remove_writer	cancelled_sock_sendfile_update_filepos
set_resultr   r   BlockingIOErrorInterruptedError_sock_add_cancellation_callback
add_writerr   rH   rN   ENOTCONNr   ConnectionError	__cause__r   r   set_exceptionrs   rt   ru   )r&   r   registered_fdr   rG   r   r   r   
total_sentfdsentrR   new_excr   r   r   r   r   l  st   



z1_UnixSelectorEventLoop._sock_sendfile_native_implc                 C   s    |dkrt ||t j d S d S Nr   )r   lseekSEEK_SET)r&   rG   r   r   r   r   r   r     s   z4_UnixSelectorEventLoop._sock_sendfile_update_fileposc                    s    fdd}| | d S )Nc                    s.   |   r }|dkr | d S d S d S )Nr;   )r   rG   r   )r   r   r&   r   r   r   cb  s   zB_UnixSelectorEventLoop._sock_add_cancellation_callback.<locals>.cb)add_done_callback)r&   r   r   r   r   r   r   r     s   z6_UnixSelectorEventLoop._sock_add_cancellation_callbackr   NN)__name__
__module____qualname____doc__r$   r-   r:   rU   r8   r1   rB   rh   rk   r   rr   r   r   r   r   r   r   __classcell__r   r   r(   r   r"   9   sD    -
 


.CFr"   c                       s   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZejfddZdddZdd Zdd Z  ZS ) rb   i   Nc                    s   t  | || jd< || _|| _| | _|| _d| _d| _	t
| jj}t|sBt|sBt|sBd | _d | _d | _tdt
| jd | j| jj|  | j| jj| j| j |d urm| jtj|d  d S d S )Nrd   Fz)Pipe transport is for pipes/sockets only.)r#   r$   _extra_loop_piperG   _fileno	_protocol_closing_pausedr   r   r   r   S_ISFIFOr   S_ISCHRr    set_blockingr   connection_made_add_reader_read_readyr	   _set_result_unless_cancelled)r&   looprd   re   rf   rg   moder(   r   r   r$     s8   



z_UnixReadPipeTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  t| jdd }| jd urH|d urHt	|| jt
j}|rB|d n|d n| jd urS|d n|d dd	|S )
Nclosedclosingfd=	_selectorpollingidleopen<{}> )r)   r   r   appendr   r   getattrr   r
   _test_selector_event	selectors
EVENT_READformatjoin)r&   rM   r'   r   r   r   r   __repr__  s$   





z_UnixReadPipeTransport.__repr__c              
   C   s   z
t | j| j}W n$ ttfy   Y d S  ty. } z| |d W Y d }~d S d }~ww |r9| j	| d S | j
 rDtd|  d| _| j
| j | j
| jj | j
| jd  d S )Nz"Fatal read error on pipe transport%r was closed by peerT)r   readr   max_sizer   r   rH   _fatal_errorr   data_receivedr   	get_debugr   rM   r   _remove_readerr   eof_received_call_connection_lost)r&   r9   rR   r   r   r   r     s    
z"_UnixReadPipeTransport._read_readyc                 C   sB   | j s| jrd S d| _| j| j | j rtd|  d S d S )NTz%r pauses reading)r   r   r   r   r   r   r   debugr&   r   r   r   pause_reading  s   
z$_UnixReadPipeTransport.pause_readingc                 C   sF   | j s| jsd S d| _| j| j| j | j r!td|  d S d S )NFz%r resumes reading)	r   r   r   r   r   r   r   r   r  r  r   r   r   resume_reading  s   
z%_UnixReadPipeTransport.resume_readingc                 C   
   || _ d S r   r   r&   re   r   r   r   set_protocol     
z#_UnixReadPipeTransport.set_protocolc                 C      | j S r   r  r  r   r   r   get_protocol     z#_UnixReadPipeTransport.get_protocolc                 C   r  r   r   r  r   r   r   
is_closing  r  z!_UnixReadPipeTransport.is_closingc                 C   s   | j s
| d  d S d S r   )r   _closer  r   r   r   r-   !  s   z_UnixReadPipeTransport.closec                 C   0   | j d ur|d| t| d | j   d S d S Nzunclosed transport r+   r   r4   r-   r&   _warnr   r   r   __del__%     
z_UnixReadPipeTransport.__del__Fatal error on pipe transportc                 C   sZ   t |tr|jtjkr| j rtjd| |dd n| j||| | j	d | 
| d S Nz%r: %sTexc_info)message	exceptionr   re   )r^   rH   rN   EIOr   r   r   r  call_exception_handlerr   r  r&   rR   r  r   r   r   r   *  s   
z#_UnixReadPipeTransport._fatal_errorc                 C   s(   d| _ | j| j | j| j| d S NT)r   r   r   r   r   r  r&   rR   r   r   r   r  8  s   z_UnixReadPipeTransport._closec                 C   N   z| j | W | j  d | _d | _ d | _d S | j  d | _d | _ d | _w r   r   connection_lostr   r-   r   r#  r   r   r   r  =     


z,_UnixReadPipeTransport._call_connection_lostr   r  )r   r   r   r   r$   r   r   r  r  r
  r  r  r-   r2   r3   r  r   r  r  r   r   r   r(   r   rb     s    
rb   c                       s   e Zd Zd% fdd	Zdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZejfddZdd Zd&dd Zd'd!d"Zd#d$ Z  ZS )(rj   Nc           
         s  t  || || jd< || _| | _|| _t | _d| _	d| _
t| jj}t|}t|}t|}	|sJ|sJ|	sJd | _d | _d | _tdt| jd | j| jj|  |	sd|rptjdsp| j| jj| j| j |d ur| jtj|d  d S d S )Nrd   r   Fz?Pipe transport is only for pipes, sockets and character devicesaix)r#   r$   r   r   rG   r   r   	bytearray_buffer
_conn_lostr   r   r   r   r   r   r   r   r    r   r   r   r   r.   platform
startswithr   r   r	   r   )
r&   r   rd   re   rf   rg   r   is_charis_fifo	is_socketr(   r   r   r$   J  s8   





z _UnixWritePipeTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  t| jdd }| jd urT|d urTt	|| jt
j}|rB|d n|d |  }|d|  n| jd ur_|d n|d d	d
|S )Nr   r   r   r   r   r   zbufsize=r   r   r   )r)   r   r   r   r   r   r   r   r
   r   r   EVENT_WRITEget_write_buffer_sizer   r   )r&   rM   r'   r   r{   r   r   r   r   o  s(   






z _UnixWritePipeTransport.__repr__c                 C   s
   t | jS r   )lenr+  r  r   r   r   r3    r  z-_UnixWritePipeTransport.get_write_buffer_sizec                 C   s8   | j  rtd|  | jr| t  d S |   d S )Nr   )r   r   r   rM   r+  r  BrokenPipeErrorr  r   r   r   r     s
   
z#_UnixWritePipeTransport._read_readyc              
   C   sB  t |tttfsJ t|t |trt|}|sd S | js!| jr5| jtjkr,t	
d |  jd7  _d S | jsz	t| j|}W n5 ttfyN   d}Y n) ttfyW     tyv } z|  jd7  _| |d W Y d }~d S d }~ww |t|krd S |dkrt||d  }| j| j| j |  j|7  _|   d S )Nz=pipe closed by peer or os.write(pipe, data) raised exception.r   r   #Fatal write error on pipe transport)r^   bytesr*  
memoryviewreprr,  r   r   !LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr+  r   writer   r   r   rs   rt   ru   r   r4  r   _add_writer_write_ready_maybe_pause_protocol)r&   r9   nrR   r   r   r   r<    s<   

z_UnixWritePipeTransport.writec              
   C   s  | j sJ dz
t| j| j }W n@ ttfy   Y d S  ttfy&     tyQ } z | j 	  |  j
d7  _
| j| j | |d W Y d }~d S d }~ww |t| j krz| j 	  | j| j |   | jrx| j| j | d  d S |dkr| j d |= d S d S )NzData should not be emptyr   r6  r   )r+  r   r<  r   r   r   rs   rt   ru   r5   r,  r   _remove_writerr   r4  _maybe_resume_protocolr   r   r  )r&   r@  rR   r   r   r   r>    s2   


z$_UnixWritePipeTransport._write_readyc                 C   r   r"  r   r  r   r   r   can_write_eof     z%_UnixWritePipeTransport.can_write_eofc                 C   sF   | j rd S | js
J d| _ | js!| j| j | j| jd  d S d S r"  )r   r   r+  r   r   r   r   r  r  r   r   r   	write_eof  s   
z!_UnixWritePipeTransport.write_eofc                 C   r  r   r  r	  r   r   r   r
    r  z$_UnixWritePipeTransport.set_protocolc                 C   r  r   r  r  r   r   r   r    r  z$_UnixWritePipeTransport.get_protocolc                 C   r  r   r  r  r   r   r   r    r  z"_UnixWritePipeTransport.is_closingc                 C   s$   | j d ur| js|   d S d S d S r   )r   r   rE  r  r   r   r   r-     s   z_UnixWritePipeTransport.closec                 C   r  r  r  r  r   r   r   r    r  z_UnixWritePipeTransport.__del__c                 C      |  d  d S r   )r  r  r   r   r   abort     z_UnixWritePipeTransport.abortr  c                 C   sN   t |tr| j rtjd| |dd n| j||| | jd | | d S r  )	r^   rH   r   r   r   r  r   r   r  r!  r   r   r   r     s   

z$_UnixWritePipeTransport._fatal_errorc                 C   sF   d| _ | jr| j| j | j  | j| j | j| j| d S r"  )	r   r+  r   rA  r   r5   r   r   r  r#  r   r   r   r    s   
z_UnixWritePipeTransport._closec                 C   r$  r   r%  r#  r   r   r   r  
  r'  z-_UnixWritePipeTransport._call_connection_lostr   r(  r   )r   r   r   r$   r   r3  r   r<  r>  rC  rE  r
  r  r  r-   r2   r3   r  rG  r   r  r  r   r   r   r(   r   rj   G  s$    %	#	

rj   c                   @   s   e Zd Zdd ZdS )ro   c           	   	   K   s   d }|t jkrtjdrt \}}z7t j|f||||d|d|| _|d ur:|	  t
| d|d| j_d }W |d urI|	  |	  d S d S |d urX|	  |	  w w )Nr)  F)rw   rx   ry   rz   universal_newlinesr{   wb)	buffering)
subprocessPIPEr.   r-  r.  r   
socketpairPopen_procr-   r   detachrx   )	r&   rQ   rw   rx   ry   rz   r{   r|   stdin_wr   r   r   _start  s0   
z_UnixSubprocessTransport._startN)r   r   r   rS  r   r   r   r   ro     s    ro   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )r   aH  Abstract 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.
    c                 G      t  )a  Register 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.
        NotImplementedErrorr&   r   rP   rQ   r   r   r   rp   C  s   	z&AbstractChildWatcher.add_child_handlerc                 C   rT  )zRemoves the handler for process 'pid'.

        The function returns True if the handler was successfully removed,
        False if there was nothing to remove.rU  r&   r   r   r   r   remove_child_handlerN  s   z)AbstractChildWatcher.remove_child_handlerc                 C   rT  )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.
        rU  r&   r   r   r   r   attach_loopV  s   z AbstractChildWatcher.attach_loopc                 C   rT  )zlClose the watcher.

        This must be called to make sure that any underlying resource is freed.
        rU  r  r   r   r   r-   `  s   zAbstractChildWatcher.closec                 C   rT  )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.

        rU  r  r   r   r   rm   g  s   zAbstractChildWatcher.is_activec                 C   rT  )zdEnter the watcher's context and allow starting new processes

        This function must return selfrU  r  r   r   r   	__enter__p  s   zAbstractChildWatcher.__enter__c                 C   rT  )zExit the watcher's contextrU  r&   abcr   r   r   __exit__v  s   zAbstractChildWatcher.__exit__N)r   r   r   r   rp   rY  r[  r-   rm   r\  ra  r   r   r   r   r   ,  s    
	r   c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )r   a6  Child 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                 C      d | _ i | _d S r   r   
_callbacksr  r   r   r   r$        
zPidfdChildWatcher.__init__c                 C      | S r   r   r  r   r   r   r\    rD  zPidfdChildWatcher.__enter__c                 C      d S r   r   )r&   exc_type	exc_valueexc_tracebackr   r   r   ra    rD  zPidfdChildWatcher.__exit__c                 C      | j d uo	| j  S r   r   
is_runningr  r   r   r   rm        zPidfdChildWatcher.is_activec                 C   rF  r   r[  r  r   r   r   r-     rH  zPidfdChildWatcher.closec                 C   sd   | j d ur|d u r| jrtdt | j D ]\}}}| j | t| q| j	  || _ d S NzCA loop is being detached from a child watcher with pending handlers)
r   rd  r2   r3   RuntimeWarningvaluesr   r   r-   r5   )r&   r   pidfd_r   r   r   r[    s   

zPidfdChildWatcher.attach_loopc                 G   s\   | j |}|d ur|d ||f| j |< d S t|}| j|| j| |||f| j |< d S r   )rd  rV   r   
pidfd_openr   r   _do_wait)r&   r   rP   rQ   existingrs  r   r   r   rp     s   
z#PidfdChildWatcher.add_child_handlerc                 C   s~   | j |\}}}| j| z
t|d\}}W n ty*   d}td| Y nw t	|}t
| |||g|R   d S )Nr      zJchild process pid %d exit status already read:  will report returncode 255)rd  popr   r   r   waitpidChildProcessErrorr   r;  r   r-   )r&   r   rs  rP   rQ   rt  r!   r   r   r   r   rv    s   	
zPidfdChildWatcher._do_waitc                 C   sF   z| j |\}}}W n
 ty   Y dS w | j| t| dS )NFT)rd  ry  rY   r   r   r   r-   )r&   r   rs  rt  r   r   r   rY    s   
z&PidfdChildWatcher.remove_child_handlerN)r   r   r   r   r$   r\  ra  rm   r-   r[  rp   rv  rY  r   r   r   r   r   {  s    	r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )BaseChildWatcherc                 C   rb  r   rc  r  r   r   r   r$     re  zBaseChildWatcher.__init__c                 C   rF  r   ro  r  r   r   r   r-     rH  zBaseChildWatcher.closec                 C   rk  r   rl  r  r   r   r   rm     rn  zBaseChildWatcher.is_activec                 C   rT  r   rU  )r&   expected_pidr   r   r   _do_waitpid  r  zBaseChildWatcher._do_waitpidc                 C   rT  r   rU  r  r   r   r   _do_waitpid_all  r  z BaseChildWatcher._do_waitpid_allc                 C   s   |d u st |tjsJ | jd ur|d u r| jrtdt | jd ur*| jt	j
 || _|d ur?|t	j
| j |   d S d S rp  )r^   r   AbstractEventLoopr   rd  r2   r3   rq  r1   rD   SIGCHLDrU   	_sig_chldr  rZ  r   r   r   r[    s   
zBaseChildWatcher.attach_loopc              
   C   sZ   z|    W d S  ttfy     ty, } z| jd|d W Y d }~d S d }~ww )N$Unknown exception in SIGCHLD handler)r  r  )r  rs   rt   ru   r   r   r#  r   r   r   r    s   zBaseChildWatcher._sig_chldN)
r   r   r   r$   r-   rm   r~  r  r[  r  r   r   r   r   r|    s    r|  c                       sP   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )r   ad  '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)
    c                    s   | j   t   d S r   )rd  r5   r#   r-   r  r(   r   r   r-   	  s   
zSafeChildWatcher.closec                 C   rf  r   r   r  r   r   r   r\    rD  zSafeChildWatcher.__enter__c                 C   rg  r   r   r]  r   r   r   ra    rD  zSafeChildWatcher.__exit__c                 G   s   ||f| j |< | | d S r   )rd  r~  rW  r   r   r   rp     s   z"SafeChildWatcher.add_child_handlerc                 C   $   z| j |= W dS  ty   Y dS w NTFrd  rY   rX  r   r   r   rY       z%SafeChildWatcher.remove_child_handlerc                 C      t | jD ]}| | qd S r   r0   rd  r~  rX  r   r   r   r     s   z SafeChildWatcher._do_waitpid_allc                 C   s   |dksJ zt |t j\}}W n ty$   |}d}td| Y nw |dkr+d S t|}| j r;t	d|| z
| j
|\}}W n ty_   | j r\tjd|dd Y d S Y d S w |||g|R   d S )Nr   rx  8Unknown child process pid %d, will report returncode 255$process %s exited with returncode %s'Child watcher got an unexpected pid: %rTr  )r   rz  WNOHANGr{  r   r;  r   r   r   r  rd  ry  rY   )r&   r}  r   r!   r   rP   rQ   r   r   r   r~  %  s8   	

zSafeChildWatcher._do_waitpid)r   r   r   r   r-   r\  ra  rp   rY  r  r~  r   r   r   r(   r   r     s    
r   c                       sT   e Zd ZdZ fddZ fddZdd Zdd	 Zd
d Zdd Z	dd Z
  ZS )r   aW  '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).
    c                    s$   t    t | _i | _d| _d S r   )r#   r$   	threadingLock_lock_zombies_forksr  r(   r   r   r$   R  s   


zFastChildWatcher.__init__c                    s"   | j   | j  t   d S r   )rd  r5   r  r#   r-   r  r(   r   r   r-   X  s   

zFastChildWatcher.closec                 C   s<   | j  |  jd7  _| W  d    S 1 sw   Y  d S )Nr   )r  r  r  r   r   r   r\  ]  s   $zFastChildWatcher.__enter__c                 C   sv   | j ( |  jd8  _| js| js	 W d    d S t| j}| j  W d    n1 s.w   Y  td| d S )Nr   z5Caught subprocesses termination from unknown pids: %s)r  r  r  rJ   r5   r   r;  )r&   r^  r_  r`  collateral_victimsr   r   r   ra  c  s   
	zFastChildWatcher.__exit__c              	   G   s   | j sJ d| j( z| j|}W n ty*   ||f| j|< Y W d    d S w W d    n1 s5w   Y  |||g|R   d S )NzMust use the context manager)r  r  r  ry  rY   rd  )r&   r   rP   rQ   r   r   r   r   rp   q  s   	z"FastChildWatcher.add_child_handlerc                 C   r  r  r  rX  r   r   r   rY    r  z%FastChildWatcher.remove_child_handlerc              	   C   s  	 zt dt j\}}W n
 ty   Y d S w |dkrd S t|}| jF z
| j|\}}W n' tyV   | j	rR|| j
|< | j rJtd|| Y W d    q d }Y nw | j rctd|| W d    n1 smw   Y  |d u r~td|| n	|||g|R   q)NTr;   r   z,unknown process %s exited with returncode %sr  z8Caught subprocess termination from unknown pid: %d -> %d)r   rz  r  r{  r   r  rd  ry  rY   r  r  r   r   r   r  r;  )r&   r   r!   r   rP   rQ   r   r   r   r    sH   


z FastChildWatcher._do_waitpid_all)r   r   r   r   r$   r-   r\  ra  rp   rY  r  r   r   r   r(   r   r   H  s    	r   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r   a~  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).
    c                 C   s   i | _ d | _d S r   )rd  _saved_sighandlerr  r   r   r   r$     re  zMultiLoopChildWatcher.__init__c                 C   s
   | j d uS r   )r  r  r   r   r   rm     r  zMultiLoopChildWatcher.is_activec                 C   sT   | j   | jd u rd S ttj}|| jkrtd nttj| j d | _d S )Nz+SIGCHLD handler was changed by outside code)	rd  r5   r  rD   	getsignalr  r  r   r;  )r&   r]   r   r   r   r-     s   



zMultiLoopChildWatcher.closec                 C   rf  r   r   r  r   r   r   r\    rD  zMultiLoopChildWatcher.__enter__c                 C   rg  r   r   r&   rh  exc_valexc_tbr   r   r   ra    rD  zMultiLoopChildWatcher.__exit__c                 G   s&   t  }|||f| j|< | | d S r   )r   get_running_looprd  r~  )r&   r   rP   rQ   r   r   r   r   rp     s   z'MultiLoopChildWatcher.add_child_handlerc                 C   r  r  r  rX  r   r   r   rY    r  z*MultiLoopChildWatcher.remove_child_handlerc                 C   sN   | j d urd S ttj| j| _ | j d u rtd tj| _ ttjd d S )NzaPrevious SIGCHLD handler was set by non-Python code, restore to default handler on watcher close.F)r  rD   r  r  r   r;  r\   rL   rZ  r   r   r   r[    s   


z!MultiLoopChildWatcher.attach_loopc                 C   r  r   r  rX  r   r   r   r    s   z%MultiLoopChildWatcher._do_waitpid_allc           	      C   s   |dksJ zt |t j\}}W n ty&   |}d}td| d}Y nw |dkr-d S t|}d}z| j|\}}}W n t	yP   tjd|dd Y d S w |
 r^td|| d S |rk| rktd	|| |j|||g|R   d S )
Nr   rx  r  FTr  r  %Loop %r that handles pid %r is closedr  )r   rz  r  r{  r   r;  r   rd  ry  rY   	is_closedr   r  r   )	r&   r}  r   r!   r   	debug_logr   rP   rQ   r   r   r   r~     s<   
z!MultiLoopChildWatcher._do_waitpidc              	   C   sD   z|    W d S  ttfy     ty!   tjddd Y d S w )Nr  Tr  )r  rs   rt   ru   r   r;  )r&   r   r   r   r   r   r  %  s   zMultiLoopChildWatcher._sig_chldN)r   r   r   r   r$   rm   r-   r\  ra  rp   rY  r[  r  r~  r  r   r   r   r   r     s    %r   c                   @   sn   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
jfddZdd Zdd Zdd Zdd ZdS )r   aA  Threaded 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.
    c                 C   s   t d| _i | _d S r   )	itertoolsr   _pid_counter_threadsr  r   r   r   r$   ;  r*   zThreadedChildWatcher.__init__c                 C   r   r"  r   r  r   r   r   rm   ?  rD  zThreadedChildWatcher.is_activec                 C   s   |    d S r   )_join_threadsr  r   r   r   r-   B  s   zThreadedChildWatcher.closec                 C   s.   dd t | j D }|D ]}|  qdS )z%Internal: Join all non-daemon threadsc                 S   s   g | ]}|  r|js|qS r   )is_alivedaemon.0threadr   r   r   
<listcomp>G  s
    
z6ThreadedChildWatcher._join_threads.<locals>.<listcomp>N)r0   r  rr  r   )r&   threadsr  r   r   r   r  E  s   
z"ThreadedChildWatcher._join_threadsc                 C   rf  r   r   r  r   r   r   r\  L  rD  zThreadedChildWatcher.__enter__c                 C   rg  r   r   r  r   r   r   ra  O  rD  zThreadedChildWatcher.__exit__c                 C   s:   dd t | j D }|r|| j dt| d d S d S )Nc                 S   s   g | ]}|  r|qS r   )r  r  r   r   r   r  S  s    
z0ThreadedChildWatcher.__del__.<locals>.<listcomp>z0 has registered but not finished child processesr+   )r0   r  rr  r)   r4   )r&   r  r  r   r   r   r  R  s   
zThreadedChildWatcher.__del__c                 G   sF   t  }tj| jdt| j ||||fdd}|| j|< |  d S )Nzwaitpid-T)targetnamerQ   r  )	r   r  r  Threadr~  nextr  r  start)r&   r   rP   rQ   r   r  r   r   r   rp   Z  s   

z&ThreadedChildWatcher.add_child_handlerc                 C   r   r"  r   rX  r   r   r   rY  c  s   z)ThreadedChildWatcher.remove_child_handlerc                 C   rg  r   r   rZ  r   r   r   r[  i  rD  z ThreadedChildWatcher.attach_loopc                 C   s   |dksJ z
t |d\}}W n ty#   |}d}td| Y nw t|}| r3td|| | r?td|| n|j	|||g|R   | j
| d S )Nr   rx  r  r  r  )r   rz  r{  r   r;  r   r   r  r  r   r  ry  )r&   r   r}  rP   rQ   r   r!   r   r   r   r   r~  l  s(   	z ThreadedChildWatcher._do_waitpidN)r   r   r   r   r$   rm   r-   r  r\  ra  r2   r3   r  rp   rY  r[  r~  r   r   r   r   r   .  s    	r   c                       sH   e Zd ZdZeZ fddZdd Z fddZdd	 Z	d
d Z
  ZS )_UnixDefaultEventLoopPolicyz:UNIX event loop policy with a watcher for child processes.c                    s   t    d | _d S r   )r#   r$   _watcherr  r(   r   r   r$     s   

z$_UnixDefaultEventLoopPolicy.__init__c                 C   s~   t j2 | jd u r%t | _t t u r-| j| jj	 W d    d S W d    d S W d    d S 1 s8w   Y  d S r   )
r   r  r  r   r  current_threadmain_threadr[  _localr   r  r   r   r   _init_watcher  s   
"z)_UnixDefaultEventLoopPolicy._init_watcherc                    s>   t  | | jdurt t u r| j| dS dS dS )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  r  r  r  r[  rZ  r(   r   r   r    s   
z*_UnixDefaultEventLoopPolicy.set_event_loopc                 C   s   | j du r	|   | j S )z~Get the watcher for child processes.

        If not yet set, a ThreadedChildWatcher object is automatically created.
        N)r  r  r  r   r   r   rl     s   
z-_UnixDefaultEventLoopPolicy.get_child_watcherc                 C   s4   |du st |tsJ | jdur| j  || _dS )z$Set the watcher for child processes.N)r^   r   r  r-   )r&   r}   r   r   r   set_child_watcher  s   


z-_UnixDefaultEventLoopPolicy.set_child_watcher)r   r   r   r   r"   _loop_factoryr$   r  r  rl   r  r   r   r   r(   r   r    s    
r  )3r   rN   r   r  r   r   rD   r   r   rL  r.   r  r2    r   r   r   r   r   r   r	   r
   r   r   logr   __all__r-  ImportErrorr   r   BaseSelectorEventLoopr"   ReadTransportrb   _FlowControlMixinWriteTransportrj   BaseSubprocessTransportro   r   r   r|  r   r   r   r   BaseDefaultEventLoopPolicyr  r   r   r   r   r   r   <module>   sd    
	
   	 	 NON5Ji}Y2