Fix infinite recursion in import hook and stdout contamination in apply.sh

sitecustomize.py: exec_module was calling importlib.util.find_spec(fullname)
after Python had already added the module to sys.modules. find_spec
short-circuits to sys.modules[name].__spec__, which is our own wrapper spec,
so real_spec.loader.exec_module(module) immediately called exec_module again
— infinite recursion. Fix: resolve the real file spec in find_spec (before
sys.modules is populated) and store it on the Loader. exec_module now uses
the stored spec directly with no find_spec call.

Also move _mark_done into the finally block so a module that fails to load
doesn't trigger infinite retry loops on subsequent import attempts.

apply.sh: warn() writes to stdout. Inside $(find_mw_python), stdout is
captured by the command substitution, so any warn() call folded its text
into $PYTHON — causing every subsequent "$PYTHON" invocation to fail. Fix:
emit the fallback warning to stderr (>&2) so it goes to the log without
being captured.
This commit is contained in:
2026-06-15 02:25:51 +00:00
parent 01bf5f8603
commit 2d32c81485
2 changed files with 32 additions and 17 deletions
+2 -1
View File
@@ -53,8 +53,9 @@ find_mw_python() {
fi
# Verify the chosen interpreter can actually import middlewared.
# Use >&2 so this message goes to stderr, not captured by $(...) substitution.
if ! "$py" -c "import middlewared" 2>/dev/null; then
warn "Detected Python '$py' cannot import middlewared; falling back to python3"
echo "WARNING: '$py' cannot import middlewared; falling back to python3" >&2
py="python3"
fi