Skip to content

Commit 364f547

Browse files
committed
fix(boto3): ensure span finishes correctly when reading streaming body
1 parent eea1fc1 commit 364f547

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
@@ -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

‎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)