Visitar URL original
Synchronize mmap buffer exports with close and resize by 1ndahous3 · Pull Request #9015 · RustPython/RustPython · GitHub
Skip to content

Synchronize mmap buffer exports with close and resize - #9015

Open
1ndahous3 wants to merge 2 commits into
RustPython:mainfrom
1ndahous3:mmap_buffer_lifetime
Open

1ndahous3 wants to merge 2 commits into
RustPython:mainfrom
1ndahous3:mmap_buffer_lifetime

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Reject buffer exports from closed or invalid mappings.
  • Synchronize buffer acquisition with closing and Windows resizing, so a concurrent export cannot be invalidated after the export count was checked.
  • Reject resizing closed mappings while preserving existing buffer and access error precedence.

AI assistance

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory-mapped file stability when creating views, closing mappings, or resizing them.
    • Closing or resizing a mapping with an active borrowed view now fails promptly with the appropriate error, rather than waiting for the view to be released.
    • Invalid operations on closed mappings and attempts to resize while a view is active now raise the expected errors.
    • Exported child views continue to keep a mapping open after the parent view is released.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 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-08T22:49:05.071212Z aa7ac19 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 8, 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: 5c9e99cf-8a59-4055-9cea-7a8ebbbbb3f1
📥 Commits

Reviewing files that changed from the base of the PR and between 7a6b435 and aa7ac19.

📒 Files selected for processing (1)
  • crates/stdlib/src/mmap.rs

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

Walkthrough

The mmap implementation now validates mappings during buffer creation and manages mapping guards during close and resize operations. Tests cover closed mappings, exported child views, and Windows resize behavior.

Changes

mmap guard lifecycle

Layer / File(s) Summary
Buffer access and close
crates/stdlib/src/mmap.rs, extra_tests/snippets/stdlib_mmap.py
Buffer creation validates the mapping while holding its guard. Close checks for exported buffers before and after acquiring the mapping guard. Tests cover closed mappings and active child views.
Resize guard handling
crates/stdlib/src/mmap.rs, extra_tests/snippets/stdlib_mmap.py
Resizeability checks return a mapping guard and check for exported buffers before and after locking. Unix resize releases the guard before continuing. Windows resize retains it through resizing and releases it on error paths. Tests cover active views, successful resizing after view release, and closed mappings.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to aa7ac

This change makes mmap buffer exports synchronize with close and resize. No concrete merge-blocking risk was identified in the supplied context. The PR reports no test results, so the usual pre-merge checks still apply.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 63.64% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 summarizes the main change: synchronizing mmap buffer exports with close and resize operations.
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
  • Autopilot · 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: 7a6b4352d2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/stdlib/src/mmap.rs

#[pymethod]
fn close(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
let mut mmap = zelf.mmap.lock();

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 Reject exported close calls before taking the data lock

When one thread passes this mmap to _SSLSocket.write, crates/stdlib/src/openssl.rs:3073-3104 holds the mmap guard via data.borrow_buf() across a potentially unbounded allow_threads call. A concurrent mapped.close() now blocks here instead of observing the already-nonzero export count and raising BufferError; because this mutex wait remains attached to the interpreter, it can also participate in a stop-the-world deadlock. Keep an unlocked export-count fast path before this acquisition and recheck under the lock to preserve the race fix; check_resizeable needs the same ordering.

Useful? React with 👍 / 👎.

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.

Thanks, you're right. Fixed in aa7ac19. close() and check_resizeable() now reject existing exports before acquiring the mapping mutex, while retaining the check under the lock to prevent invalidating a concurrent export.

Both new regressions hold an actual borrowed mmap buffer across the operation. They failed before this change and now pass on Linux and Windows.

@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: aa7ac19eb5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +45 to +46
with assert_raises(ValueError):
memoryview(mapped)

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 new assertions disallowed by the test policy

This block adds new test setup, assertions, and data to a test file, but the repository's critical test policy permits test-file edits only for adding or removing expected-failure markers (and their associated TODO comments). The added snippet therefore needs to be removed or the repository policy changed before landing.

AGENTS.md reference: AGENTS.md:L273-L279

Useful? React with 👍 / 👎.

@codspeed

codspeed Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 15.38%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 61 untouched benchmarks
⏩ 4 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ rustpython[frozenset.py] 411.6 µs 356.8 µs +15.38%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing 1ndahous3:mmap_buffer_lifetime (aa7ac19) with main (54e47cd)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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.

1 participant