Skip to content

Commit b517d85

Browse files
committed
update python wait for debug client
1 parent 3324d1f commit b517d85

File tree

8 files changed

+93
-64
lines changed

8 files changed

+93
-64
lines changed

lambda-debug-mode/python/base-concurrent-lambda-debug-mode/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"request": "attach",
88
"connect": {
99
"host": "localhost",
10-
"port": 19891
10+
"port": 19891
1111
},
1212
"pathMappings": [
1313
{
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
3-
4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
73
# Print the incoming invocation event.
84
print(event)
95

106
# Return the incoming invocation event.
117
return event
128

139

14-
def wait_for_debug_client(timeout=3600):
15-
"""Utility function to enable debugging with Visual Studio Code"""
10+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1611
import time, threading
1712
import sys, glob
1813
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
1914
import debugpy
2015

21-
debugpy.listen(("0.0.0.0", 19891))
22-
class T(threading.Thread):
23-
daemon = True
24-
def run(self):
16+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
17+
wait_for_debug_client._debugpy_listening = False
18+
19+
if not wait_for_debug_client._debugpy_listening:
20+
try:
21+
debugpy.listen(("0.0.0.0", port))
22+
wait_for_debug_client._debugpy_listening = True
23+
print(f"debugpy is now listening on port {port}")
24+
except RuntimeError as e:
25+
print(f"debugpy.listen() failed or already active: {e}")
26+
27+
if not debugpy.is_client_connected():
28+
print("Waiting for client to attach debugger...")
29+
30+
def cancel_wait():
2531
time.sleep(timeout)
26-
print("Canceling debug wait task ...")
32+
print("Canceling debug wait task after timeout...")
2733
debugpy.wait_for_client.cancel()
28-
T().start()
29-
print("Waiting for client to attach debugger ...")
30-
debugpy.wait_for_client()
31-
3234

33-
if __name__ == "__main__":
34-
handler({}, {})
35+
threading.Thread(target=cancel_wait, daemon=True).start()
36+
debugpy.wait_for_client()
37+
else:
38+
print("Debugger already attached.")
3539

40+
wait_for_debug_client()

lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ for i in {1..3}; do
2222
echo "Invoking the Lambda function, attempt $i."
2323
AWS_MAX_ATTEMPTS=1 \
2424
awslocal lambda invoke \
25+
--cli-binary-format raw-in-base64-out \
2526
--cli-connect-timeout 3600 \
2627
--cli-read-timeout 3600 \
2728
--function-name "$FUNCTION_NAME" \
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print the incoming invocation event.
85
print(event)
96

107
# Return the incoming invocation event.
118
return event
129

1310

14-
def wait_for_debug_client(timeout=3600):
15-
"""Utility function to enable debugging with Visual Studio Code"""
11+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1612
import time, threading
1713
import sys, glob
1814
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
1915
import debugpy
2016

21-
debugpy.listen(("0.0.0.0", 19891))
22-
class T(threading.Thread):
23-
daemon = True
24-
def run(self):
17+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
18+
wait_for_debug_client._debugpy_listening = False
19+
20+
if not wait_for_debug_client._debugpy_listening:
21+
try:
22+
debugpy.listen(("0.0.0.0", port))
23+
wait_for_debug_client._debugpy_listening = True
24+
print(f"debugpy is now listening on port {port}")
25+
except RuntimeError as e:
26+
print(f"debugpy.listen() failed or already active: {e}")
27+
28+
if not debugpy.is_client_connected():
29+
print("Waiting for client to attach debugger...")
30+
31+
def cancel_wait():
2532
time.sleep(timeout)
26-
print("Canceling debug wait task ...")
33+
print("Canceling debug wait task after timeout...")
2734
debugpy.wait_for_client.cancel()
28-
T().start()
29-
print("Waiting for client to attach debugger ...")
30-
debugpy.wait_for_client()
31-
3235

33-
if __name__ == "__main__":
34-
handler({}, {})
36+
threading.Thread(target=cancel_wait, daemon=True).start()
37+
debugpy.wait_for_client()
38+
else:
39+
print("Debugger already attached.")
3540

41+
wait_for_debug_client()

lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ echo "Set a breakpoint and attach the Python remote debugger from your IDE"
2121
echo "Invoking the Lambda function."
2222
AWS_MAX_ATTEMPTS=1 \
2323
awslocal lambda invoke \
24+
--cli-binary-format raw-in-base64-out \
2425
--cli-connect-timeout 3600 \
2526
--cli-read-timeout 3600 \
2627
--function-name "$FUNCTION_NAME" \
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print a message to log that this the handler of handler_function_one.py file.
85
print("The handler of handler_function_one.py is evaluating.")
96

@@ -14,25 +11,35 @@ def handler(event, context):
1411
return event
1512

1613

17-
def wait_for_debug_client(timeout=3600):
18-
"""Utility function to enable debugging with Visual Studio Code"""
14+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1915
import time, threading
2016
import sys, glob
2117
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
2218
import debugpy
2319

24-
debugpy.listen(("0.0.0.0", 19891))
25-
class T(threading.Thread):
26-
daemon = True
27-
def run(self):
20+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
21+
wait_for_debug_client._debugpy_listening = False
22+
23+
if not wait_for_debug_client._debugpy_listening:
24+
try:
25+
debugpy.listen(("0.0.0.0", port))
26+
wait_for_debug_client._debugpy_listening = True
27+
print(f"debugpy is now listening on port {port}")
28+
except RuntimeError as e:
29+
print(f"debugpy.listen() failed or already active: {e}")
30+
31+
if not debugpy.is_client_connected():
32+
print("Waiting for client to attach debugger...")
33+
34+
def cancel_wait():
2835
time.sleep(timeout)
29-
print("Canceling debug wait task ...")
36+
print("Canceling debug wait task after timeout...")
3037
debugpy.wait_for_client.cancel()
31-
T().start()
32-
print("Waiting for client to attach debugger ...")
33-
debugpy.wait_for_client()
3438

39+
threading.Thread(target=cancel_wait, daemon=True).start()
40+
debugpy.wait_for_client()
41+
else:
42+
print("Debugger already attached.")
3543

36-
if __name__ == "__main__":
37-
handler({}, {})
3844

45+
wait_for_debug_client()
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print a message to log that this the handler of handler_function_two.py file.
85
print("The handler of handler_function_two.py is evaluating.")
96

@@ -14,24 +11,35 @@ def handler(event, context):
1411
return event
1512

1613

17-
def wait_for_debug_client(timeout=3600):
18-
"""Utility function to enable debugging with Visual Studio Code"""
14+
def wait_for_debug_client(port: int=19892, timeout: int=3600):
1915
import time, threading
2016
import sys, glob
2117
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
2218
import debugpy
2319

24-
debugpy.listen(("0.0.0.0", 19892))
25-
class T(threading.Thread):
26-
daemon = True
27-
def run(self):
20+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
21+
wait_for_debug_client._debugpy_listening = False
22+
23+
if not wait_for_debug_client._debugpy_listening:
24+
try:
25+
debugpy.listen(("0.0.0.0", port))
26+
wait_for_debug_client._debugpy_listening = True
27+
print(f"debugpy is now listening on port {port}")
28+
except RuntimeError as e:
29+
print(f"debugpy.listen() failed or already active: {e}")
30+
31+
if not debugpy.is_client_connected():
32+
print("Waiting for client to attach debugger...")
33+
34+
def cancel_wait():
2835
time.sleep(timeout)
29-
print("Canceling debug wait task ...")
36+
print("Canceling debug wait task after timeout...")
3037
debugpy.wait_for_client.cancel()
31-
T().start()
32-
print("Waiting for client to attach debugger ...")
33-
debugpy.wait_for_client()
38+
39+
threading.Thread(target=cancel_wait, daemon=True).start()
40+
debugpy.wait_for_client()
41+
else:
42+
print("Debugger already attached.")
3443

3544

36-
if __name__ == "__main__":
37-
handler({}, {})
45+
wait_for_debug_client()

lambda-debug-mode/python/base-multiple-lambda-debug-mode/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ for function_name in "${FUNCTION_NAMES[@]}"; do
3838
AWS_MAX_ATTEMPTS=1 \
3939
awslocal lambda invoke \
4040
--cli-connect-timeout 3600 \
41+
--cli-binary-format raw-in-base64-out \
4142
--cli-read-timeout 3600 \
4243
--function-name "$function_name" \
4344
--payload '{"message": "Testing Lambda Debug Mode lifting the 1-second timeout for '"$function_name"'. "}' \

0 commit comments

Comments
 (0)