@@ -219,6 +219,7 @@ def _instrument_streaming_body(
219219 raw_stream = body ._raw_stream # type: ignore[attr-defined]
220220 orig_raw_close = raw_stream .close
221221 finished = False
222+ read_in_progress = False
222223
223224 def finish_span (error : "Optional[BaseException]" = None ) -> None :
224225 nonlocal finished
@@ -238,6 +239,8 @@ def content_length_reached() -> bool:
238239 )
239240
240241 def sentry_streaming_body_read (* args : "Any" , ** kwargs : "Any" ) -> bytes :
242+ nonlocal read_in_progress
243+ read_in_progress = True
241244 try :
242245 read_return_value = orig_read (* args , ** kwargs )
243246 with capture_internal_exceptions ():
@@ -253,6 +256,8 @@ def sentry_streaming_body_read(*args: "Any", **kwargs: "Any") -> bytes:
253256 except BaseException as error :
254257 finish_span (error )
255258 raise
259+ finally :
260+ read_in_progress = False
256261
257262 def sentry_streaming_body_close (* args : "Any" , ** kwargs : "Any" ) -> None :
258263 try :
@@ -265,7 +270,8 @@ def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None:
265270 def sentry_raw_stream_close (* args : "Any" , ** kwargs : "Any" ) -> None :
266271 try :
267272 orig_raw_close (* args , ** kwargs )
268- finish_span ()
273+ if not read_in_progress :
274+ finish_span ()
269275 except BaseException as error :
270276 finish_span (error )
271277 raise
0 commit comments