PyO3 / PyO3/pyo3

Disallow races in initializing types

Open
#5,211 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

@kumaraditya303 looked a little at the races I reported against CPython here: https://github.com/python/cpython/issues/130421#issuecomment-2970633104

He tells me that the PyTuple_Pack and _Py_IncRef races are happening because it's possible for a supertype and subtype to initialize simultaneously. Right now CPython doesn't do anything to sanity check that situation and PyO3 should probably do something a little saner.

I think as a first pass we should consider replacing the GILOnceCell uses in the pyclass internals with e.g. OnceLock and OnceLockExt::get_or_init_py_attached. If that doesn't fix the races, we should also add checks when initializing subclasses to make sure that the superclass is initialized and wait until that's finished before trying to initialize a subclass.

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 by tracing the pyclass internals that use GILOnceCell, then compare their initialization paths with OnceLock and OnceLockExt::get_or_init_py_attached. Check whether simultaneous supertype and subtype initialization can still occur; done means the reported PyTuple_Pack and _Py_IncRef races are prevented and superclass initialization is ordered before subclass initialization.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.