Use runtime offsets for interpreter state access#861
Draft
jonashaag wants to merge 1 commit into
Draft
Conversation
This was referenced Jul 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_Py_DebugOffsetsthreads_head,imports_modules, andceval_giloffsets for interpreter validation, sampling, thread-name lookup, and GIL lookup_PyRuntimethrough either its symbol or the binary'sPyRuntimesection, retaining the existing generated-binding fallback when runtime metadata is unavailableCloses #852.
Motivation
The generated Python 3.14 bindings assume
PyInterpreterState.imports.modulesis at offset 7664. A Python 3.14.6 runtime reports offset 7688 through_Py_DebugOffsets. Reading the generated offset produces an invalidsys.modulespointer andEFAULT.Besides losing thread names, this is expensive because the empty thread-name cache causes py-spy to retry walking remote
sys.moduleson every sample. In an observed 100 Hz workload, affected 100-sample windows spent 3.2–4.3 seconds in thread-name lookup while frame, code, filename, and line-table reads took about 90 ms. Using the runtime offset resolved the thread name once, produced about 1,493 subsequent cache hits, and removed sampling-lag warnings.CPython places
_Py_DebugOffsetsfirst in_PyRuntimespecifically so out-of-process debuggers can locate fields without relying on a build-specific struct layout. py-spy already usedruntime_state.interpreters_headfrom this table. This change retains the interpreter-state offsets it also needs and uses them consistently.This is narrower than the full offset-based stack/object reader proposed in #831: frame and object access remains on the existing trait-based path. Unlike #855, it does not add another generated binding for each affected Python patch/build layout.
Validation
pre-commit run --all-filescargo test --releasewith Python 3.13cargo test --libcargo check --all-targetscargo check --lib --no-default-features