diff --git a/scripts/debug_wrapper.py b/scripts/debug_wrapper.py index eb71d43d..2e96e110 100644 --- a/scripts/debug_wrapper.py +++ b/scripts/debug_wrapper.py @@ -27,7 +27,7 @@ logger.info(f"Files in current directory: {os.listdir()}") logger.info(f"Python path: {sys.path}") # Default timeout in seconds -DEBUG_TIMEOUT = int(os.environ.get('DEBUG_TIMEOUT', '30')) +DEBUG_TIMEOUT = int(os.environ.get('DEBUG_TIMEOUT', '60')) # Increased default timeout # Whether to wait for debugger to attach WAIT_FOR_DEBUGGER = os.environ.get('WAIT_FOR_DEBUGGER', 'false').lower() == 'true' @@ -40,7 +40,7 @@ try: logger.info("Successfully imported debugpy") # Critical: Configure debugpy to use regular Python for the adapter, not uwsgi - python_path = '/usr/local/bin/python3' + python_path = '/dispatcharrpy/bin/python' if os.path.exists(python_path): logger.info(f"Setting debugpy adapter to use Python interpreter: {python_path}") debugpy.configure(python=python_path) @@ -50,42 +50,37 @@ try: # Don't wait for connection, just set up the debugging session logger.info("Initializing debugpy on 0.0.0.0:5678...") try: - # Set socket timeout using the existing DEBUG_TIMEOUT variable - import socket - socket.setdefaulttimeout(DEBUG_TIMEOUT) - # Use connect instead of listen to avoid the adapter process + # Configure debugpy to listen without socket timeout initially debugpy.listen(("0.0.0.0", 5678)) logger.info("debugpy now listening on 0.0.0.0:5678") - # Reset socket timeout - socket.setdefaulttimeout(None) if WAIT_FOR_DEBUGGER: logger.info(f"Waiting for debugger to attach (timeout: {DEBUG_TIMEOUT}s)...") start_time = time.time() + + # Use a more reliable approach for checking connection while not debugpy.is_client_connected() and (time.time() - start_time < DEBUG_TIMEOUT): time.sleep(1) - logger.info("Waiting for debugger connection...") + if (time.time() - start_time) % 5 == 0: # Log only every 5 seconds to reduce spam + logger.info(f"Still waiting for debugger connection... ({int(time.time() - start_time)}s)") if debugpy.is_client_connected(): - logger.info("Debugger attached!") + logger.info("Debugger attached successfully!") else: - logger.info(f"Debugger not attached after {DEBUG_TIMEOUT}s, continuing anyway...") + logger.warning(f"Debugger not attached after {DEBUG_TIMEOUT}s, continuing anyway...") except RuntimeError as re: - if "timed out waiting for adapter to connect" in str(re): + if "already in use" in str(re): + logger.warning(f"Port 5678 already in use. This might indicate another debugging session is active.") + logger.info("Continuing without debugging...") + elif "timed out waiting for adapter to connect" in str(re): logger.warning(f"debugpy.listen timed out after {DEBUG_TIMEOUT}s. This is normal in some environments.") logger.info("Continuing without debugging...") else: logger.error(f"RuntimeError with debugpy.listen: {re}", exc_info=True) logger.info("Continuing without debugging...") - except socket.timeout: - logger.warning(f"Socket timeout after {DEBUG_TIMEOUT}s while initializing debugpy.") - logger.info("Continuing without debugging...") except Exception as e: logger.error(f"Error with debugpy.listen: {e}", exc_info=True) logger.info("Continuing without debugging...") - finally: - # Ensure socket timeout is always reset - socket.setdefaulttimeout(None) except ImportError: logger.error("debugpy not installed, continuing without debugging support")