Repository navigation
fix(python): refuse copy=False over memory no Python object owns - #170
Merged
Merged
Conversation
A memory view without an exporter (`obj` is `None`: `ctypes.memoryview_at`, or a C extension's `PyMemoryView_FromMemory`) does not own its memory and keeps nothing alive that does, so `read_only_chain`, which took such a view for the buffer's owner, let a matrix borrow memory that could be freed under it: keeping the array and the view alive keeps the allocation alive only when some object owns it. `DMatrix(copy=False)` now refuses an array whose base chain reaches an ownerless memory view; Python >= 3.14 tests it with `ctypes.memoryview_at(..., readonly=True)` over a ctypes buffer that outlives the test.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
DMatrix(x, copy=False)refuses an array whose base chain reaches a memory view without an exporter (objisNone), such asctypes.memoryview_at(...)or a C extension'sPyMemoryView_FromMemory.A view like that doesn't own its memory and keeps nothing alive that does. #169's
read_only_chaintreated it as the buffer's owner, and #169's review fix (3380d8fe) let it through when it was read-only. A matrix could then borrow memory that might be freed under it. Keeping_arrayalive, and its weak-reference pins, keeps an allocation alive only when some Python object owns it. That's whatBorrowedArray'sSAFETYargument relies on.Changes
python/src/borrowed.rs:read_only_chainreturns an error for an ownerless memory view instead of asking that view's buffer whether it is read-only; the module and function docs list the refusal.python/python/hessboost/_matrix.py:DMatrix'sRaises:names it.python/tests/test_data.py: on Python >= 3.14, an array fromnp.frombuffer(ctypes.memoryview_at(addr, size, readonly=True))is refused, and the same values inbytesare borrowed. The ctypes buffer that owns the memory stays alive for the whole test, so nothing reads freed memory. The test failed before the fix ("DID NOT RAISE") and passes after. It skips on 3.12 and 3.13, which have nomemoryview_at.Checks (local)
python/:cargo fmt --check,cargo clippy --all-targets -- -D warnings, the same with--features cuda,uv run --locked pytest(449 passed),pyright --verifytypes hessboost --ignoreexternal(100%)ruff check,ruff format --check,ty check