Skip to content

Commit fc62293

Browse files
committed
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
1 parent be21802 commit fc62293

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎aws_lambda_powertools/utilities/streaming/s3_object.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ def closed(self) -> bool:
208208
# central directory on creation), and after close() it would wrap an already closed raw stream.
209209
if self._transformed_stream is None:
210210
return self.raw_stream.closed
211-
return self._transformed_stream.closed
211+
# Some transformations don't return IO streams (e.g. CsvTransform returns a csv.DictReader),
212+
# which have no `closed` attribute; the raw stream state is authoritative in that case.
213+
transformed_closed = getattr(self._transformed_stream, "closed", None)
214+
if transformed_closed is None:
215+
return self.raw_stream.closed
216+
return transformed_closed
212217

213218
def __enter__(self):
214219
return self
@@ -229,7 +234,11 @@ def close(self):
229234
# >>> s3object = S3Object(...., is_gzip=True)
230235
# >>> s3object.close() <- transformed_stream doesn't exist yet at this point
231236
if self.raw_stream != self._transformed_stream and self._transformed_stream is not None:
232-
self._transformed_stream.close()
237+
# Some transformations don't return IO streams (e.g. CsvTransform returns a csv.DictReader);
238+
# those have no close() to call, and the raw stream closed above releases the resource.
239+
transformed_close = getattr(self._transformed_stream, "close", None)
240+
if transformed_close is not None:
241+
transformed_close()
233242

234243
def read(self, size: int = -1) -> bytes:
235244
return self.transformed_stream.read(size)

‎tests/functional/streaming/_boto3/test_s3_object.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,49 @@ def test_s3_closed_does_not_make_requests_for_zip(s3_client, s3_client_stub):
116116
s3_client_stub.assert_no_pending_responses()
117117

118118

119+
@pytest.mark.parametrize("is_gzip", [False, True], ids=["raw", "gzip"])
120+
def test_s3_csv_closed_after_close(s3_client, s3_client_stub, is_gzip):
121+
# GIVEN a CSV object, whose transformed stream is a DictReader without closed/close attributes
122+
payload = b"name,city\nfoo,bar\n"
123+
wire_payload = gzip.compress(payload) if is_gzip else payload
124+
_stub_get_object(s3_client_stub, wire_payload)
125+
obj = S3Object(bucket="bucket", key="key", boto3_client=s3_client, is_gzip=is_gzip, is_csv=True)
126+
127+
# WHEN a row is read, materializing the DictReader
128+
assert next(obj) == {"name": "foo", "city": "bar"}
129+
130+
# THEN inspecting and calling close work, and the object reports as closed
131+
assert obj.closed is False
132+
obj.close()
133+
assert obj.closed is True
134+
135+
136+
def test_s3_csv_context_manager_close(s3_client, s3_client_stub):
137+
# GIVEN a CSV object used as a context manager
138+
_stub_get_object(s3_client_stub, b"name,city\nfoo,bar\n")
139+
140+
# WHEN a row is read inside the context
141+
with S3Object(bucket="bucket", key="key", boto3_client=s3_client, is_csv=True) as obj:
142+
assert next(obj) == {"name": "foo", "city": "bar"}
143+
144+
# THEN exiting the context closes it without raising AttributeError
145+
assert obj.closed is True
146+
147+
148+
def test_s3_csv_in_place_transform_close(s3_client, s3_client_stub):
149+
# GIVEN a CsvTransform applied in place
150+
_stub_get_object(s3_client_stub, b"name,city\nfoo,bar\n")
151+
obj = S3Object(bucket="bucket", key="key", boto3_client=s3_client)
152+
obj.transform(CsvTransform(), in_place=True)
153+
154+
# WHEN a row is read and the object is closed
155+
assert next(obj) == {"name": "foo", "city": "bar"}
156+
obj.close()
157+
158+
# THEN it reports as closed
159+
assert obj.closed is True
160+
161+
119162
def _stub_get_object(s3_client_stub, payload: bytes):
120163
streaming_body = PowertoolsStreamingBody(raw_stream=io.BytesIO(payload), content_length=len(payload))
121164
s3_client_stub.add_response("get_object", {"Body": streaming_body})

0 commit comments

Comments
 (0)