(is_main_process=True, max_retries=10, port_range=(10000, 20000))
| 38 | return s.connect_ex((host, port)) == 0 |
| 39 | |
| 40 | def setup_debug(is_main_process=True, max_retries=10, port_range=(10000, 20000)): |
| 41 | if is_main_process: |
| 42 | host = os.environ['SLURM_NODELIST'].split(',')[0] |
| 43 | |
| 44 | for _ in range(max_retries): |
| 45 | port = random.randint(*port_range) |
| 46 | try: |
| 47 | if is_port_in_use(host, port): |
| 48 | print(f"Port {port} is already in use, trying another...") |
| 49 | continue |
| 50 | |
| 51 | # 更新 launch.json |
| 52 | update_vscode_launch_file(host, port) |
| 53 | |
| 54 | print("master_addr = ", host) |
| 55 | debugpy.listen((host, port)) |
| 56 | print(f"Waiting for debugger attach at port {port}...", flush=True) |
| 57 | debugpy.wait_for_client() |
| 58 | print("Debugger attached", flush=True) |
| 59 | return |
| 60 | except Exception as e: |
| 61 | print(f"Failed to bind to port {port}: {e}") |
| 62 | |
| 63 | raise RuntimeError("Could not find a free port for debugpy after several attempts.") |
nothing calls this directly
no test coverage detected