@@ -1484,17 +1484,37 @@ 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[sentry_sdk.Scope], Optional[sentry_sdk.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 ):
14951501 # type: () -> None
14961502 self ._stop_event .set ()
14971503
1504+ def _capture_exception (self ):
1505+ # type: () -> ExcInfo
1506+ exc_info = sys .exc_info ()
1507+
1508+ client = sentry_sdk .get_client ()
1509+ event , hint = event_from_exception (
1510+ exc_info ,
1511+ client_options = client .options ,
1512+ mechanism = {"type" : "threading" , "handled" : False },
1513+ )
1514+ sentry_sdk .capture_event (event , hint = hint )
1515+
1516+ return exc_info
1517+
14981518 def run (self ):
14991519 # type: () -> None
15001520
@@ -1510,6 +1530,18 @@ def run(self):
15101530 integer_configured_timeout = integer_configured_timeout + 1
15111531
15121532 # Raising Exception after timeout duration is reached
1533+ if self .isolation_scope is not None and self .current_scope is not None :
1534+ with sentry_sdk .scope .use_isolation_scope (self .isolation_scope ):
1535+ with sentry_sdk .scope .use_scope (self .current_scope ):
1536+ try :
1537+ raise ServerlessTimeoutWarning (
1538+ "WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds." .format (
1539+ integer_configured_timeout
1540+ )
1541+ )
1542+ except Exception :
1543+ reraise (* self ._capture_exception ())
1544+
15131545 raise ServerlessTimeoutWarning (
15141546 "WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds." .format (
15151547 integer_configured_timeout
0 commit comments