fix(profiling): clean up containers post-fork [backport 4.7]#17724
Merged
Conversation
Fixes a crash in the Profiler when fork is called while the Sampling Thread is actively modifying the LRUCache for Frames. Instead of calling std::list::clear() post-fork (which can crash on a corrupted list), use placement new to reinitialise the containers, abandoning the old data as an intentional one-time leak. Also adds prefork/postfork_parent handlers via pthread_atfork so that restart_after_fork uses a pre-saved flag rather than relying on thread_seq_num parity (which prefork itself changes). Backport of PR #17042 to branch 4.7.
Codeowners resolved as |
vlad-scherbich
approved these changes
Apr 24, 2026
Contributor
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.

Backport of PR #17042 (commit 39a5f23) to branch 4.7.
Description
Fixes a crash in the Profiler (~100/week) when
forkis called while the Sampling Thread is actively modifying theLRUCachefor Frames.The root cause:
postfork_childcalledframe_cache_.clear(), which traverses thestd::listto free nodes. If the sampling thread was mid-operation (splice inlookup,emplace_front/pop_backinstore) at fork time, the list's internal pointers can be in a corrupted state in the child, causing the crash.Fix: replace
clear()withpostfork_child()which uses placement new to construct fresh empty containers, abandoning the old data as an intentional one-time leak.Also adds
prefork()/postfork_parent()atfork handlers registered viapthread_atforkso thatrestart_after_forkuses a pre-savedwas_running_at_fork_flag instead of relying onthread_seq_numparity (whichpreforkitself changes).Notes on conflict resolution
No semantic conflicts. The 4.7 branch uses the same
stack_function-name prefix asmain, so the changes apply verbatim.