Skip to content

Commit 05d5050

Browse files
committed
fix(boto3): ensure span finishes correctly when reading streaming body
1 parent e3ab470 commit 05d5050

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

‎tests/integrations/boto3/test_client.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def __init__(self, exception):
104104
self._exception = exception
105105

106106
def read(self, *args, **kwargs):
107+
# urllib3 closes the response before propagating some read failures.
108+
self.close()
107109
raise self._exception
108110

109111
def respond(request, **kwargs):

0 commit comments

Comments
 (0)