11from functools import wraps
22
3- from concurrent .futures import ThreadPoolExecutor
3+ from concurrent .futures import ThreadPoolExecutor , Future
44
55import sentry_sdk
66from sentry_sdk .integrations import Integration
99from typing import TYPE_CHECKING
1010
1111if TYPE_CHECKING :
12- from typing import Any
13- from typing import Callable
12+ from typing import Any , Callable , TypeVar
13+
14+ T = TypeVar ("T" , bound = Any )
1415
1516
1617class ConcurrentIntegration (Integration ):
@@ -23,24 +24,31 @@ def __init__(self, record_exceptions_on_futures=True):
2324 @staticmethod
2425 def setup_once ():
2526 # type: () -> None
26- old_submit = ThreadPoolExecutor .submit
27+ ThreadPoolExecutor .submit = _wrap_submit_call (ThreadPoolExecutor .submit )
28+
29+
30+ def _wrap_submit_call (func ):
31+ # type: (Any) -> Any
32+ """
33+ Wrap task call with a try catch to get exceptions.
34+ """
2735
28- @wraps (old_submit )
29- def sentry_submit (self , fn , * args , ** kwargs ):
30- # type: (ThreadPoolExecutor, Callable, *Any, **Any) -> Any
31- integration = sentry_sdk .get_client ().get_integration (ConcurrentIntegration )
32- if integration is None :
33- return old_submit (self , fn , * args , ** kwargs )
36+ @wraps (func )
37+ def sentry_submit (self , fn , * args , ** kwargs ):
38+ # type: (ThreadPoolExecutor, Callable[..., T], *Any, **Any) -> Future[T]
39+ integration = sentry_sdk .get_client ().get_integration (ConcurrentIntegration )
40+ if integration is None :
41+ return func (self , fn , * args , ** kwargs )
3442
35- isolation_scope = sentry_sdk .get_isolation_scope ().fork ()
36- current_scope = sentry_sdk .get_current_scope ().fork ()
43+ isolation_scope = sentry_sdk .get_isolation_scope ().fork ()
44+ current_scope = sentry_sdk .get_current_scope ().fork ()
3745
38- def wrapped_fn (* args , ** kwargs ):
39- # type: (*Any, **Any) -> Any
40- with use_isolation_scope (isolation_scope ):
41- with use_scope (current_scope ):
42- return fn (* args , ** kwargs )
46+ def wrapped_fn (* args , ** kwargs ):
47+ # type: (*Any, **Any) -> Any
48+ with use_isolation_scope (isolation_scope ):
49+ with use_scope (current_scope ):
50+ return fn (* args , ** kwargs )
4351
44- return old_submit (self , wrapped_fn , * args , ** kwargs )
52+ return func (self , wrapped_fn , * args , ** kwargs )
4553
46- ThreadPoolExecutor . submit = sentry_submit
54+ return sentry_submit
0 commit comments