[3.15] gh-151613: Fix remote debugging frame cache ABA (#152448)

gh-151613: Fix remote debugging frame cache ABA (#151614)

The remote debugging frame cache previously used only the last_profiled_frame address as its cache anchor. If a frame returned and a later frame reused the same _PyInterpreterFrame address, the profiler could accept a stale cache entry and splice parent frames from a different call chain into the current stack.

This adds a last_profiled_frame_seq counter next to last_profiled_frame, increments it when the anchor advances, stores it in frame cache entries, and validates cache hits against both the frame address and the sequence. Cache miss walks now copy stack chunks before storing new cache entries so stored continuations come from a stable snapshot. The new regression test exercises alternating call chains and checks that cached stacks never contain frames from both branches.

(cherry picked from commit 8cda6ae2f1)
This commit is contained in:
Pablo Galindo Salgado 2026-06-27 19:42:31 +02:00 committed by GitHub
parent 5d0fa7a5c4
commit 65fbec64f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 180 additions and 54 deletions

View file

@ -1573,6 +1573,8 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->current_frame = &_tstate->base_frame;
// base_frame pointer for profilers to validate stack unwinding
tstate->base_frame = &_tstate->base_frame;
tstate->last_profiled_frame = NULL;
tstate->last_profiled_frame_seq = 0;
tstate->datastack_chunk = NULL;
tstate->datastack_top = NULL;
tstate->datastack_limit = NULL;