8000 fix: Incorrect timeout warnings in AWS Lambda and GCP integrations (#… · puzzle/sentry-python@e12a350 · GitHub
[go: up one dir, main page]

Skip to content

Commit e12a350

Browse files
shantanu73Shantanu  Dhimanrhcarvalho
authored
fix: Incorrect timeout warnings in AWS Lambda and GCP integrations (getsentry#854)
1) Added code to stop thread in aws_lambda.py & gcp.py. 2) Modified logic of run() function of class TimeoutThread to remove the dependency on time.sleep() and to stop the thread either when the original handler returns (by calling the stop method) or the timeout is reached, conditionally raising ServerlessTimeoutWarning. Co-authored-by: Shantanu Dhiman <shantanu.dhiman@calsoftinc.com> Co-authored-by: Rodolfo Carvalho <rodolfo.carvalho@sentry.io>
1 parent b36c548 commit e12a350

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def sentry_handler(event, context, *args, **kwargs):
8383
_make_request_event_processor(event, context, configured_time)
8484
)
8585
scope.set_tag("aws_region", context.invoked_function_arn.split(":")[3])
86+
87+
timeout_thread = None
8688
# Starting the Timeout thread only if the configured time is greater than Timeout warning
8789
# buffer and timeout_warning parameter is set True.
8890
if (
@@ -94,7 +96,8 @@ def sentry_handler(event, context, *args, **kwargs):
9496
) / MILLIS_TO_SECONDS
9597

9698
timeout_thread = TimeoutThread(
97-
waiting_time, configured_time / MILLIS_TO_SECONDS
99+
waiting_time,
100+
configured_time / MILLIS_TO_SECONDS,
98101
)
99102

100103
# Starting the thread to raise timeout warning exception
@@ -116,6 +119,9 @@ def sentry_handler(event, context, *args, **kwargs):
116119
)
117120
hub.capture_event(event, hint=hint)
118121
reraise(*exc_info)
122+
finally:
123+
if timeout_thread:
124+
timeout_thread.stop()
119125

120126
return sentry_handler # type: ignore
121127

sentry_sdk/integrations/gcp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def sentry_func(functionhandler, event, *args, **kwargs):
6363
_make_request_event_processor(event, configured_time, initial_time)
6464
)
6565
scope.set_tag("gcp_region", environ.get("FUNCTION_REGION"))
66+
timeout_thread = None
6667
if (
6768
integration.timeout_warning
6869
and configured_time > TIMEOUT_WARNING_BUFFER
@@ -93,6 +94,8 @@ def sentry_func(functionhandler, event, *args, **kwargs):
9394
hub.capture_event(event, hint=hint)
9495
reraise(*exc_info)
9596
finally:
97+
if timeout_thread:
98+
timeout_thread.stop()
9699
# Flush out the event queue
97100
hub.flush()
98101

sentry_sdk/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import os
55
import sys
6-
import time
76
import threading
87

98
from datetime import datetime
@@ -891,11 +890,19 @@ def __init__(self, waiting_time, configured_timeout):
891890
threading.Thread.__init__(self)
892891
self.waiting_time = waiting_time
893892
self.configured_timeout = configured_timeout
893+
self._stop_event = threading.Event()
894+
895+
def stop(self):
896+
# type: () -> None
897+
self._stop_event.set()
894898

895899
def run(self):
896900
# type: () -> None
897901

898-
time.sleep(self.waiting_time)
902+
self._stop_event.wait(self.waiting_time)
903+
904+
if self._stop_event.is_set():
905+
return
899906

900907
integer_configured_timeout = int(self.configured_timeout)
901908

0 commit comments

Comments
 (0)
0