o
    bi-                     @   s   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
Zd dlmZ eeZdd Zdd Zd	d
 ZG dd deZG dd deZde	e defddZde	e de	e fddZG dd dejZdS )    N)closing)Optional)_ERROR_REPORT_TIMEOUTc                  C   s`   t ttjtj} | d | tjtjd |  d W  d    S 1 s)w   Y  d S )N) r      )	r   socketAF_INETSOCK_STREAMbind
setsockopt
SOL_SOCKETSO_REUSEADDRgetsockname)s r   J/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/_internal/util.pyfind_free_port   s
   

$r   c                 C   s
   t | S N)npisnanvaluer   r   r   is_nan   s   
r   c                 C   s   t | pt| S r   )r   r   isinfr   r   r   r   is_nan_or_inf   s   r   c                   @   s   e Zd ZdZdS )StartTracebackzMThese exceptions (and their tracebacks) can be skipped with `skip_exceptions`N)__name__
__module____qualname____doc__r   r   r   r   r       s    r   c                       s.   e Zd Zdeddf fddZdd Z  ZS )StartTracebackWithWorkerRankworker_rankreturnNc                    s   t    || _d S r   )super__init__r!   )selfr!   	__class__r   r   r$   '   s   

z%StartTracebackWithWorkerRank.__init__c                 C   s   | j | jffS r   )r'   r!   )r%   r   r   r   
__reduce__+   s   z'StartTracebackWithWorkerRank.__reduce__)r   r   r   intr$   r(   __classcell__r   r   r&   r   r    &   s    r    excr"   c                 C   sb   t ttjdd}|r| S t| trt| jS t		| 
| j}t| dd}|r/t||_|S )a  Skip all contained `StartTracebacks` to reduce traceback output.

    Returns a shallow copy of the exception with all `StartTracebacks` removed.

    If the RAY_AIR_FULL_TRACEBACKS environment variable is set,
    the original exception (not a copy) is returned.
    RAY_AIR_FULL_TRACEBACKS0	__cause__N)boolr)   osenvironget
isinstancer   skip_exceptionsr.   copywith_traceback__traceback__getattr)r+   should_not_shortennew_exccauser   r   r   r4   /   s   


r4   c                 C   s   | sd S t | dd S )Nr.   )r8   )r+   r   r   r   exception_causeK   s   r<   c                       s@   e Zd ZdZdd ZdefddZdd Zd fd
d	Z  Z	S )RunnerThreadz(Supervisor thread that runs your script.c                O   s*   t jj| g|R i | || _d | _d S r   )	threadingThreadr$   _error_queue_ret)r%   error_queueargskwargsr   r   r   r$   U   s   
zRunnerThread.__init__ec                 C   s:   z| j j|dtd W d S  tjy   td Y d S w )NT)blocktimeoutzRunner Thread was unable to report error to main function runner thread. This means a previous error was not processed. This should never happen.)r@   putr   queueFullloggercriticalr%   rE   r   r   r   _propagate_exceptionZ   s   
z!RunnerThread._propagate_exceptionc              
   C   s   z| j | ji | j| _W d S  ty   td Y d S  tyF } z|jdkr/td n| 	| W Y d }~d S W Y d }~d S d }~w t
y] } z| 	| W Y d }~d S d }~ww )NzfThread runner raised StopIteration. Interpreting it as a signal to terminate the thread without error.r   zuThread runner raised SystemExit with error code 0. Interpreting it as a signal to terminate the thread without error.)_target_args_kwargsrA   StopIterationrK   debug
SystemExitcoderN   BaseExceptionrM   r   r   r   runi   s$   

	
zRunnerThread.runNc                    s   t t| | | jS r   )r#   r=   joinrA   )r%   rG   r&   r   r   rX      s   zRunnerThread.joinr   )
r   r   r   r   r$   rV   rN   rW   rX   r*   r   r   r&   r   r=   R   s    r=   )r5   loggingr0   rI   r   r>   
contextlibr   typingr   numpyr   ray.air.constantsr   	getLoggerr   rK   r   r   r   	Exceptionr   r    r4   r<   r?   r=   r   r   r   r   <module>   s&    
	