Skip to content

Commit 58535a3

Browse files
fix(aws): Inject scopes in TimeoutThread exception with AWS lambda
1 parent cab17a4 commit 58535a3

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
138138
timeout_thread = TimeoutThread(
139139
waiting_time,
140140
configured_time / MILLIS_TO_SECONDS,
141+
isolation_scope=scope,
142+
current_scope=sentry_sdk.get_current_scope(),
141143
)
142144

143145
# Starting the thread to raise timeout warning exception

sentry_sdk/utils.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,11 +1484,17 @@ class TimeoutThread(threading.Thread):
14841484
waiting_time and raises a custom ServerlessTimeout exception.
14851485
"""
14861486

1487-
def __init__(self, waiting_time, configured_timeout):
1488-
# type: (float, int) -> None
1487+
def __init__(
1488+
self, waiting_time, configured_timeout, isolation_scope=None, current_scope=None
1489+
):
1490+
# type: (float, int, Optional[Scope], Optional[Scope]) -> None
14891491
threading.Thread.__init__(self)
14901492
self.waiting_time = waiting_time
14911493
self.configured_timeout = configured_timeout
1494+
1495+
self.isolation_scope = isolation_scope
1496+
self.current_scope = current_scope
1497+
14921498
self._stop_event = threading.Event()
14931499

14941500
def stop(self):
@@ -1509,12 +1515,17 @@ def run(self):
15091515
if integer_configured_timeout < self.configured_timeout:
15101516
integer_configured_timeout = integer_configured_timeout + 1
15111517

1512-
# Raising Exception after timeout duration is reached
1513-
raise ServerlessTimeoutWarning(
1514-
"WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format(
1515-
integer_configured_timeout
1516-
)
1517-
)
1518+
from sentry_sdk.scope import use_isolation_scope, use_scope
1519+
1520+
with use_isolation_scope(self.isolation_scope):
1521+
with use_scope(self.current_scope):
1522+
# with use_scope(self.current_scope):
1523+
# Raising Exception after timeout duration is reached
1524+
raise ServerlessTimeoutWarning(
1525+
"WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format(
1526+
integer_configured_timeout
1527+
)
1528+
)
15181529

15191530

15201531
def to_base64(original):

0 commit comments

Comments
 (0)