Skip to content

Allow same-size bytearray.resize with exported buffers - #9217

Merged
youknowone merged 2 commits into
RustPython:mainfrom
seungje0612:fix-bytearray-resize
Oct 22, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
seungje0612:fix-bytearray-resize

Conversation

@seungje0612

@seungje0612 seungje0612 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor
  • I did not use AI to write the code of this patch.
  • This PR follows our AI policy

AI assistance: Codex:GPT-6

Summary

  1. Update bytearray.resize() in crates/vm/src/builtins/bytearray.rs to use resize_access(), allowing unchanged-size requests under exports while rejecting actual length changes and avoiding nested payload read locks.

  2. Remove the expected-failure/TODO marker from ByteArrayTest.test_resize_forbidden in Lib/test/test_bytes.py

  3. Add three regression tests to extra_tests/snippets/builtin_bytearray.py:

  • test_bytearray_resize_exported_buffer: verify same-size success, rejection of length changes while a child view remains, and zero-filled growth after releasing all views.
  • test_bytearray_resize_invalid_size: verify ValueError for negative sizes and TypeError for float arguments while exported, without modifying the buffer.
  • test_bytearray_resize_codec_callback_with_waiting_writer: verify correct resize results and completion without deadlock during a codec error callback with a competing writer, using a subprocess timeout.

Details

  1. ByteArrayTest.test_resize_forbidden fails at b.resize(10) when the bytearray already has length 10 and an active buffer export. Before this change, resize() calls try_resizable() and raises BufferError before comparing the requested size with the current length.

  2. CPython's implementation checks for an unchanged length before checking whether resizing is permitted. An unchanged-size request succeeds without modifying the buffer, even when exports are active.

  3. Match this behavior while continuing to reject actual length changes under an export. Use the existing resize_access() method to obtain either the stable exported length or exclusive payload access. This allows same-size resize requests to succeed under active exports without acquiring an additional payload read lock. In particular, it avoids introducing a potential deadlock when a codec error callback re-enters resize() while decoding holds a read guard and another thread is waiting for the write lock.

Summary by CodeRabbit

  • Bug Fixes
    • Resizing a bytearray to its current length now succeeds while memory views are active; attempts to change its length continue to raise an error without altering the buffer.
    • Resizing works again after memory views are released, and invalid sizes continue to produce the appropriate errors even when a view is active.
    • Resizing from a codec error handler while another write is waiting no longer causes the writer to hang.

Use the existing export snapshot or exclusive payload access to allow
same-size requests without a recursive read lock in codec callbacks.
Continue rejecting exported length changes and preserve zero filling.

Enable the CPython regression and consolidate resize coverage in the
existing bytearray snippet, including a bounded concurrent callback test.

Assisted-by: Codex:GPT-6
@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-19T15:44:52.609071Z 8756983 New commits
ℹ️ 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: 6d2a58fb-8631-48c1-8c72-22e120502084
📥 Commits

Reviewing files that changed from the base of the PR and between 58b54c4 and 8c4da2b.

⛔ Files ignored due to path filters: 1
  • Lib/test/test_bytes.py is excluded by !Lib/**
📒 Files selected for processing: 2
  • crates/vm/src/builtins/bytearray.rs
  • extra_tests/snippets/builtin_bytearray.py

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

📝 Walkthrough

bytearray.resize now succeeds when the requested size matches the length of an actively exported buffer and raises BufferError when it differs. Tests cover active views, invalid sizes, and a codec callback while a writer waits.

Changes: Bytearray resize behavior

Layer / File(s) Summary
Export-aware resize rules
crates/vm/src/builtins/bytearray.rs, extra_tests/snippets/builtin_bytearray.py
Resize compares the requested size with the current or exported length. Tests cover same-size and length-changing requests with active views, invalid sizes, and resizing after views are released.
Codec callback with waiting writer
extra_tests/snippets/builtin_bytearray.py
A subprocess test checks same-size and length-changing resize calls during codec error handling while a writer waits. It verifies that decoding and the writer complete, then checks that resizing succeeds after the callback.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: 1ndahous3

Merge Risk: ⚪ Minimal · up to 8c4da

Same-size resize and the waiting-writer case are covered; no merge-blocking issue was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 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 primary change: allowing same-size bytearray.resize calls when buffers are exported.
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.
✨ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c4da2bc84

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

buffer = bytearray(data)
view = memoryview(buffer)
child = view[:]
assert buffer.resize(len(buffer)) is None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the test logic disallowed by repository policy

For this AI-assisted change, the new regression test and its assertions add test logic to extra_tests/snippets/builtin_bytearray.py, but the root instructions limit test-file edits to removing passing expected-failure/TODO markers or adding expected-failure markers. This addition is outside both permitted forms and should be removed or moved to a validation mechanism approved by the maintainers.

AGENTS.md reference: AGENTS.md:L273-L280

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these test not covered by test_bytse.py and test_bytearray.py ?
we usually only add tests not already exist in Lib/test/

@seungje0612 seungje0612 Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comment.

I found that core behavior of the first two added tests are already covered by test_bytes.py.
And test_bytearray_resize_codec_callback_with_waiting_writer was intended to catch a potential deadlock if the new length check used len(). But the implementation uses resize_access() to avoid this problem.

So, I will remove all three added tests.

@github-actions github-actions Bot added the z-ca-2026 Tag to track Contribution Academy 2026 label Oct 19, 2026

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thank you!

@youknowone
youknowone merged commit e57c8db into RustPython:main Oct 22, 2026
20 of 23 checks passed
@seungje0612
seungje0612 deleted the fix-bytearray-resize branch October 25, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants