Skip to content

Guard against non-free-threaded extensions in free-threaded builds #137956

@encukou

Description

@encukou

Extensions that use the non-free-threaded ABI are incompatible with free-threaded builds (and vice versa).
Loading incompatible extenstions is currently prevented by:

  • filename tags including a t, or not
  • build/install tools not installing incompatible extensions in sys.path

I'd like to add an extra last layer of defense for cases where these mechanisms fail (as they did in this report), giving users a clear error message instead of a crash. (Or just before a crash, alas.)

It turns out that static PyModuleDef includes bit-pattern(s) in the “GIL-enabled ABI” that cannot not appear in free-threaded PyModuleDef's ob_flags. We can detect this and raise an error.

My implementation reserves a bit in ob_flags that needs to stay zero. When the GIL/FT schism is over (or even earlier if needed), the bit can be reused for something else.

This only goes one way for now: detecting FT extensions loaded in GIL-ful builds is not a priority for me. My motivation is related to stable ABI: if things go wrong, existing abi3 extensions end up installed in the wrong place. (For new extensions I plan a better way.)

I'd like to get this in main now, even though we don't have the “abi3t” story fully planned, to get as much cross-platform buildbot testing as possible.
It should be easy to remove if it's superfluous or doesn't work as expected, or if we need to restructure the internals.

Linked PRs

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions