Skip to content

Commit 15eb914

Browse files
committed
Cancel timer when transaction already finished
1 parent 2e69500 commit 15eb914

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

sentry_sdk/integrations/wsgi.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def __call__(self, environ, start_response):
124124
origin=self.span_origin,
125125
)
126126

127+
timer = None
127128
if transaction is not None:
128129
sentry_sdk.start_transaction(
129130
transaction,
@@ -147,7 +148,7 @@ def __call__(self, environ, start_response):
147148
except BaseException:
148149
exc_info = sys.exc_info()
149150
_capture_exception(exc_info)
150-
finish_running_transaction(current_scope, exc_info)
151+
finish_running_transaction(current_scope, exc_info, timer)
151152
reraise(*exc_info)
152153

153154
finally:
@@ -157,6 +158,7 @@ def __call__(self, environ, start_response):
157158
response=response,
158159
current_scope=current_scope,
159160
isolation_scope=scope,
161+
timer=timer,
160162
)
161163

162164

@@ -271,18 +273,20 @@ class _ScopedResponse:
271273
- WSGI servers streaming responses interleaved from the same thread
272274
"""
273275

274-
__slots__ = ("_response", "_current_scope", "_isolation_scope")
276+
__slots__ = ("_response", "_current_scope", "_isolation_scope", "_timer")
275277

276278
def __init__(
277279
self,
278280
response, # type: Iterator[bytes]
279281
current_scope, # type: sentry_sdk.scope.Scope
280282
isolation_scope, # type: sentry_sdk.scope.Scope
283+
timer=None, # type: Optional[Timer]
281284
):
282285
# type: (...) -> None
283286
self._response = response
284287
self._current_scope = current_scope
285288
self._isolation_scope = isolation_scope
289+
self._timer = timer
286290

287291
def __iter__(self):
288292
# type: () -> Iterator[bytes]
@@ -304,14 +308,14 @@ def __iter__(self):
304308
finally:
305309
with use_isolation_scope(self._isolation_scope):
306310
with use_scope(self._current_scope):
307-
finish_running_transaction()
311+
finish_running_transaction(timer=self._timer)
308312

309313
def close(self):
310314
# type: () -> None
311315
with use_isolation_scope(self._isolation_scope):
312316
with use_scope(self._current_scope):
313317
try:
314-
finish_running_transaction()
318+
finish_running_transaction(timer=self._timer)
315319
self._response.close() # type: ignore
316320
except AttributeError:
317321
pass

sentry_sdk/tracing_utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from types import FrameType
3838

3939
from sentry_sdk._types import ExcInfo
40+
from threading import Timer
4041

4142

4243
SENTRY_TRACE_REGEX = re.compile(
@@ -743,12 +744,15 @@ def get_current_span(scope=None):
743744
from sentry_sdk.tracing import Span
744745

745746

746-
def finish_running_transaction(scope=None, exc_info=None):
747-
# type: (Optional[sentry_sdk.Scope], Optional[ExcInfo]) -> None
747+
def finish_running_transaction(scope=None, exc_info=None, timer=None):
748+
# type: (Optional[sentry_sdk.Scope], Optional[ExcInfo], Timer) -> None
748749
current_scope = scope or sentry_sdk.get_current_scope()
749750
if current_scope.transaction is not None and hasattr(
750751
current_scope.transaction, "_context_manager_state"
751752
):
753+
if timer is not None:
754+
timer.cancel()
755+
752756
if exc_info is not None:
753757
current_scope.transaction.__exit__(*exc_info)
754758
else:

tests/integrations/wsgi/test_wsgi.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,40 @@ def long_running_app(environ, start_response):
537537
assert (
538538
transaction_duration <= new_max_duration * 1.2
539539
) # we allow 2% margin for processing the request
540+
541+
542+
def test_long_running_transaction_timer_canceled(sentry_init, capture_events):
543+
# we allow transactions to be 0.5 seconds as a maximum
544+
new_max_duration = 0.5
545+
546+
with mock.patch.object(
547+
sentry_sdk.integrations.wsgi,
548+
"MAX_TRANSACTION_DURATION_SECONDS",
549+
new_max_duration,
550+
):
551+
with mock.patch(
552+
"sentry_sdk.integrations.wsgi.finish_long_running_transaction"
553+
) as mock_finish:
554+
555+
def generate_content():
556+
# This response will take 0.3 seconds to generate
557+
for _ in range(3):
558+
time.sleep(0.1)
559+
yield "ok"
560+
561+
def long_running_app(environ, start_response):
562+
start_response("200 OK", [])
563+
return generate_content()
564+
565+
sentry_init(send_default_pii=True, traces_sample_rate=1.0)
566+
app = SentryWsgiMiddleware(long_running_app)
567+
568+
events = capture_events()
569+
570+
client = Client(app)
571+
response = client.get("/")
572+
_ = response.get_data()
573+
574+
(transaction,) = events
575+
576+
mock_finish.assert_not_called()

0 commit comments

Comments
 (0)