|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import threading |
| 5 | +import importlib |
| 6 | + |
| 7 | +import ray |
| 8 | +from ray.util.annotations import DeveloperAPI |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | +POST_MORTEM_ERROR_UUID = "post_mortem_error_uuid" |
| 13 | + |
| 14 | + |
| 15 | +def _try_import_debugpy(): |
| 16 | + try: |
| 17 | + debugpy = importlib.import_module("debugpy") |
| 18 | + if not hasattr(debugpy, "__version__") or debugpy.__version__ < "1.8.0": |
| 19 | + raise ImportError() |
| 20 | + return debugpy |
| 21 | + except (ModuleNotFoundError, ImportError): |
| 22 | + log.error( |
| 23 | + "Module 'debugpy>=1.8.0' cannot be loaded. " |
| 24 | + "Ray Debugpy Debugger will not work without 'debugpy>=1.8.0' installed. " |
| 25 | + "Install this module using 'pip install debugpy==1.8.0' " |
| 26 | + ) |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +# A lock to ensure that only one thread can open the debugger port. |
| 31 | +debugger_port_lock = threading.Lock() |
| 32 | + |
| 33 | + |
| 34 | +def _override_breakpoint_hooks(): |
| 35 | + """ |
| 36 | + This method overrides the breakpoint() function to set_trace() |
| 37 | + so that other threads can reuse the same setup logic. |
| 38 | + This is based on: https://github.com/microsoft/debugpy/blob/ef9a67fe150179ee4df9997f9273723c26687fab/src/debugpy/_vendored/pydevd/pydev_sitecustomize/sitecustomize.py#L87 # noqa: E501 |
| 39 | + """ |
| 40 | + sys.__breakpointhook__ = set_trace |
| 41 | + sys.breakpointhook = set_trace |
| 42 | + import builtins as __builtin__ |
| 43 | + |
| 44 | + __builtin__.breakpoint = set_trace |
| 45 | + |
| 46 | + |
| 47 | +def _ensure_debugger_port_open_thread_safe(): |
| 48 | + """ |
| 49 | + This is a thread safe method that ensure that the debugger port |
| 50 | + is open, and if not, open it. |
| 51 | + """ |
| 52 | + |
| 53 | + # The lock is acquired before checking the debugger port so only |
| 54 | + # one thread can open the debugger port. |
| 55 | + with debugger_port_lock: |
| 56 | + debugpy = _try_import_debugpy() |
| 57 | + if not debugpy: |
| 58 | + return |
| 59 | + |
| 60 | + debugger_port = ray._private.worker.global_worker.debugger_port |
| 61 | + if not debugger_port: |
| 62 | + (host, port) = debugpy.listen( |
| 63 | + (ray._private.worker.global_worker.node_ip_address, 0) |
| 64 | + ) |
| 65 | + ray._private.worker.global_worker.set_debugger_port(port) |
| 66 | + log.info(f"Ray debugger is listening on {host}:{port}") |
| 67 | + else: |
| 68 | + log.info(f"Ray debugger is already open on {debugger_port}") |
| 69 | + |
| 70 | + |
| 71 | +@DeveloperAPI |
| 72 | +def set_trace(breakpoint_uuid=None): |
| 73 | + """Interrupt the flow of the program and drop into the Ray debugger. |
| 74 | + Can be used within a Ray task or actor. |
| 75 | + """ |
| 76 | + debugpy = _try_import_debugpy() |
| 77 | + if not debugpy: |
| 78 | + return |
| 79 | + |
| 80 | + _ensure_debugger_port_open_thread_safe() |
| 81 | + |
| 82 | + # debugpy overrides the breakpoint() function, so we need to set it back |
| 83 | + # so other threads can reuse it. |
| 84 | + _override_breakpoint_hooks() |
| 85 | + |
| 86 | + with ray._private.worker.global_worker.worker_paused_by_debugger(): |
| 87 | + log.info("Waiting for debugger to attach...") |
| 88 | + debugpy.wait_for_client() |
| 89 | + |
| 90 | + log.info("Debugger client is connected") |
| 91 | + if breakpoint_uuid == POST_MORTEM_ERROR_UUID: |
| 92 | + _debugpy_excepthook() |
| 93 | + else: |
| 94 | + _debugpy_breakpoint() |
| 95 | + |
| 96 | + |
| 97 | +def _debugpy_breakpoint(): |
| 98 | + """ |
| 99 | + Drop the user into the debugger on a breakpoint. |
| 100 | + """ |
| 101 | + import pydevd |
| 102 | + |
| 103 | + pydevd.settrace(stop_at_frame=sys._getframe().f_back) |
| 104 | + |
| 105 | + |
| 106 | +def _debugpy_excepthook(): |
| 107 | + """ |
| 108 | + Drop the user into the debugger on an unhandled exception. |
| 109 | + """ |
| 110 | + import threading |
| 111 | + |
| 112 | + import pydevd |
| 113 | + |
| 114 | + py_db = pydevd.get_global_debugger() |
| 115 | + thread = threading.current_thread() |
| 116 | + additional_info = py_db.set_additional_thread_info(thread) |
| 117 | + additional_info.is_tracing += 1 |
| 118 | + try: |
| 119 | + error = sys.exc_info() |
| 120 | + py_db.stop_on_unhandled_exception(py_db, thread, additional_info, error) |
| 121 | + sys.excepthook(error[0], error[1], error[2]) |
| 122 | + finally: |
| 123 | + additional_info.is_tracing -= 1 |
| 124 | + |
| 125 | + |
| 126 | +def _is_ray_debugger_enabled(): |
| 127 | + return "RAY_DEBUG" in os.environ |
| 128 | + |
| 129 | + |
| 130 | +def _post_mortem(): |
| 131 | + return set_trace(POST_MORTEM_ERROR_UUID) |
0 commit comments