o
    ci                     @   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 e e	Z
dZdd Ze Zdd Zdd	 Zedd
dZdd Zdd Zdd Zdd ZdS )    N)DeveloperAPIpost_mortem_error_uuidc               	   C   sN   zt d} t| dr| jdk rt | W S  ttfy&   td Y d S w )Ndebugpy__version__z1.8.0zModule 'debugpy>=1.8.0' cannot be loaded. Ray Debugpy Debugger will not work without 'debugpy>=1.8.0' installed. Install this module using 'pip install debugpy==1.8.0' )	importlibimport_modulehasattrr   ImportErrorModuleNotFoundErrorlogerror)r    r   D/home/ubuntu/.local/lib/python3.10/site-packages/ray/util/debugpy.py_try_import_debugpy   s   
r   c                  C   s   t t_t t_ddl} t | _dS )a>  
    This method overrides the breakpoint() function to set_trace()
    so that other threads can reuse the same setup logic.
    This is based on: https://github.com/microsoft/debugpy/blob/ef9a67fe150179ee4df9997f9273723c26687fab/src/debugpy/_vendored/pydevd/pydev_sitecustomize/sitecustomize.py#L87 # noqa: E501
    r   N)	set_tracesys__breakpointhook__breakpointhookbuiltins
breakpoint)__builtin__r   r   r   _override_breakpoint_hooks"   s   
r   c                  C   s   t P t } | s	 W d   dS tjjjj}|s:| tjjjjdf\}}tjjj	| t
d| d|  nt
d|  W d   dS W d   dS 1 sUw   Y  dS )zk
    This is a thread safe method that ensure that the debugger port
    is open, and if not, open it.
    Nr   zRay debugger is listening on :z Ray debugger is already open on )debugger_port_lockr   ray_privateworkerglobal_workerdebugger_portlistennode_ip_addressset_debugger_portr   info)r   r   hostportr   r   r   &_ensure_debugger_port_open_thread_safe/   s    "r%   c                 C   s   t  }|sdS t  t  tjjj  d}t	| |
  W d   n1 s*w   Y  t	d | tkr=t  dS t  dS )zrInterrupt the flow of the program and drop into the Ray debugger.
    Can be used within a Ray task or actor.
    NzuWaiting for debugger to attach (see https://docs.ray.io/en/latest/ray-observability/ray-distributed-debugger.html)...zDebugger client is connected)r   r%   r   r   r   r   r   worker_paused_by_debuggerr   r"   wait_for_clientPOST_MORTEM_ERROR_UUID_debugpy_excepthook_debugpy_breakpoint)breakpoint_uuidr   msgr   r   r   r   G   s   


	

r   c                  C   s   ddl } | jt jd dS )z:
    Drop the user into the debugger on a breakpoint.
    r   N)stop_at_frame)pydevdsettracer   	_getframef_back)r.   r   r   r   r*   f   s   r*   c               	   C   s   ddl } ddl}| }|  }||}| jd7  _z#t }||||| t	|d |d |d  W | jd8  _dS | jd8  _w )zD
    Drop the user into the debugger on an unhandled exception.
    r   N      )
	threadingr.   get_global_debuggercurrent_threadset_additional_thread_info
is_tracingr   exc_infostop_on_unhandled_exception
excepthook)r4   r.   py_dbthreadadditional_infor   r   r   r   r)   o   s   
"r)   c                   C   s   t jdddkS )NRAY_DEBUG_POST_MORTEM01)osenvirongetr   r   r   r   $_is_ray_debugger_post_mortem_enabled   s   rE   c                   C   s   t tS N)r   r(   r   r   r   r   _post_mortem   s   rG   rF   )loggingrB   r   r4   r   r   ray.util.annotationsr   	getLogger__name__r   r(   r   Lockr   r   r%   r   r*   r)   rE   rG   r   r   r   r   <module>   s&    
	