From b517d85c791beb7734c71726b587d7ac985382e6 Mon Sep 17 00:00:00 2001 From: MEPalma <64580864+MEPalma@users.noreply.github.com> Date: Tue, 8 Jul 2025 16:14:32 +0200 Subject: [PATCH 1/3] update python wait for debug client --- .../.vscode/launch.json | 2 +- .../handler.py | 39 +++++++++++-------- .../base-concurrent-lambda-debug-mode/run.sh | 1 + .../base-enable-lambda-debug-mode/handler.py | 38 ++++++++++-------- .../base-enable-lambda-debug-mode/run.sh | 1 + .../handler_function_one.py | 37 +++++++++++------- .../handler_function_two.py | 38 +++++++++++------- .../base-multiple-lambda-debug-mode/run.sh | 1 + 8 files changed, 93 insertions(+), 64 deletions(-) diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json index 9461889..e07fc22 100644 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json @@ -7,7 +7,7 @@ "request": "attach", "connect": { "host": "localhost", - "port": 19891 + "port": 19891 }, "pathMappings": [ { diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py index 0a52c0b..6ae7dc5 100644 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py @@ -1,9 +1,5 @@ def handler(event, context): """Lambda handler that will get invoked by the LocalStack runtime""" - - # Wait for the debugger to get attached. - wait_for_debug_client() - # Print the incoming invocation event. print(event) @@ -11,25 +7,34 @@ def handler(event, context): return event -def wait_for_debug_client(timeout=3600): - """Utility function to enable debugging with Visual Studio Code""" +def wait_for_debug_client(port: int=19891, timeout: int=3600): import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) import debugpy - debugpy.listen(("0.0.0.0", 19891)) - class T(threading.Thread): - daemon = True - def run(self): + if not hasattr(wait_for_debug_client, "_debugpy_listening"): + wait_for_debug_client._debugpy_listening = False + + if not wait_for_debug_client._debugpy_listening: + try: + debugpy.listen(("0.0.0.0", port)) + wait_for_debug_client._debugpy_listening = True + print(f"debugpy is now listening on port {port}") + except RuntimeError as e: + print(f"debugpy.listen() failed or already active: {e}") + + if not debugpy.is_client_connected(): + print("Waiting for client to attach debugger...") + + def cancel_wait(): time.sleep(timeout) - print("Canceling debug wait task ...") + print("Canceling debug wait task after timeout...") debugpy.wait_for_client.cancel() - T().start() - print("Waiting for client to attach debugger ...") - debugpy.wait_for_client() - -if __name__ == "__main__": - handler({}, {}) + threading.Thread(target=cancel_wait, daemon=True).start() + debugpy.wait_for_client() + else: + print("Debugger already attached.") +wait_for_debug_client() \ No newline at end of file diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh index b27e10f..8b07c0b 100755 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh @@ -22,6 +22,7 @@ for i in {1..3}; do echo "Invoking the Lambda function, attempt $i." AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ + --cli-binary-format raw-in-base64-out \ --cli-connect-timeout 3600 \ --cli-read-timeout 3600 \ --function-name "$FUNCTION_NAME" \ diff --git a/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py b/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py index 0a52c0b..77b2693 100644 --- a/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py +++ b/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py @@ -1,9 +1,6 @@ def handler(event, context): """Lambda handler that will get invoked by the LocalStack runtime""" - # Wait for the debugger to get attached. - wait_for_debug_client() - # Print the incoming invocation event. print(event) @@ -11,25 +8,34 @@ def handler(event, context): return event -def wait_for_debug_client(timeout=3600): - """Utility function to enable debugging with Visual Studio Code""" +def wait_for_debug_client(port: int=19891, timeout: int=3600): import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) import debugpy - debugpy.listen(("0.0.0.0", 19891)) - class T(threading.Thread): - daemon = True - def run(self): + if not hasattr(wait_for_debug_client, "_debugpy_listening"): + wait_for_debug_client._debugpy_listening = False + + if not wait_for_debug_client._debugpy_listening: + try: + debugpy.listen(("0.0.0.0", port)) + wait_for_debug_client._debugpy_listening = True + print(f"debugpy is now listening on port {port}") + except RuntimeError as e: + print(f"debugpy.listen() failed or already active: {e}") + + if not debugpy.is_client_connected(): + print("Waiting for client to attach debugger...") + + def cancel_wait(): time.sleep(timeout) - print("Canceling debug wait task ...") + print("Canceling debug wait task after timeout...") debugpy.wait_for_client.cancel() - T().start() - print("Waiting for client to attach debugger ...") - debugpy.wait_for_client() - -if __name__ == "__main__": - handler({}, {}) + threading.Thread(target=cancel_wait, daemon=True).start() + debugpy.wait_for_client() + else: + print("Debugger already attached.") +wait_for_debug_client() diff --git a/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh index f4c03aa..f809cc5 100755 --- a/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh @@ -21,6 +21,7 @@ echo "Set a breakpoint and attach the Python remote debugger from your IDE" echo "Invoking the Lambda function." AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ + --cli-binary-format raw-in-base64-out \ --cli-connect-timeout 3600 \ --cli-read-timeout 3600 \ --function-name "$FUNCTION_NAME" \ diff --git a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py index fff0b39..2d69828 100644 --- a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py +++ b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py @@ -1,9 +1,6 @@ def handler(event, context): """Lambda handler that will get invoked by the LocalStack runtime""" - # Wait for the debugger to get attached. - wait_for_debug_client() - # Print a message to log that this the handler of handler_function_one.py file. print("The handler of handler_function_one.py is evaluating.") @@ -14,25 +11,35 @@ def handler(event, context): return event -def wait_for_debug_client(timeout=3600): - """Utility function to enable debugging with Visual Studio Code""" +def wait_for_debug_client(port: int=19891, timeout: int=3600): import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) import debugpy - debugpy.listen(("0.0.0.0", 19891)) - class T(threading.Thread): - daemon = True - def run(self): + if not hasattr(wait_for_debug_client, "_debugpy_listening"): + wait_for_debug_client._debugpy_listening = False + + if not wait_for_debug_client._debugpy_listening: + try: + debugpy.listen(("0.0.0.0", port)) + wait_for_debug_client._debugpy_listening = True + print(f"debugpy is now listening on port {port}") + except RuntimeError as e: + print(f"debugpy.listen() failed or already active: {e}") + + if not debugpy.is_client_connected(): + print("Waiting for client to attach debugger...") + + def cancel_wait(): time.sleep(timeout) - print("Canceling debug wait task ...") + print("Canceling debug wait task after timeout...") debugpy.wait_for_client.cancel() - T().start() - print("Waiting for client to attach debugger ...") - debugpy.wait_for_client() + threading.Thread(target=cancel_wait, daemon=True).start() + debugpy.wait_for_client() + else: + print("Debugger already attached.") -if __name__ == "__main__": - handler({}, {}) +wait_for_debug_client() \ No newline at end of file diff --git a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_two.py b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_two.py index 57481f1..07b7a63 100644 --- a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_two.py +++ b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_two.py @@ -1,9 +1,6 @@ def handler(event, context): """Lambda handler that will get invoked by the LocalStack runtime""" - # Wait for the debugger to get attached. - wait_for_debug_client() - # Print a message to log that this the handler of handler_function_two.py file. print("The handler of handler_function_two.py is evaluating.") @@ -14,24 +11,35 @@ def handler(event, context): return event -def wait_for_debug_client(timeout=3600): - """Utility function to enable debugging with Visual Studio Code""" +def wait_for_debug_client(port: int=19892, timeout: int=3600): import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) import debugpy - debugpy.listen(("0.0.0.0", 19892)) - class T(threading.Thread): - daemon = True - def run(self): + if not hasattr(wait_for_debug_client, "_debugpy_listening"): + wait_for_debug_client._debugpy_listening = False + + if not wait_for_debug_client._debugpy_listening: + try: + debugpy.listen(("0.0.0.0", port)) + wait_for_debug_client._debugpy_listening = True + print(f"debugpy is now listening on port {port}") + except RuntimeError as e: + print(f"debugpy.listen() failed or already active: {e}") + + if not debugpy.is_client_connected(): + print("Waiting for client to attach debugger...") + + def cancel_wait(): time.sleep(timeout) - print("Canceling debug wait task ...") + print("Canceling debug wait task after timeout...") debugpy.wait_for_client.cancel() - T().start() - print("Waiting for client to attach debugger ...") - debugpy.wait_for_client() + + threading.Thread(target=cancel_wait, daemon=True).start() + debugpy.wait_for_client() + else: + print("Debugger already attached.") -if __name__ == "__main__": - handler({}, {}) +wait_for_debug_client() \ No newline at end of file diff --git a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh index f9a3c49..50e7f3d 100755 --- a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh @@ -38,6 +38,7 @@ for function_name in "${FUNCTION_NAMES[@]}"; do AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ --cli-connect-timeout 3600 \ + --cli-binary-format raw-in-base64-out \ --cli-read-timeout 3600 \ --function-name "$function_name" \ --payload '{"message": "Testing Lambda Debug Mode lifting the 1-second timeout for '"$function_name"'. "}' \ From 991a53ba5f2b5d1110abe37e18262096c24e7e30 Mon Sep 17 00:00:00 2001 From: MEPalma <64580864+MEPalma@users.noreply.github.com> Date: Tue, 8 Jul 2025 16:28:00 +0200 Subject: [PATCH 2/3] minor --- .../base-concurrent-lambda-debug-mode/.vscode/launch.json | 2 +- .../python/base-concurrent-lambda-debug-mode/handler.py | 2 ++ .../lambda_debug_mode_config.yaml | 1 - .../python/base-enable-lambda-debug-mode/handler.py | 2 ++ .../base-enable-lambda-debug-mode/lambda_debug_mode_config.yaml | 1 - .../base-multiple-lambda-debug-mode/handler_function_one.py | 2 ++ 6 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json index e07fc22..9461889 100644 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json @@ -7,7 +7,7 @@ "request": "attach", "connect": { "host": "localhost", - "port": 19891 + "port": 19891 }, "pathMappings": [ { diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py index 6ae7dc5..7a36820 100644 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/handler.py @@ -8,6 +8,8 @@ def handler(event, context): def wait_for_debug_client(port: int=19891, timeout: int=3600): + """Utility function to enable debugging with Visual Studio Code""" + import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/lambda_debug_mode_config.yaml b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/lambda_debug_mode_config.yaml index faddc97..88a2448 100644 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/lambda_debug_mode_config.yaml +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/lambda_debug_mode_config.yaml @@ -1,4 +1,3 @@ functions: arn:aws:lambda:us-east-1:000000000000:function:function-one: debug-port: 19891 - diff --git a/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py b/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py index 77b2693..c0688e1 100644 --- a/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py +++ b/lambda-debug-mode/python/base-enable-lambda-debug-mode/handler.py @@ -9,6 +9,8 @@ def handler(event, context): def wait_for_debug_client(port: int=19891, timeout: int=3600): + """Utility function to enable debugging with Visual Studio Code""" + import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) diff --git a/lambda-debug-mode/python/base-enable-lambda-debug-mode/lambda_debug_mode_config.yaml b/lambda-debug-mode/python/base-enable-lambda-debug-mode/lambda_debug_mode_config.yaml index faddc97..88a2448 100644 --- a/lambda-debug-mode/python/base-enable-lambda-debug-mode/lambda_debug_mode_config.yaml +++ b/lambda-debug-mode/python/base-enable-lambda-debug-mode/lambda_debug_mode_config.yaml @@ -1,4 +1,3 @@ functions: arn:aws:lambda:us-east-1:000000000000:function:function-one: debug-port: 19891 - diff --git a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py index 2d69828..e086d2a 100644 --- a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py +++ b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_one.py @@ -12,6 +12,8 @@ def handler(event, context): def wait_for_debug_client(port: int=19891, timeout: int=3600): + """Utility function to enable debugging with Visual Studio Code""" + import time, threading import sys, glob sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0]) From 2f89d76c244c54e27c67b7d4a5bd92783e5bbc15 Mon Sep 17 00:00:00 2001 From: MEPalma <64580864+MEPalma@users.noreply.github.com> Date: Tue, 22 Jul 2025 18:04:06 +0200 Subject: [PATCH 3/3] remove v2 flag --- .../python/base-concurrent-lambda-debug-mode/run.sh | 1 - lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh | 1 - lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh | 1 - 3 files changed, 3 deletions(-) diff --git a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh index 8b07c0b..b27e10f 100755 --- a/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh @@ -22,7 +22,6 @@ for i in {1..3}; do echo "Invoking the Lambda function, attempt $i." AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ - --cli-binary-format raw-in-base64-out \ --cli-connect-timeout 3600 \ --cli-read-timeout 3600 \ --function-name "$FUNCTION_NAME" \ diff --git a/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh index f809cc5..f4c03aa 100755 --- a/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh @@ -21,7 +21,6 @@ echo "Set a breakpoint and attach the Python remote debugger from your IDE" echo "Invoking the Lambda function." AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ - --cli-binary-format raw-in-base64-out \ --cli-connect-timeout 3600 \ --cli-read-timeout 3600 \ --function-name "$FUNCTION_NAME" \ diff --git a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh index 50e7f3d..f9a3c49 100755 --- a/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh +++ b/lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh @@ -38,7 +38,6 @@ for function_name in "${FUNCTION_NAMES[@]}"; do AWS_MAX_ATTEMPTS=1 \ awslocal lambda invoke \ --cli-connect-timeout 3600 \ - --cli-binary-format raw-in-base64-out \ --cli-read-timeout 3600 \ --function-name "$function_name" \ --payload '{"message": "Testing Lambda Debug Mode lifting the 1-second timeout for '"$function_name"'. "}' \