Skip to content

Commit a614fc6

Browse files
committed
Closing the transaction always.
1 parent d904f38 commit a614fc6

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

sentry_sdk/integrations/flask.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def _request_started(app, **kwargs):
156156

157157

158158
def _request_finished(sender, response, **kwargs):
159+
# type: (Flask, Any, **Any) -> None
159160
# Manually close the transaction because Flask does not call `close()` on the WSGI response
160161
finish_running_transaction()
161162

sentry_sdk/integrations/wsgi.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,24 @@ def __iter__(self):
257257
# type: () -> Iterator[bytes]
258258
iterator = iter(self._response)
259259

260-
while True:
261-
with use_isolation_scope(self._isolation_scope):
262-
with use_scope(self._current_scope):
263-
try:
264-
chunk = next(iterator)
265-
except StopIteration:
266-
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
267-
# This is done here to make sure the Transaction stays
268-
# open until all streaming responses are done.
269-
finish_running_transaction()
270-
break
271-
except BaseException:
272-
reraise(*_capture_exception())
260+
try:
261+
while True:
262+
with use_isolation_scope(self._isolation_scope):
263+
with use_scope(self._current_scope):
264+
try:
265+
chunk = next(iterator)
266+
except StopIteration:
267+
break
268+
except BaseException:
269+
reraise(*_capture_exception())
270+
271+
yield chunk
273272

274-
yield chunk
273+
finally:
274+
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
275+
# This is done here to make sure the Transaction stays
276+
# open until all streaming responses are done.
277+
finish_running_transaction(self._current_scope)
275278

276279
def close(self):
277280
# type: () -> None

sentry_sdk/tracing_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ def get_current_span(scope=None):
726726
from sentry_sdk.tracing import Span
727727

728728

729-
def finish_running_transaction():
730-
# type: () -> None
731-
current_scope = sentry_sdk.get_current_scope()
729+
def finish_running_transaction(scope):
730+
# type: (Optional[sentry_sdk.Scope]) -> None
731+
current_scope = scope or sentry_sdk.get_current_scope()
732732
if current_scope.transaction is not None and hasattr(
733733
current_scope.transaction, "_context_manager_state"
734734
):

0 commit comments

Comments
 (0)