Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect timeout warnings in AWS Lambda and GCP integrations #854

Merged
merged 6 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@untitaker In this "finally" block, the code to stop the thread basically works in case of :-

  1. A successful run of lambda function.
  2. When an exception is there in the code.
    But, it doesn't stop the thread in case of timeout, which is an expected behavior.

By "stop the thread" I mean explicitly stopping a thread by sending a stop signal.

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