Skip to content

fix(tools): close S3 response body in S3ReaderTool - #7654

Open
FanouZeng-TT wants to merge 1 commit into
crewAIInc:mainfrom
FanouZeng-TT:fix/s3-reader-body-leak
Open

FanouZeng-TT wants to merge 1 commit into
crewAIInc:mainfrom
FanouZeng-TT:fix/s3-reader-body-leak

Conversation

@FanouZeng-TT

Copy link
Copy Markdown

Related issue

Fixes #7646

Summary

  • close the S3 get_object() response body in S3ReaderTool so pooled HTTP connections are released deterministically
  • wrap read/decode in try/finally so the body is closed exactly once on success and on every failure path
  • add offline regression tests with a mocked S3 client covering success, decode failure, and read failure

The body previously stayed open on every path, leaving cleanup to garbage collection and retaining pooled connections across repeated tool calls.

Verification

  • Tests added or updated for the changed behavior

  • Relevant tests and quality checks pass locally

  • uv run --frozen pytest lib/crewai-tools/tests/tools/s3_reader_tool_test.py -q — red baseline before the fix: 3 failed (success, decode failure, read failure), 1 passed (ClientError behavior baseline); after the fix: 4 passed

  • ruff check on both changed files — passed

  • Tests are fully offline: a fake boto3/botocore module pair is injected via monkeypatch.setitem(sys.modules, ...), so neither the SDK nor network access is required

Additional context

This PR was prepared with AI assistance; please apply the llm-generated label (external contributors cannot add repository labels).

🤖 Generated with Claude Code

The get_object() response body was never closed, so successful reads and
decode failures both left the underlying HTTP response open for garbage
collection. Wrap read/decode in try/finally so the body is closed exactly
once on every path.
@FanouZeng-TT

Copy link
Copy Markdown
Author

This PR was prepared with AI assistance. Per the contributing requirements it needs the llm-generated label — external contributors cannot add repository labels, so could a maintainer apply it? Thanks!

@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 528b0b7c-6d3a-4f92-ab33-531fce51af04

📥 Commits

Reviewing files that changed from the base of the PR and between 0374c63 and 963c700.

📒 Files selected for processing (2)
  • lib/crewai-tools/src/crewai_tools/aws/s3/reader_tool.py
  • lib/crewai-tools/tests/tools/s3_reader_tool_test.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

S3ReaderTool now closes successful and failed S3 response-body reads. New tests verify closure, propagated read and decode errors, and existing ClientError formatting.

Changes

S3 response body cleanup

Layer / File(s) Summary
Body cleanup and validation
lib/crewai-tools/src/crewai_tools/aws/s3/reader_tool.py, lib/crewai-tools/tests/tools/s3_reader_tool_test.py
S3ReaderTool reads and decodes the body inside try and closes it in finally. Tests verify closure after success, UnicodeDecodeError, and OSError, and verify existing ClientError output.

Priority: ➖ Normal

Severity of issue fixed: Medium

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: closing the S3 response body in S3ReaderTool.
Description check ✅ Passed The description includes all required sections. It links issue #7646, explains the change, lists verification checks and results, and provides additional context.
Linked Issues check ✅ Passed The change satisfies #7646. After get_object() succeeds, _run stores response["Body"] and closes it in finally. This closes the body once after successful reads, UTF-8 decode failures, and rea…
Out of Scope Changes check ✅ Passed The changes stay within #7646. The source change adds deterministic S3 response-body cleanup. The new tests verify cleanup and preservation of existing behavior. No unrelated production behavior or fi…
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] S3ReaderTool leaves response bodies open

1 participant