Repository navigation
Conversation
50668b4 to
7443718
Compare
|
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: meta#1853 and getsentry/sentry-react-native#5441. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7443718 to
90501e6
Compare
|
Thanks for the heads-up. Confirmed the same code path exists in |
|
Hello 👋 |
Thanks for the update! We will take a look. |
|
Hello 👋 ! |
lavenzg
left a comment
There was a problem hiding this comment.
Sorry for the late review!
…iler-pthread-kill-1853
Replace the overloaded bool with a tri-state SampleResult enum: - Success: stack walked, record the sample. - ThreadExited: registered thread is gone; skip this profiler but keep the loop running. - Failed: unrecoverable error; stop the timer thread.
|
@lavenzg has imported this pull request. If you are a Meta employee, you can view this in D120366165. |
|
@antonis has updated the pull request. You must reimport the pull request before landing. |
|
I saw a lint failure and pushed a fix 37bb36a I don't have access to the log of the internal linter so feel free to ignore if this is irrelevant. |
|
@lavenzg Happy to help if something more is needed on my side on this 🙇 |
I have imported to internal tool and we will review it and merge it soon. |
|
@lavenzg Thank you for importing this. Since I don't have access to the internal tool output please let me know if there is something that I need to iterate upon 🙇 |
|
Hi @lavenzg 👋 Just a gentle follow-up on this one. I noticed the bot asked for a reimport after my lint fix on Sep 17, so I wanted to check whether the internal diff picked up the latest commits, or if the reimport is still pending. The PR also still shows as "changes requested" from your earlier review. If anything is still outstanding there, or if the internal review raised anything else, please let me know and I'll happily address it. Since I can't see the internal tooling, I'm also glad to rebase or split this up if that makes it easier to land. There's continued interest from users hitting this crash (e.g. getsentry/sentry-react-native#5441), so it would be great to get it in when you have a moment. Thanks again for your time on this 🙇 |
|
@antonis Sorry for the late response. I'm landing this now (with two follow-up commits). It should be in the |
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: