Bug #75768
openPyO3 workaround uses hardcoded python3 from PATH for interpreter execution
0%
Description
Bug #64213: MGR modules incompatible with later PyO3 versions - PyO3 modules may only be initialized once per interpreter process has introduced a new mechanic for executing any cryptography related functionality in a separate cleanly spawned Python interpreter.
This was and is necessary since PyO3 libraries, i.e. Python libraries mainly written in Rust, do not currently support Python subinterpreters, which ceph-mgr explicitly uses.
However the way the merged code does the spawning of that interpreter boils down to executing python3 from PATH to execute the relevant code which is part of the ceph-common Python library.
The problem here is that it is not guaranteed that this python3 is the one that ceph-common is installed for, or even that it is the correct version.
Many distributions allow multiple versions of Python to be installed at the same time, and if Ceph was installed for Python 3.12, but the default Python 3 was, say, 3.13, then this would most likely break.
That is unless you had Ceph installed for both versions at which point you could run into any number of other issues including version mismatches.
This fails, for instance, when packaging Ceph Tentacle in nixpkgs which does not have python3 on its PATH at all (or if it does it's because a user has put an interpreter there, which in turn most likely does not have ceph-common installed).
Now the way you would usually do this in the most pythonic way is to use the multiprocessing library or any other related mechanics and completely avoid having to know about any of those details.
If for some reason you could not use those, you would probably want to use at least sys.executable to determine which Python to use.
However given that Ceph uses libpython3 with its own executables (such as ceph-mgr) things get a little more complicated.
I do not want to assume why the implementation is what it is currently, however if I had to guess, I'd probably say that multiprocessing simply didn't work at all, and that sys.executable was broken too.
Which is true.
As of time of writing the Python environment in which ceph-mgr modules run does not know about its Python interpreter, which subsequently breaks sys.executable and the multiprocessing library.
So working backwards we can:
1. figure out why Ceph doesn't know about its Python interpreter, despite getting the interpreter explicitly passed via CMake.
2. correct for that behaviour
3. use the Python facilities (to any degree) to ensure the correct Python interpreter is used
NM Updated by Nitzan Mordechai 6 months ago
- Assignee set to Nitzan Mordechai
UB Updated by Utkarsh Bhatt 2 months ago
In Ceph 20.2.4, WCHAR stringifies the macro name instead of expanding it. GDB on the binary (built from 20.2.4) confirms Python receives U"MGR_PYTHON_EXECUTABLE", leaving sys.executable empty. This also breaks Cephadm bootstrap under Python 3.14 (with pyo3 separate interpreter not resolving).
KC Updated by Kefu Chai 21 days ago
Utkarsh Bhatt wrote in #note-2:
In Ceph 20.2.4, WCHAR stringifies the macro name instead of expanding it. GDB on the binary (built from 20.2.4) confirms Python receives U"MGR_PYTHON_EXECUTABLE", leaving sys.executable empty. This also breaks Cephadm bootstrap under Python 3.14 (with pyo3 separate interpreter not resolving).
https://github.com/ceph/ceph/pull/73050 addresses this problem.