fix(profiling): prevent lock profiler flush reentrancy - #19856
fix(profiling): prevent lock profiler flush reentrancy#19856KowalskiThomas wants to merge 1 commit into
Conversation
|
a806f47 to
a1dfe85
Compare
Codeowners resolved asResolved from the full PR diff against |
Dependency direction analysis
|
Circular import analysis
|
a1dfe85 to
20a63dd
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 20a63dd9f4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if tid in _FLUSH_SAMPLE_ACTIVE_THREADS: | ||
| return |
There was a problem hiding this comment.
Detect unsafe frame teardown before the first flush
When a profiled lock is first acquired from a __del__ during task/frame teardown—the scenario shown in the commit description—this thread ID is not yet in the set, so the guard admits the sample and still reaches _c_get_task()/sys._getframe() and push_pyframes() with the detached frame that caused the segmentation fault. This only suppresses a flush nested inside another _flush_sample; the added test pre-populates the set and therefore does not exercise the reported entry path. Guard or reject the frame-teardown case itself so the original gRPC/asyncio crash is actually avoided.
Useful? React with 👍 / 👎.
|
This change is marked for backport to 4.14 and it does not conflict with that branch. |
|
This change is marked for backport to 4.11 and it does not conflict with that branch. |
|
This change is marked for backport to 4.12 and it does not conflict with that branch. |
|
This change is marked for backport to 4.13 and it does not conflict with that branch. |
Description
This PR adds a guard in the Lock Profiler to prevent reentrancy in
_flush_sample, which is causing crashes.Some C extensions (e.g. gRPC) acquire a Lock from a
__del__that fires inside asubtype_deallocchain which itself is triggered by a Python frame teardown (e.g._PyFrame_ClearExceptCodeon an asyncio task step). In that context, walking the caller frame withsys._getframeor readingtask._coro.cr_framecan yield aPyFrameObjectthat has just been detached from the interpreter's frame chain, and dereferencing it (e.g. in theisinstancecheck insideSampleHandle.push_pyframes) causes a segmentation fault.Skipping the sample when we detect we are already sampling on this thread avoids those crashes.