Skip to content

Release BytesIO and StringIO buffers on close - #9230

Merged
youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:memoryio_close_buffer
Oct 20, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
1ndahous3:memoryio_close_buffer

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Release backing storage when closing BytesIO and StringIO, even if the stream object remains alive.
  • Synchronize close and storage replacement with buffer exports; active BytesIO views still prevent close and resizing.
  • Reinitializing a closed stream reopens it, matching CPython.
  • Release initialization arguments after updating the stream and unlocking it, so buffer-release callbacks and finalizers observe the new state and can reenter safely.

Performance

Resident memory after closing and retaining 16 streams, each written with 8 MiB of data.

Windows 11 x64

Stream Main Patched
BytesIO 169.2 MiB 49.0 MiB
StringIO 177.1 MiB 57.1 MiB

Debian 12 amd64

Stream Main Patched
BytesIO 193.4 MiB 63.5 MiB
StringIO 200.8 MiB 71.6 MiB

AI assistance

Written with Codex (GPT-6), reviewed by a human before submission.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed stream behavior when closing or reinitializing byte and string buffers. Reinitialized streams now correctly reflect their open or closed state and supplied content.
    • Closing a stream now releases its stored content, and buffer access, truncation, and restoration handle closed streams consistently.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T18:27:30.306825Z f26e909 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ddbed40f-63d6-4023-a73e-531d76344401
📥 Commits

Reviewing files that changed from the base of the PR and between c08375b and f26e909.

📒 Files selected for processing: 2
  • crates/vm/src/stdlib/_io.rs
  • extra_tests/snippets/stdlib_io.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

📝 Walkthrough

StringIO and BytesIO initialization and closure now update their contents while holding buffer locks. BytesIO buffer operations also check the closed state after acquiring the buffer. Tests cover stream reinitialization, reentrant closure, and storage capacity after closure.

Changes: IO stream buffer lifecycle

Layer / File(s) Summary
Stream reinitialization
crates/vm/src/stdlib/_io.rs, extra_tests/snippets/stdlib_io.py
Initialization replaces stream contents under the buffer lock and resets the closed state. Tests cover reinitialization and reentrant closure during reinitialization.
Stream closure and storage release
crates/vm/src/stdlib/_io.rs
Closing either stream clears its contents while holding the buffer lock. A test checks that each backing vector has zero capacity after closure.
Guarded buffer access and state updates
crates/vm/src/stdlib/_io.rs
StringIO state restoration uses its closed-checking accessor. BytesIO truncation, state restoration, and buffer export check the closed state while holding the relevant buffer lock.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to f26e9

The reviewed buffer-lifecycle changes have no identified issue that needs correction before merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 53.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: releasing BytesIO and StringIO backing buffers when the streams close.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@youknowone
youknowone merged commit 915a390 into RustPython:main Oct 20, 2026
30 checks passed
@youknowone

Copy link
Copy Markdown
Member

Thank you so much!

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.

2 participants