From ba7ea2bab4be3721fea88bd12d41c40d85093f75 Mon Sep 17 00:00:00 2001 From: Wei Mingzhi Date: Sun, 10 Nov 2024 17:09:55 +0800 Subject: [PATCH] Fix issue with flask url handler returning an iterator. If the iterator does some actual job then yield's part of response, sentry sdk would not catch anything after the url handler itself returned, and will miss everything being done in the iterator. This fixes the issue. Signed-off-by: Wei Mingzhi --- sentry_sdk/integrations/wsgi.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 50deae10c5..61c7036af9 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -134,12 +134,17 @@ def __call__(self, environ, start_response): _sentry_start_response, start_response, transaction ), ) + if hasattr(response, "__iter__"): + scoped_response = _ScopedResponse(scope, response) + for it in scoped_response: + yield it except BaseException: reraise(*_capture_exception()) finally: _wsgi_middleware_applied.set(False) - return _ScopedResponse(scope, response) + if not hasattr(response, "__iter__"): + return _ScopedResponse(scope, response) def _sentry_start_response( # type: ignore