@@ -217,6 +217,7 @@ def _instrument_streaming_body(
217217 raw_stream = body ._raw_stream # type: ignore[attr-defined]
218218 orig_raw_close = raw_stream .close
219219 finished = False
220+ read_in_progress = False
220221
221222 def finish_span (error : "Optional[BaseException]" = None ) -> None :
222223 nonlocal finished
@@ -236,6 +237,8 @@ def content_length_reached() -> bool:
236237 )
237238
238239 def sentry_streaming_body_read (* args : "Any" , ** kwargs : "Any" ) -> bytes :
240+ nonlocal read_in_progress
241+ read_in_progress = True
239242 try :
240243 read_return_value = orig_read (* args , ** kwargs )
241244 with capture_internal_exceptions ():
@@ -251,6 +254,8 @@ def sentry_streaming_body_read(*args: "Any", **kwargs: "Any") -> bytes:
251254 except BaseException as error :
252255 finish_span (error )
253256 raise
257+ finally :
258+ read_in_progress = False
254259
255260 def sentry_streaming_body_close (* args : "Any" , ** kwargs : "Any" ) -> None :
256261 try :
@@ -263,7 +268,8 @@ def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None:
263268 def sentry_raw_stream_close (* args : "Any" , ** kwargs : "Any" ) -> None :
264269 try :
265270 orig_raw_close (* args , ** kwargs )
266- finish_span ()
271+ if not read_in_progress :
272+ finish_span ()
267273 except BaseException as error :
268274 finish_span (error )
269275 raise
0 commit comments