Skip to content

Commit

Permalink
fix: Incorrect timeout warnings in AWS Lambda and GCP integrations (#854
Browse files Browse the repository at this point in the history
)

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>
  • Loading branch information
3 people committed Oct 14, 2020
1 parent b36c548 commit e12a350
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion sentry_sdk/integrations/aws_lambda.py
Expand Up @@ -83,6 +83,8 @@ def sentry_handler(event, context, *args, **kwargs):
_make_request_event_processor(event, context, configured_time)
)
scope.set_tag("aws_region", context.invoked_function_arn.split(":")[3])

timeout_thread = None
# Starting the Timeout thread only if the configured time is greater than Timeout warning
# buffer and timeout_warning parameter is set True.
if (
Expand All @@ -94,7 +96,8 @@ def sentry_handler(event, context, *args, **kwargs):
) / MILLIS_TO_SECONDS

timeout_thread = TimeoutThread(
waiting_time, configured_time / MILLIS_TO_SECONDS
waiting_time,
configured_time / MILLIS_TO_SECONDS,
)

# Starting the thread to raise timeout warning exception
Expand All @@ -116,6 +119,9 @@ def sentry_handler(event, context, *args, **kwargs):
)
hub.capture_event(event, hint=hint)
reraise(*exc_info)
finally:
if timeout_thread:
timeout_thread.stop()

return sentry_handler # type: ignore

Expand Down
3 changes: 3 additions & 0 deletions sentry_sdk/integrations/gcp.py
Expand Up @@ -63,6 +63,7 @@ def sentry_func(functionhandler, event, *args, **kwargs):
_make_request_event_processor(event, configured_time, initial_time)
)
scope.set_tag("gcp_region", environ.get("FUNCTION_REGION"))
timeout_thread = None
if (
integration.timeout_warning
and configured_time > TIMEOUT_WARNING_BUFFER
Expand Down Expand Up @@ -93,6 +94,8 @@ def sentry_func(functionhandler, event, *args, **kwargs):
hub.capture_event(event, hint=hint)
reraise(*exc_info)
finally:
if timeout_thread:
timeout_thread.stop()
# Flush out the event queue
hub.flush()

Expand Down
11 changes: 9 additions & 2 deletions sentry_sdk/utils.py
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import sys
import time
import threading

from datetime import datetime
Expand Down Expand Up @@ -891,11 +890,19 @@ def __init__(self, waiting_time, configured_timeout):
threading.Thread.__init__(self)
self.waiting_time = waiting_time
self.configured_timeout = configured_timeout
self._stop_event = threading.Event()

def stop(self):
# type: () -> None
self._stop_event.set()

def run(self):
# type: () -> None

time.sleep(self.waiting_time)
self._stop_event.wait(self.waiting_time)

if self._stop_event.is_set():
return

integer_configured_timeout = int(self.configured_timeout)

Expand Down

0 comments on commit e12a350

Please sign in to comment.