Skip to content

Commit

Permalink
Closing the transaction always.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Nov 19, 2024
1 parent d904f38 commit a614fc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def _request_started(app, **kwargs):


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

Expand Down
31 changes: 17 additions & 14 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,24 @@ def __iter__(self):
# type: () -> Iterator[bytes]
iterator = iter(self._response)

while True:
with use_isolation_scope(self._isolation_scope):
with use_scope(self._current_scope):
try:
chunk = next(iterator)
except StopIteration:
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
# This is done here to make sure the Transaction stays
# open until all streaming responses are done.
finish_running_transaction()
break
except BaseException:
reraise(*_capture_exception())
try:
while True:
with use_isolation_scope(self._isolation_scope):
with use_scope(self._current_scope):
try:
chunk = next(iterator)
except StopIteration:
break
except BaseException:
reraise(*_capture_exception())

yield chunk

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

def close(self):
# type: () -> None
Expand Down
6 changes: 3 additions & 3 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,9 @@ def get_current_span(scope=None):
from sentry_sdk.tracing import Span


def finish_running_transaction():
# type: () -> None
current_scope = sentry_sdk.get_current_scope()
def finish_running_transaction(scope):
# type: (Optional[sentry_sdk.Scope]) -> None
current_scope = scope or sentry_sdk.get_current_scope()
if current_scope.transaction is not None and hasattr(
current_scope.transaction, "_context_manager_state"
):
Expand Down

0 comments on commit a614fc6

Please sign in to comment.