Skip to content

Commit 660be15

Browse files
committed
Make sure we do not close already closed transactions
1 parent c32c202 commit 660be15

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

sentry_sdk/integrations/wsgi.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,11 @@ def __call__(self, environ, start_response):
119119
origin=self.span_origin,
120120
)
121121

122-
if transaction is not None:
123-
transaction = sentry_sdk.start_transaction(
124-
transaction,
125-
custom_sampling_context={"wsgi_environ": environ},
126-
)
127-
transaction.__enter__()
122+
transaction = sentry_sdk.start_transaction(
123+
transaction,
124+
custom_sampling_context={"wsgi_environ": environ},
125+
)
126+
transaction.__enter__()
128127

129128
try:
130129
response = self.app(
@@ -272,7 +271,7 @@ def __iter__(self):
272271
# Close the Sentry transaction (it could be that response.close() is never called by the framework)
273272
# This is done here to make sure the Transaction stays
274273
# open until all streaming responses are done.
275-
if self._transaction is not None:
274+
if self._transaction is not None and hasattr(self._transaction, "_context_manager_state"):
276275
self._transaction.__exit__(None, None, None)
277276

278277
def close(self):
@@ -285,7 +284,7 @@ def close(self):
285284
# Close the Sentry transaction
286285
# This is done here to make sure the Transaction stays
287286
# open until all streaming responses are done.
288-
if self._transaction is not None:
287+
if self._transaction is not None and hasattr(self._transaction, "_context_manager_state"):
289288
self._transaction.__exit__(None, None, None)
290289
except AttributeError:
291290
pass

0 commit comments

Comments
 (0)