From fc6229353158a897bdfce2a7b1c226e1407027d1 Mon Sep 17 00:00:00 2001 From: Caio Theodoro <48462974+caiotheodoro@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:40:59 -0300 Subject: [PATCH] fix(streaming): close S3Object when transform doesn't return an IO stream CsvTransform returns a csv.DictReader, which has no close() or closed. After reading a row, S3Object.closed and close() raised AttributeError. Only call close() on the transformed stream when it exposes one, and fall back to the raw stream's state for closed when it doesn't. closes #8886 --- .../utilities/streaming/s3_object.py | 13 +++++- .../streaming/_boto3/test_s3_object.py | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/aws_lambda_powertools/utilities/streaming/s3_object.py b/aws_lambda_powertools/utilities/streaming/s3_object.py index 74b09c14cba..5d111486828 100644 --- a/aws_lambda_powertools/utilities/streaming/s3_object.py +++ b/aws_lambda_powertools/utilities/streaming/s3_object.py @@ -208,7 +208,12 @@ def closed(self) -> bool: # central directory on creation), and after close() it would wrap an already closed raw stream. if self._transformed_stream is None: return self.raw_stream.closed - return self._transformed_stream.closed + # Some transformations don't return IO streams (e.g. CsvTransform returns a csv.DictReader), + # which have no `closed` attribute; the raw stream state is authoritative in that case. + transformed_closed = getattr(self._transformed_stream, "closed", None) + if transformed_closed is None: + return self.raw_stream.closed + return transformed_closed def __enter__(self): return self @@ -229,7 +234,11 @@ def close(self): # >>> s3object = S3Object(...., is_gzip=True) # >>> s3object.close() <- transformed_stream doesn't exist yet at this point if self.raw_stream != self._transformed_stream and self._transformed_stream is not None: - self._transformed_stream.close() + # Some transformations don't return IO streams (e.g. CsvTransform returns a csv.DictReader); + # those have no close() to call, and the raw stream closed above releases the resource. + transformed_close = getattr(self._transformed_stream, "close", None) + if transformed_close is not None: + transformed_close() def read(self, size: int = -1) -> bytes: return self.transformed_stream.read(size) diff --git a/tests/functional/streaming/_boto3/test_s3_object.py b/tests/functional/streaming/_boto3/test_s3_object.py index 7358e54a6aa..11a894eb8c5 100644 --- a/tests/functional/streaming/_boto3/test_s3_object.py +++ b/tests/functional/streaming/_boto3/test_s3_object.py @@ -116,6 +116,49 @@ def test_s3_closed_does_not_make_requests_for_zip(s3_client, s3_client_stub): s3_client_stub.assert_no_pending_responses() +@pytest.mark.parametrize("is_gzip", [False, True], ids=["raw", "gzip"]) +def test_s3_csv_closed_after_close(s3_client, s3_client_stub, is_gzip): + # GIVEN a CSV object, whose transformed stream is a DictReader without closed/close attributes + payload = b"name,city\nfoo,bar\n" + wire_payload = gzip.compress(payload) if is_gzip else payload + _stub_get_object(s3_client_stub, wire_payload) + obj = S3Object(bucket="bucket", key="key", boto3_client=s3_client, is_gzip=is_gzip, is_csv=True) + + # WHEN a row is read, materializing the DictReader + assert next(obj) == {"name": "foo", "city": "bar"} + + # THEN inspecting and calling close work, and the object reports as closed + assert obj.closed is False + obj.close() + assert obj.closed is True + + +def test_s3_csv_context_manager_close(s3_client, s3_client_stub): + # GIVEN a CSV object used as a context manager + _stub_get_object(s3_client_stub, b"name,city\nfoo,bar\n") + + # WHEN a row is read inside the context + with S3Object(bucket="bucket", key="key", boto3_client=s3_client, is_csv=True) as obj: + assert next(obj) == {"name": "foo", "city": "bar"} + + # THEN exiting the context closes it without raising AttributeError + assert obj.closed is True + + +def test_s3_csv_in_place_transform_close(s3_client, s3_client_stub): + # GIVEN a CsvTransform applied in place + _stub_get_object(s3_client_stub, b"name,city\nfoo,bar\n") + obj = S3Object(bucket="bucket", key="key", boto3_client=s3_client) + obj.transform(CsvTransform(), in_place=True) + + # WHEN a row is read and the object is closed + assert next(obj) == {"name": "foo", "city": "bar"} + obj.close() + + # THEN it reports as closed + assert obj.closed is True + + def _stub_get_object(s3_client_stub, payload: bytes): streaming_body = PowertoolsStreamingBody(raw_stream=io.BytesIO(payload), content_length=len(payload)) s3_client_stub.add_response("get_object", {"Body": streaming_body})