Skip to content

Bug: S3Object with a CSV transformation raises AttributeError on close() and closed #8886

Open

Description

@k0nnect

Expected Behaviour

an S3Object created with is_csv=True, or transformed in place with CsvTransform, can be closed like any other S3Object: close() and leaving a with block return normally, and closed returns False before closing and True after.

Current Behaviour

once at least one row has been read, close() raises AttributeError: 'DictReader' object has no attribute 'close'. leaving a with S3Object(..., is_csv=True) block raises the same error, because __exit__ calls close(). reading closed raises AttributeError: 'DictReader' object has no attribute 'closed'.

the error comes after the rows were already processed, so a handler that reads a csv file inside a with block does all its work and then fails the invocation.

the cause is in aws_lambda_powertools/utilities/streaming/s3_object.py. close() (line 232) calls self._transformed_stream.close() and closed (line 211) returns self._transformed_stream.closed. both assume the transformed stream is a file object, but CsvTransform.transform() returns a csv.DictReader, which has neither close() nor closed. it happens with is_csv=True, with is_gzip=True, is_csv=True, and with transform(CsvTransform(), in_place=True).

if the object is closed before any row is read, nothing fails, because the transformed stream has not been created yet.

Code snippet

import io

import boto3
from botocore import stub
from botocore.response import StreamingBody

from aws_lambda_powertools.utilities.streaming import S3Object

payload = b"id,name\n1,alice\n2,bob\n"

client = boto3.client("s3", region_name="us-east-1", aws_access_key_id="x", aws_secret_access_key="x")
s3_stub = stub.Stubber(client)
s3_stub.add_response("get_object", {"Body": StreamingBody(raw_stream=io.BytesIO(payload), content_length=len(payload))})
s3_stub.activate()

with S3Object(bucket="bucket", key="key", is_csv=True, boto3_client=client) as rows:
    for row in rows:
        print(row)

Possible Solution

in S3Object.close(), only call close() on the transformed stream when it has one. the raw stream that the DictReader reads from is already closed a few lines above, so the s3 connection is still released. in S3Object.closed, fall back to self.raw_stream.closed when the transformed stream has no closed attribute.

i have this fix ready locally (11 lines in s3_object.py) with functional tests for is_csv=True, is_gzip=True, is_csv=True and transform(CsvTransform(), in_place=True). the tests fail on develop and pass with the fix, and the existing streaming tests still pass. happy to send the pr with tests if you agree with the approach.

disclosure: i found this while reviewing the streaming utility with the help of an ai assistant (claude code). the snippet above was run against develop.

Steps to Reproduce

  1. install aws-lambda-powertools and boto3.
  2. run the snippet above. it stubs GetObject, so it needs no aws account.
  3. both rows are printed, then the with block raises AttributeError on exit.

to see the closed error, read one row with next(s3_object) and then read s3_object.closed.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.41.0 (develop at be21802)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

{'id': '1', 'name': 'alice'}
{'id': '2', 'name': 'bob'}
Traceback (most recent call last):
  File "repro_issue.py", line 16, in <module>
    with S3Object(bucket="bucket", key="key", is_csv=True, boto3_client=client) as rows:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "aws_lambda_powertools/utilities/streaming/s3_object.py", line 217, in __exit__
    self.close()
  File "aws_lambda_powertools/utilities/streaming/s3_object.py", line 232, in close
    self._transformed_stream.close()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DictReader' object has no attribute 'close'

Activity

  1. added
    bug
    triage
    on Nov 23, 2026
  2. boring-cyborg commented on Nov 23, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  3. powertools-for-aws-oss-automation commented on Nov 23, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

  4. k0nnect commented on Nov 24, 2026

    @k0nnect
    Author

    i see #8898 was opened for this. for the maintainers: i still have the fix and functional tests ready locally, as offered above, and can open a pr once this is triaged if that's more useful. fine either way as long as it gets fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtriage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions