Skip to content

Report thread safety issue with PyO3's PyCFunction::new_closure#2960

Merged
LawnGnome merged 1 commit into
rustsec:mainfrom
davidhewitt:pyo3-closure-sync
Jun 11, 2026
Merged

Report thread safety issue with PyO3's PyCFunction::new_closure#2960
LawnGnome merged 1 commit into
rustsec:mainfrom
davidhewitt:pyo3-closure-sync

Conversation

@davidhewitt

Copy link
Copy Markdown
Contributor

Affected crate(s)

  • pyo3 (40M recent downloads per crates.io)

Links to upstream issue(s) or PR(s)

PyO3/pyo3#6096

Severity

PyCFunction::new_closure missed a Sync bound on the closure to be wrapped as a Python callable. Older Python releases would have been less likely to observe data races due to partial serial execution imposed by the Global Interpreter Lock. As the Python ecosystem transitions to free-threaded Python, the Sync bound is critical for thread safety.

This API is not the primary mechanism by which user code produces Python callables (typically done by #[pyfunction] and #[pymethods] proc macros), however it seems highly likely that users will have written thread-unsafe code using this API. PyO3 happened to have such a case in its own test suite! (Was using a RefCell inside the closure in question.)

Checklist

  • Advisory filename(s) starts with RUSTSEC-0000-0000 as the ID
  • date field is set to the public disclosure date
  • Contains a concise and descriptive title after advisory metadata
  • Asked maintainer(s) if publishing an advisory is appropriate

@davidhewitt davidhewitt changed the title report thread safety issue with PyO3's PyCFunction::new_closure Report thread safety issue with PyO3's PyCFunction::new_closure Jun 11, 2026
@LawnGnome

Copy link
Copy Markdown
Contributor

Thanks!

@LawnGnome LawnGnome merged commit f1a5907 into rustsec:main Jun 11, 2026
1 check passed
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.

2 participants