PyO3 / PyO3/pyo3

`PyByteArray::to_vec` is unsound

Open
#4,736 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

free-threading Unsound
Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

https://github.com/PyO3/pyo3/blob/v0.23.2/src/types/bytearray.rs#L297-L299

The implementation just calls the unsafe as_bytes function while not doing anything to uphold the safety invariant that no other thread modifies the bytearray. I think this is fine with the GIL, but without it, another thread might be ruining the show.

Aside: for the as_bytes API to be any useful with free threaded python people will want to know about critical sections (at least I think that's the right tool). So maybe a cross reference in the docs would go a long way.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/types/bytearray.rs lines 297-299 and compare PyByteArray::to_vec with the unsafe as_bytes API and its safety invariant. Review the as_bytes documentation and the linked with_critical_section guidance for free-threaded Python. Done means the unsoundness is addressed and the relevant API documentation clearly explains the required usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend-api-design, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.