Fix sampling profiler race when registered thread exits#1995
Open
antonis wants to merge 1 commit into
Open
Conversation
50668b4 to
7443718
Compare
Contributor
|
Hi, the |
If a thread that registered a SamplingProfiler exits while the profiler instance lives on (for example when the host keeps the Runtime alive after the JS thread is gone), the sampling timer thread can call pthread_kill on a stale pthread_t. On Android bionic this aborts the process with "invalid pthread_t 0x... passed to pthread_kill"; on other POSIX platforms it silently signals a stale thread ID. The sampler never synchronized with thread termination, so there was no way to know the registered thread had gone away. Register each profiler with a thread-local ThreadDeathGuard backed by pthread_key_create. Its destructor runs before the pthread_t is reaped and, under the existing runtimeDataLock_, invalidates the profiler's registered thread so subsequent sample attempts skip it cleanly. A shared_ptr-based ProfilerHandle keeps the synchronization state alive across either-end destruction, so there is no UAF if the profiler is destroyed on a different thread than the one that registered it. The sampler now checks for the invalidated registration before calling pthread_kill and also handles an ESRCH return for platforms that return it instead of aborting. Related: facebook#1853 and getsentry/sentry-react-native#5441. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7443718 to
90501e6
Compare
Author
|
Thanks for the heads-up. Confirmed the same code path exists in |
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
Fix a crash in the sampling profiler where
pthread_killis invoked on a thread that has exited but whose correspondingSamplingProfileris still registered. On Android bionic this aborts the process withinvalid pthread_t 0x... passed to pthread_kill; on other POSIX platforms the sampler silently signals a stale thread ID.Root cause: thread termination was never synchronized with the sampling path. If a JS thread exited while a host held the Runtime alive, the sampler would still attempt to sample it.
Fix: register each profiler with a thread-local
ThreadDeathGuardbacked bypthread_key_create. Its destructor, which runs before thepthread_tis reaped, invalidates the registered thread under the existingruntimeDataLock_. Ashared_ptr-basedProfilerHandlekeeps the synchronization state alive across either-end destruction, so there is no UAF if the profiler is destroyed on a different thread than the one that registered it. The sampler now skips any profiler whose registered thread has exited, and also returns cleanly on a non-fatalESRCHfrompthread_killon platforms where that window is observable.Related: #1853 and getsentry/sentry-react-native#5441.
Test Plan
SamplingProfilerTest.SamplingAfterRegisteredThreadExitDoesNotCrashreproduces the scenario deterministically on all POSIX platforms and asserts that the registered thread is invalidated afterstd::thread::join().SamplingProfilerTestsuite continues to pass.static_h: