Skip to content

apk: the phone's Python is the runtime's (3.14.8), not p4a's 3.14.2 with its incremental GC - #2122

Merged
trissim merged 1 commit into
mainfrom
fork-android-python-runtime
Nov 18, 2026
Merged

trissim merged 1 commit into
mainfrom
fork-android-python-runtime

Conversation

@trissim

@trissim trissim commented Nov 18, 2026

Copy link
Copy Markdown

The phone crashed at 03:48 with SIGSEGV (SEGV_MAPERR, fault addr 0xa8) in qasync's _QThreadWorker. I symbolized its backtrace with the NDK's llvm-addr2line against the build's own libpython3.14.so, which is byte-identical to the one in the dist:

#00 _PyObject_IS_GC      Include/internal/pycore_object.h:837
#01 mark_stacks          Python/gc.c:1509
#02 gc_collect_increment Python/gc.c:1627
    _PyGC_Collect        Python/gc.c:2042
    _Py_HandlePending    Python/ceval_gil.c:1399
    ... gen_send_ex2 / gen_iternext -> PySequence_Tuple

That's CPython 3.14.2's incremental GC, triggered in a worker thread while it built a tuple from a generator. mark_stacks walks every thread's frame stack, and it read a stack slot whose type pointer is NULL (0xa8 is PyTypeObject.tp_flags). The crash is in CPython's own collector, not in a C extension, PySide or our render work. CPython reverted the incremental GC in 3.14.5 after mark_stacks bugs, among them gh-148144, an uninitialized frame->visited on copied generator frames (Python 3.14.5 rolls back the incremental garbage collector; discussion). p4a 58d21141 pins 3.14.2, while the desktop runtime, which compiles the APK's bytecode and runs every bundle check, is 3.14.8.

What changed

  • tools/android/recipes/python3 and hostpython3 (new): p4a's recipes at TOAD_ANDROID_PYTHON. p4a's own patches are applied from p4a's folder, not copied. The two patches p4a wrote against 3.14.2 whose change CPython has since made itself (3.14_armv7l_fix.patch, 3.14_fix_remote_debug.patch) are declared with the source line that carries each change. Each is skipped only where that line is present; otherwise the build fails. Their forward application fails against 3.14.8, and the other four p4a patches apply in sequence with no rejects.
  • tools/android/build.sh reads the runtime's version into TOAD_ANDROID_PYTHON, so the runtime owns which Python ships. After packaging it runs:
  • tools/android/apk_python.py (new), which reads PY_VERSION from the APK's own libpython and fails the build unless it is the runtime's. Run on the current main APK it reports 3.14.2 and fails, as it should.

Evidence

I built in a scratch APP folder seeded from the shared build cache, with python3 and hostpython3's build state removed.

main APK (shared build) this branch
libpython PY_VERSION 3.14.2 3.14.8
GC path in Python/gc.c mark_stacks + gc_collect_increment _PyGC_Collect → gc_collect_main (generational); no mark_stacks in gc.c
APK bytes 207,930,843 208,017,375 (+86,532)
  • The one mark_stacks symbol left in the new libpython is Objects/frameobject.c's, a bytecode stack-depth analysis for f_lineno, not the GC's.
  • build.sh (which runs check.sh first) exited 0. It printed "shipped app ran every view kind and bound key on a scratch bus", then "the APK's interpreter is Python 3.14.8".
  • Not proven here: check.sh and shipped_run run the shipped bundle on the desktop runtime's 3.14.8 interpreter, not the arm64 one. The phone is the only place the new interpreter itself runs. qt start: a native crash leaves every thread's Python stack in the errors folder #2116's fault trace will capture every thread's stack if anything native still faults there.
  • Ratchet, shadow and glyph checks don't apply: no file they read changed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3

…ith its incremental GC

The phone's SIGSEGV at 03:48, symbolized with the NDK's llvm-addr2line against the build's own libpython3.14.so: _PyObject_IS_GC <- mark_stacks <- gc_collect_increment <- _PyGC_Collect <- _Py_HandlePending, in qasync's _QThreadWorker while it built a tuple from a generator. That's CPython 3.14.2's incremental GC reading a NULL type (fault addr 0xa8, tp_flags) off a thread's stack. CPython reverted the incremental GC in 3.14.5. p4a 58d21141 pins 3.14.2; the desktop runtime that compiles the APK's bytecode and runs every bundle check is 3.14.8.

tools/android/recipes/python3 and hostpython3 are p4a's recipes at TOAD_ANDROID_PYTHON, which build.sh reads from the runtime, so the runtime owns which Python ships. p4a's patches are applied from p4a's own folder. After packaging, tools/android/apk_python.py reads PY_VERSION from the APK's libpython and fails the build unless it is the runtime's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3
@trissim
trissim merged commit 4a0772f into main Nov 18, 2026
1 of 2 checks 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.

1 participant