Repository navigation
Commit 2751477
Fix sampling profiler race when registered thread exits (#1995)
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 `pthread_kill` either signals whichever thread now has that ID or delivers nothing. In the second case the sampler thread waits forever for the signal handler while holding `profilerLock_`, which hangs `SamplingProfiler::disable()` and the next GC of every profiled runtime.
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.
Pull Request resolved: #1995
Test Plan:
- New unit tests `SamplingProfilerTest.SamplingAfterRegisteredThreadExitDoesNotCrash` and `SamplingProfilerTest.SamplingSkipsExitedThreadButSamplesOthers` reproduce the scenario deterministically on all POSIX platforms. The first joins the worker thread that last registered the profiler, samples for 50 ms, and expects no samples. The second expects a profiler on a live thread to keep collecting samples while the exited thread's profiler is skipped.
- Existing `SamplingProfilerTest` suite continues to pass.
- Local build and test run on macOS arm64 (Xcode 16.4, ninja) against `static_h`, before `SamplingSkipsExitedThreadButSamplesOthers` was added:
```
$ 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.
```
- Re-run on this diff (macOS arm64, CMake ASan Debug build with `-O1`): `SamplingProfilerTest.*` runs 5 tests, all passing. Without the fix, both new tests fail because the exited thread's profiler collects samples. The full `HermesVMRuntimeTests` suite passes (405 tests).
Reviewed By: avp
Differential Revision: D120366165
Pulled By: lavenzg
fbshipit-source-id: 09c7af450055dfda1c05d3f4e28c8f05620a603c1 parent c319a1a commit 2751477
5 files changed
Lines changed: 368 additions & 24 deletions
File tree
- lib/VM/Profiler
- unittests/VMRuntime
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| 33 | + | |
| 34 | + | |
31 | 35 | | |
32 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
33 | 43 | | |
34 | 44 | | |
35 | 45 | | |
| |||
39 | 49 | | |
40 | 50 | | |
41 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
42 | 65 | | |
43 | 66 | | |
44 | 67 | | |
45 | 68 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
49 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
50 | 78 | | |
51 | 79 | | |
52 | 80 | | |
| |||
78 | 106 | | |
79 | 107 | | |
80 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
81 | 193 | | |
82 | 194 | | |
83 | 195 | | |
84 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
85 | 204 | | |
86 | 205 | | |
87 | 206 | | |
| |||
92 | 211 | | |
93 | 212 | | |
94 | 213 | | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
95 | 220 | | |
96 | 221 | | |
97 | 222 | | |
| |||
210 | 335 | | |
211 | 336 | | |
212 | 337 | | |
213 | | - | |
| 338 | + | |
| 339 | + | |
214 | 340 | | |
215 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
216 | 353 | | |
217 | 354 | | |
218 | 355 | | |
219 | 356 | | |
220 | 357 | | |
221 | 358 | | |
222 | 359 | | |
223 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
224 | 370 | | |
225 | 371 | | |
226 | 372 | | |
227 | 373 | | |
228 | | - | |
| 374 | + | |
229 | 375 | | |
230 | 376 | | |
231 | 377 | | |
232 | 378 | | |
233 | 379 | | |
234 | 380 | | |
235 | 381 | | |
236 | | - | |
| 382 | + | |
237 | 383 | | |
238 | 384 | | |
239 | 385 | | |
| |||
250 | 396 | | |
251 | 397 | | |
252 | 398 | | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
257 | 409 | | |
258 | 410 | | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
259 | 427 | | |
260 | 428 | | |
261 | 429 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
69 | 77 | | |
70 | | - | |
71 | 78 | | |
72 | 79 | | |
73 | 80 | | |
74 | 81 | | |
75 | | - | |
| 82 | + | |
76 | 83 | | |
77 | 84 | | |
78 | 85 | | |
| |||
97 | 104 | | |
98 | 105 | | |
99 | 106 | | |
100 | | - | |
101 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
102 | 113 | | |
103 | 114 | | |
104 | 115 | | |
| |||
125 | 136 | | |
126 | 137 | | |
127 | 138 | | |
128 | | - | |
| 139 | + | |
129 | 140 | | |
130 | 141 | | |
131 | 142 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
23 | 37 | | |
24 | 38 | | |
25 | 39 | | |
| |||
116 | 130 | | |
117 | 131 | | |
118 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
119 | 147 | | |
120 | 148 | | |
121 | 149 | | |
| |||
125 | 153 | | |
126 | 154 | | |
127 | 155 | | |
128 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
129 | 161 | | |
130 | 162 | | |
131 | 163 | | |
| |||
158 | 190 | | |
159 | 191 | | |
160 | 192 | | |
161 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
162 | 198 | | |
163 | 199 | | |
164 | 200 | | |
| |||
0 commit comments