Skip to content

Fix sampling profiler race when registered thread exits - #1995

Closed
antonis wants to merge 8 commits into
meta:static_hfrom
antonis:fix-sampling-profiler-pthread-kill-1853
Closed

antonis wants to merge 8 commits into
meta:static_hfrom
antonis:fix-sampling-profiler-pthread-kill-1853

Conversation

@antonis

@antonis antonis commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix a crash in the sampling profiler where pthread_kill is invoked on a thread that has exited but whose corresponding SamplingProfiler is still registered. On Android bionic this aborts the process with invalid 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 ThreadDeathGuard backed by pthread_key_create. Its destructor, which runs before the pthread_t is reaped, invalidates the registered thread under the existing runtimeDataLock_. 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 skips any profiler whose registered thread has exited, and also returns cleanly on a non-fatal ESRCH from pthread_kill on platforms where that window is observable.

Related: #1853 and getsentry/sentry-react-native#5441.

Test Plan

  • New unit test SamplingProfilerTest.SamplingAfterRegisteredThreadExitDoesNotCrash reproduces the scenario deterministically on all POSIX platforms and asserts that the registered thread is invalidated after std::thread::join().
  • Existing SamplingProfilerTest suite continues to pass.
  • Local build and test run on macOS arm64 (Xcode 16.4, ninja) against static_h:
    $ ninja -C build HermesVMRuntimeTests
    [484/484] Linking CXX executable unittests/VMRuntime/HermesVMRuntimeTests
    
    $ ./build/unittests/VMRuntime/HermesVMRuntimeTests --gtest_filter='SamplingProfilerTest.*'
    [==========] Running 4 tests from 1 test case.
    [----------] 4 tests from SamplingProfilerTest
    [ RUN      ] SamplingProfilerTest.Invariants
    [       OK ] SamplingProfilerTest.Invariants (96 ms)
    [ RUN      ] SamplingProfilerTest.RegisterDifferentThread
    [       OK ] SamplingProfilerTest.RegisterDifferentThread (29 ms)
    [ RUN      ] SamplingProfilerTest.RegisterIdenticalThread
    [       OK ] SamplingProfilerTest.RegisterIdenticalThread (30 ms)
    [ RUN      ] SamplingProfilerTest.SamplingAfterRegisteredThreadExitDoesNotCrash
    [       OK ] SamplingProfilerTest.SamplingAfterRegisteredThreadExitDoesNotCrash (85 ms)
    [==========] 4 tests from 1 test case ran. (240 ms total)
    [  PASSED  ] 4 tests.
    
    $ ./build/unittests/VMRuntime/HermesVMRuntimeTests
    [==========] 351 tests from 79 test cases ran.
    [  PASSED  ] 351 tests.
    

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Apr 23, 2026
@antonis
antonis force-pushed the fix-sampling-profiler-pthread-kill-1853 branch from 50668b4 to 7443718 Compare April 24, 2026 08:37
@antonis
antonis marked this pull request as ready for review April 24, 2026 08:52
@tmikov

tmikov commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Hi, the main branch is not actively maintained, so no guarantees about when this can be reviewed or merged. Does the problem exist in V1?

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>
@antonis
antonis force-pushed the fix-sampling-profiler-pthread-kill-1853 branch from 7443718 to 90501e6 Compare April 28, 2026 09:57
@antonis
antonis changed the base branch from main to static_h April 28, 2026 09:57
@antonis
antonis marked this pull request as draft April 28, 2026 09:57
@antonis

antonis commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up. Confirmed the same code path exists in static_h (same pthread_kill(posixProfiler->currentThread_, SIGPROF) call, no thread-death synchronization), so I've retargeted this PR to static_h and force-pushed 🙇

@antonis
antonis marked this pull request as ready for review April 28, 2026 10:03
@antonis

antonis commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Hello 👋
I was wondering if there is any feedback on this PR or guidance on how to get it merged 🙇

@lavenzg

lavenzg commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Hello 👋 I was wondering if there is any feedback on this PR or guidance on how to get it merged 🙇

Thanks for the update! We will take a look.

@yonitou

yonitou commented Jul 24, 2026 •

Copy link
Copy Markdown

Hello 👋 !
Any news on this one ? We are experiencing the same issue described here

@lavenzg lavenzg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review!

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.
@antonis
antonis requested a review from lavenzg September 15, 2026 08:46
@meta-codesync

meta-codesync Bot commented Sep 16, 2026

Copy link
Copy Markdown

@lavenzg has imported this pull request. If you are a Meta employee, you can view this in D120366165.

@facebook-github-tools

Copy link
Copy Markdown

@antonis has updated the pull request. You must reimport the pull request before landing.

@antonis

antonis commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@antonis

antonis commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

@lavenzg Happy to help if something more is needed on my side on this 🙇

@lavenzg

lavenzg commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@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.

@antonis

antonis commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@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 🙇

@antonis

antonis commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

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 🙇

@lavenzg

lavenzg commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

@antonis Sorry for the late response. I'm landing this now (with two follow-up commits). It should be in the static_h branch soon. To fix the crash in production, we would need to sync this into the stable branch. I need to check if that's doable.

@meta-codesync

meta-codesync Bot commented Nov 16, 2026

Copy link
Copy Markdown

@lavenzg merged this pull request in 2751477.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants