Skip to content

fix(streaming): close S3Object when transform doesn't return an IO stream - #8898

Open
caiotheodoro wants to merge 1 commit into
aws-powertools:developfrom
caiotheodoro:fix/s3object-csv-close
Open

caiotheodoro wants to merge 1 commit into
aws-powertools:developfrom
caiotheodoro:fix/s3object-csv-close

Conversation

@caiotheodoro

@caiotheodoro caiotheodoro commented Nov 24, 2026 •

Copy link
Copy Markdown

Draft PR — not filed

Title: fix(streaming): close S3Object when transform doesn't return an IO stream

Issue number: closes #8886

Summary

Changes

S3Object assumed every transformed stream implements IO[bytes], but
CsvTransform returns a csv.DictReader, which has neither close() nor
closed. close() now only calls close() on the transformed stream when it
exposes one (the raw stream is always closed first, releasing the underlying
resource), and closed falls back to raw_stream.closed when the transformed
stream doesn't report its own state. Gzip/zip transforms are unaffected — they
still return real IO objects whose close/closed are honoured.

User experience

Before: after reading a row from S3Object(..., is_csv=True), obj.closed
raised AttributeError: 'DictReader' object has no attribute 'closed' and
obj.close() (including with exit) raised AttributeError: 'DictReader' object has no attribute 'close' — plain CSV, gzip+CSV, and in-place
CsvTransform all affected.

After: closed correctly reports the raw stream's state and close() releases
the object cleanly.

Verification

  • New functional tests via the public API in
    tests/functional/streaming/_boto3/test_s3_object.py: is_csv (raw and
    gzip), context-manager exit, and in-place CsvTransform. All 4 fail on
    develop with the reported AttributeError; all pass with the fix.
  • pytest tests/functional/streaming/: 86 passed.
  • ruff format --check, ruff check, mypy: clean on changed files.
  • Note: issue is labeled triage — happy to hold for maintainer triage.

AI-assisted: I reproduced the bug, wrote the fix and tests, and ran the checks
above myself.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…ream

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 aws-powertools#8886
@caiotheodoro
caiotheodoro requested a review from a team as a code owner November 24, 2026 00:41
@caiotheodoro
caiotheodoro requested a review from svozza November 24, 2026 00:41
@boring-cyborg

boring-cyborg Bot commented Nov 24, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 24, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files. streaming tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant