
    dafP/                        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mZ d dlmZ ddlmZmZmZmZmZ ddZd	 Zd
 Zd Zd Zd Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d d      Z# e e$       e#eeef       d Z%e&dk(  r ejN                          yy)     NPicklingError)futures)BrokenProcessPool_ThreadWakeup)support   )create_executor_testssetup_moduleProcessPoolForkMixinProcessPoolForkserverMixinProcessPoolSpawnMixinc                 z    | rt        j                  |        ddl}|j                          |j	                          y)zInduces a segfault.r   N)timesleepfaulthandlerdisable_sigsegv)delayr   s     E/root/Python-3.12.4/Lib/test/test_concurrent_futures/test_deadlock.py_crashr      s,    

5    c                     t                y)z,Induces a segfault with dummy data in input.Nr   )datas    r   _crash_with_datar      s    
Hr   c                  .    t        j                  d       y)z#Induces a sys exit with exitcode 1.r	   N)sysexit r   r   _exitr!   !   s    HHQKr   c                      |        )z-Function that raises an Exception in process.r    )Errs    r   _raise_errorr$   &   s	    
%Kr   c                 J    ddl }|j                         t        _         |        )z@Function that raises an Exception in process and ignores stderr.r   N)ioStringIOr   stderr)r#   r&   s     r   _raise_error_ignore_stderrr)   +   s    CJ
%Kr   c                      |        S )z(Function that returns a instance of cls.r    )clss    r   _return_instancer,   2   s	    5Lr   c                       e Zd ZdZd Zy)CrashAtPicklez5Bad object that triggers a segfault at pickling time.c                     t                y Nr   selfs    r   
__reduce__zCrashAtPickle.__reduce__9   s    r   N__name__
__module____qualname____doc__r3   r    r   r   r.   r.   7   s
    ?r   r.   c                       e Zd ZdZd Zy)CrashAtUnpicklez7Bad object that triggers a segfault at unpickling time.c                     t         dfS Nr    r   r1   s    r   r3   zCrashAtUnpickle.__reduce__?   s    rzr   Nr4   r    r   r   r:   r:   =   s
    Ar   r:   c                       e Zd ZdZd Zy)ExitAtPicklez9Bad object that triggers a process exit at pickling time.c                     t                y r0   r!   r1   s    r   r3   zExitAtPickle.__reduce__E   s    r   Nr4   r    r   r   r>   r>   C   s
    Cr   r>   c                       e Zd ZdZd Zy)ExitAtUnpicklez;Bad object that triggers a process exit at unpickling time.c                     t         dfS r<   r@   r1   s    r   r3   zExitAtUnpickle.__reduce__K   s    byr   Nr4   r    r   r   rB   rB   I   s
    Er   rB   c                       e Zd ZdZd Zy)ErrorAtPicklez3Bad object that triggers an error at pickling time.c                     ddl m}  |d      )Nr   r   zError in pickle)pickler   )r2   r   s     r   r3   zErrorAtPickle.__reduce__Q   s    (-..r   Nr4   r    r   r   rE   rE   O   s
    =/r   rE   c                       e Zd ZdZd Zy)ErrorAtUnpicklez5Bad object that triggers an error at unpickling time.c                      ddl m} t        |ffS )Nr   )UnpicklingError)rG   rK   r)   )r2   rK   s     r   r3   zErrorAtUnpickle.__reduce__X   s    *)O+>>>r   Nr4   r    r   r   rI   rI   V   s
    ??r   rI   c                       e Zd Zej                  Zd ZdddZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)ExecutorDeadlockTestc                    dd l }ddlm}  |d      5 }|j                  |       |j	                  d       |j                         }d d d        |j                  j                         D ]  }|j                           |j                  d       t        d t        j                         | j                  d	|        y # 1 sw Y   }xY w)
Nr   )TemporaryFilezw+)mode)fileTwaitz
Traceback:
 zExecutor deadlock:

)r   tempfilerO   dump_tracebackseekread
_processesvalues	terminateshutdownprintr   
__stderr__fail)r2   executorr   rO   ftbps          r   _fail_on_deadlockz&ExecutorDeadlockTest._fail_on_deadlock`   s     	*% 	''Q'/FF1IB	 $$++- 	AKKM	 	t$t$3>>:		*2$/0	 	s   4CCFignore_stderrc                &   | j                   j                  d       | j                  d| j                               } |j                  |g| }|rt        j                         }nt        j                         }	 | j                  |      5  |5  |j                  | j                         d d d        d d d        |j                  d       y # 1 sw Y   $xY w# 1 sw Y   (xY w# t        j                  $ r | j                  |       Y Qw xY w)NTrR      max_workers
mp_context)timeout)r_   r[   executor_typeget_contextsubmitr   captured_stderr
contextlibnullcontextassertRaisesresultTIMEOUTr   TimeoutErrorrc   )r2   errorfuncre   argsr_   rescms           r   _check_crashz!ExecutorDeadlockTest._check_crashs   s    D)%%d&6&6&8 & :hood*T*((*B'')B	-""5) 5 5JJt||J455 	t$5 55 5 ## 	- ""8,	-sB   =C) CC.C6C) C	CC&"C) )$DDc                 J    | j                  t        t        t                      y r0   )r{   r   idrE   r1   s    r   test_error_at_task_picklez.ExecutorDeadlockTest.test_error_at_task_pickle   s     	-]_=r   c                 J    | j                  t        t        t                      y r0   )r{   r   r}   rB   r1   s    r   test_exit_at_task_unpicklez/ExecutorDeadlockTest.test_exit_at_task_unpickle   s    +R1ABr   c                     | j                  t        t        dt        j                         | j	                  t
        t        t                      y )Nr(   )
addCleanupsetattrr   r(   r{   r   r}   rI   r1   s    r   test_error_at_task_unpicklez0ExecutorDeadlockTest.test_error_at_task_unpickle   s0    h

; 	+R1BCr   c                 J    | j                  t        t        t                      y r0   )r{   r   r}   r:   r1   s    r   test_crash_at_task_unpicklez0ExecutorDeadlockTest.test_crash_at_task_unpickle   s    +R1BCr   c                 8    | j                  t        t               y r0   )r{   r   r   r1   s    r   %test_crash_during_func_exec_on_workerz:ExecutorDeadlockTest.test_crash_during_func_exec_on_worker   s    +V4r   c                 8    | j                  t        t               y r0   )r{   
SystemExitr!   r1   s    r   $test_exit_during_func_exec_on_workerz9ExecutorDeadlockTest.test_exit_during_func_exec_on_worker   s    *e,r   c                 B    | j                  t        t        t               y r0   )r{   RuntimeErrorr$   r1   s    r   %test_error_during_func_exec_on_workerz:ExecutorDeadlockTest.test_error_during_func_exec_on_worker   s    ,lCr   c                 B    | j                  t        t        t               y r0   )r{   r   r,   r.   r1   s    r   )test_crash_during_result_pickle_on_workerz>ExecutorDeadlockTest.test_crash_during_result_pickle_on_worker   s     	+-=}Mr   c                 B    | j                  t        t        t               y r0   )r{   r   r,   r>   r1   s    r   (test_exit_during_result_pickle_on_workerz=ExecutorDeadlockTest.test_exit_during_result_pickle_on_worker   s     	*&6Er   c                 B    | j                  t        t        t               y r0   )r{   r   r,   rE   r1   s    r   )test_error_during_result_pickle_on_workerz>ExecutorDeadlockTest.test_error_during_result_pickle_on_worker   s     	-)9=Ir   c                     | j                  t        t        dt        j                         | j	                  t
        t        t        d       y )Nr(   Trd   )r   r   r   r(   r{   r   r,   rI   r1   s    r   3test_error_during_result_unpickle_in_result_handlerzHExecutorDeadlockTest.test_error_during_result_unpickle_in_result_handler   s8    h

; 	+*O(, 	 	.r   c                 B    | j                  t        t        t               y r0   )r{   r   r,   rB   r1   s    r   2test_exit_during_result_unpickle_in_result_handlerzGExecutorDeadlockTest.test_exit_during_result_unpickle_in_result_handler   s     	+-=~Nr   c                 |   | j                   j                  d       | j                  d| j                               5 }|| _         |j	                  t
        d      }|j                  d       | j                  t              5  |j                          d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)NTrR   rg   rh   g?)r   )	r_   r[   rl   rm   rn   r   rr   r   rs   )r2   r_   r`   s      r   test_shutdown_deadlockz+ExecutorDeadlockTest.test_shutdown_deadlock   s     	D)A+/+;+;+=   ? 	BJ$DMb1A4(""#45 
	 	
 	 	s$   AB2B&B2&B/	+B22B;c                 
   | j                   j                  d       | j                  d| j                               5 }|| _         |j	                  t
        d      j                          |j                  }|j	                  t
        t                     }|j                  d       | j                  t              5  |j                          d d d        d d d        j                          y # 1 sw Y   "xY w# 1 sw Y   &xY w)NTrR   rg   rh   *   F)r_   r[   rl   rm   rn   r}   rs   _executor_manager_threadrE   rr   r   join)r2   r_   executor_managerr`   s       r   test_shutdown_deadlock_picklez2ExecutorDeadlockTest.test_shutdown_deadlock_pickle   s     	D)A+/+;+;+=   ? 	BJ$DM
 OOB#**,'@@ MO4A5)""=1 
	& 	 	 	s$   A=C9;C-C9-C6	2C99Dc           	      v   | j                   j                  d       dt        j                  z  }| j	                  d| j                               5 }|| _         | j                  t              5  t        |j                  t        |gdz               d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)NTrR   arg   rh   
   )r_   r[   r   PIPE_MAX_SIZErl   rm   rr   r   listmapr   )r2   r   r_   s      r   test_crash_big_dataz(ExecutorDeadlockTest.test_crash_big_data   s    
 	D)W***A+/+;+;+=   ? 	BBJ$DM""#45 BX\\"2TFRK@AB	B 	BB B	B 	Bs$   B/.$B#B/#B,	(B//B8c                    | j                   j                  d       t        t        d      st	        j
                  d      d }t        j                  j                  j                  fd} G d dt              }t        j                  j                  j                  t        j                  j                  d	|      5  t        j                  j                  d
|      5  | j                  d| j                               5 }|| _         d}t!        |      }t        j                  t        j"                  |      }	 t        j$                  t'        | j(                               | j+                  |t-        t/        |j1                  t&        |                         t        j$                  d       t        j                  t        j"                  |       	 d d d        d d d        d d d        y # t        j$                  d       t        j                  t        j"                  |       w xY w# 1 sw Y   XxY w# 1 sw Y   \xY w# 1 sw Y   y xY w)NTrR   alarmz1Tested platform does not support the alarm signalc                 @    dd l }|j                          t        d      )Nr   z timed out while submitting jobs?)r   rU   r   )_signum_framer   s      r   rk   z[ExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.timeout  s    '')ABBr   c                 @    t        j                  d        |        y )N   )r   r   )r2   
thread_runs    r   mock_runz\ExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.mock_run  s    JJqMtr   c                   6     e Zd ZdZ fdZ fdZ fdZ xZS )^ExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.MockWakeupz/Mock wakeup object to force the wakeup to blockc                 X    t         |           t        j                  d      | _        y )Nr	   )maxsize)super__init__queueQueue_dummy_queuer2   	__class__s    r   r   zgExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.MockWakeup.__init__  s     "$)KK$:!r   c                 \    | j                   j                  d d       t        |           y )NT)block)r   putr   wakeupr   s    r   r   zeExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.MockWakeup.wakeup  s&    !!%%d$%7 r   c                     t         |           	 	 | j                  j                          # t        j
                  $ r Y y w xY wr0   )r   clearr   
get_nowaitr   Emptyr   s    r   r   zdExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full.<locals>.MockWakeup.clear  sA    ))446 {{ s   - AA)r5   r6   r7   r8   r   r   r   __classcell__)r   s   @r   
MockWakeupr     s    A;! r   r   runz(concurrent.futures.process._ThreadWakeuprg   rh   d   r   )r_   r[   hasattrsignalunittestSkipTestr   process_ExecutorManagerThreadr   r   mockpatchobjectrl   rm   rangeSIGALRMr   intrt   assertEquallenr   r   )	r2   rk   r   r   r_   job_numjob_dataold_handlerr   s	           @r   5test_gh105829_should_not_deadlock_if_wakeup_pipe_fullzJExecutorDeadlockTest.test_gh105829_should_not_deadlock_if_wakeup_pipe_full   s    	D)vw'##CE E	C __;;??
	
	 	$ mm!!(()O)O).: 	?mm!!"L",.	? ##/3/?/?/A $ C ?FN ( > %mmFNNGD?LLT\\!23$$Wc$x||C7R2S.TULLOMM&..+>'?		? 	? 	?, LLOMM&..+>'? ?		? 	? 	? 	?sU   !I""H;9H/>A G19H/H; I1;H,	,H//H84H;;I	 IIN)r5   r6   r7   r   LONG_TIMEOUTrt   rc   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   rM   rM   ]   sw    ""G1& >C %.>
CDD5-DN
F
J
.O

 4BB?r   rM   )executor_mixinsc                      t                y r0   )r   r    r   r   setUpModuler   E  s    Nr   __main__r0   )(rp   r   r   r   r   r   unittest.mockrG   r   
concurrentr   concurrent.futures.processr   r   testr   utilr
   r   r   r   r   r   r   r!   r$   r)   r,   r   r.   r:   r>   rB   rE   rI   rM   globalsr   r5   mainr    r   r   <module>r      s       
       G M M




F f 6 V /F /?f ?`? `?F gi!5';'A'<'>?
 zHMMO r   