Repository navigation
Commit b96e522
authored
fix(runtime): drop Java->JS calls into a worker that is shutting down (#2131)
### Description
Fixes a native crash when a Java thread calls into a worker's JS object
while the worker shuts down. Independent of the wrapper lifetime series
(#2125–#2130); found while testing it.
#### The bug
With `enableMultithreadedJavascript`, a Java->JS call on a worker's
object runs on the calling thread under the worker isolate's `Locker`.
`callJSMethodNative` looked the runtime up by id, then waited for that
`Locker`. If the worker was tearing down meanwhile (`DestroyRuntime`
holds the `Locker`), the call got the `Locker` afterwards and read the
context `DestroyRuntime` had just freed: `SIGSEGV` in
`Runtime::GetContext()` under `Java_com_tns_Runtime_callJSMethodNative`,
seen in `testRuntimeBoundDispatch` ("drops calls to an object whose
worker has terminated"). Had it got past that, the worker would go on to
`Isolate::Dispose()` and `delete runtime_` while the call still used
them. `createJSInstanceNative` and `passExceptionToJsNative` take
another thread's runtime the same way, and `generateNewObjectId` reads
it unpinned when Java registers a call's arguments on the calling
thread.
#### The fix
The model the iOS runtime uses for native callbacks into an isolate
(`IsolateGates`/`IsolatePin`):
- Each runtime has a gate keyed by its id (`RuntimeGates`; ids are never
reused), opened in the `Runtime` constructor before the runtime can be
found by id.
- A Java->JS entry into **another thread's** runtime pins the gate
before it waits for the `Locker`; the gate hands out the runtime, so
finding and pinning it is one critical section. Once the `Locker` is
granted the call checks a flag `DestroyRuntime` sets under that
`Locker`. A thread entering its own runtime takes no pin, so the common
paths (the main thread, a worker calling itself, every call when
multithreaded JS is off) are unchanged. A cross-thread call takes the
process-wide gate mutex once, to pin, as `main` took the runtime cache
mutex once: unpinning is one atomic decrement on the gate's state (pin
count and closed bit), and only the last pin of a closed gate takes the
mutex, to wake the teardown.
- The teardown unregisters the runtime first (as before), closes the
gate last in `DestroyRuntime`, still under the `Locker`, and waits for
in-flight pins to drain before `Isolate::Dispose()` (worker teardown and
`UnwindFailedInit`). It holds no `Locker` while it waits, and a pinned
thread lets go as soon as it gets the `Locker` and finds the runtime
destroyed, so the wait cannot deadlock. `~Runtime` also waits and
retires the gate, for an init that failed before the isolate existed.
- `generateNewObjectId` for another thread's runtime bumps the id
counter under the gate mutex, without the `Locker` (the counter needs
none), and returns `ObjectTable.NO_ID` (-1) once the runtime is gone or
closing; `ObjectTable` then registers nothing and the call is dropped.
- A call whose pin is refused or whose runtime is gone is dropped and
logged (`NativeScript discarding call to "…": runtime id=N is shutting
down`), as calls to a terminated worker already are; an unknown runtime
id is dropped too, where it used to throw "Cannot find runtime for id"
into Java.
`__runOnMainThread` keeps its own `IsolateGate` (#2108): it never pins,
so it never delays a worker's disposal, and is dropped if the worker is
closing.
### Testing
- New spec in `testRuntimeBoundDispatch.js` (multithreaded JS only):
four Java threads call into a worker's `Comparable` in a loop, each
passing a new `Object` (so the foreign-thread `generateNewObjectId` path
runs too); the spec waits for a call the live worker answered,
terminates the worker, and waits for every caller to finish on its own
after 20 dropped calls in a row, so the calls keep running through the
teardown (each wait with a 5 s deadline). Every result must be the
answer (42) or a dropped call (0), never a throw, a stuck thread or a
crash; `afterEach` terminates the worker and stops the callers if the
spec timed out. Uses a new `RuntimeBoundHolder` fixture whose joins have
a deadline. Pending when multithreaded JS is off.
- Full suite on an arm64 API 36.1 emulator: 1774 specs, 0 failures, with
multithreaded JS on and off (the new spec pending when off). In the
multithreaded run's logcat, 3 in-flight calls were dropped natively
while overlapping `DestroyRuntime` and 1 `generateNewObjectId` was
refused.
- `ObjectTableTest.registersNothingWhenNoIdIsGiven` (JVM unit test).
- The same spec on unfixed `main` crashed on its first run, with the
trace above (`SIGSEGV` in `Runtime::GetContext()` under
`callJSMethodNative`, on one of the calling threads).
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved reliability when JavaScript calls overlap worker shutdown,
reducing the risk of calls accessing a runtime as it is being disposed.
* Calls made from other threads now return safe fallback results when
the runtime is unavailable, rather than attempting to use it.
* **Tests**
* Added coverage for concurrent calls during worker termination,
including checks for completed calls, dropped calls, and clean comparer
shutdown.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 9a514a7 commit b96e522
12 files changed
Lines changed: 456 additions & 24 deletions
File tree
- test-app
- app/src/main
- assets/app/tests
- java/com/tns/tests
- runtime
- src
- main
- cpp
- java/com/tns
- test/java/com/tns
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| 30 | + | |
24 | 31 | | |
25 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
26 | 39 | | |
27 | 40 | | |
28 | 41 | | |
| |||
65 | 78 | | |
66 | 79 | | |
67 | 80 | | |
| 81 | + | |
68 | 82 | | |
69 | 83 | | |
70 | 84 | | |
71 | 85 | | |
72 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
73 | 90 | | |
74 | 91 | | |
75 | 92 | | |
| |||
92 | 109 | | |
93 | 110 | | |
94 | 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 | + | |
95 | 165 | | |
96 | 166 | | |
97 | 167 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
43 | 121 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
224 | 225 | | |
225 | 226 | | |
226 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
| 141 | + | |
140 | 142 | | |
141 | 143 | | |
142 | 144 | | |
| |||
294 | 296 | | |
295 | 297 | | |
296 | 298 | | |
297 | | - | |
298 | | - | |
299 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
300 | 304 | | |
| 305 | + | |
301 | 306 | | |
302 | 307 | | |
303 | 308 | | |
| |||
895 | 900 | | |
896 | 901 | | |
897 | 902 | | |
898 | | - | |
899 | | - | |
900 | | - | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
901 | 906 | | |
902 | 907 | | |
| 908 | + | |
903 | 909 | | |
904 | 910 | | |
905 | 911 | | |
906 | 912 | | |
907 | 913 | | |
| 914 | + | |
908 | 915 | | |
909 | 916 | | |
910 | 917 | | |
| |||
1279 | 1286 | | |
1280 | 1287 | | |
1281 | 1288 | | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
1282 | 1293 | | |
1283 | 1294 | | |
1284 | 1295 | | |
| |||
1389 | 1400 | | |
1390 | 1401 | | |
1391 | 1402 | | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
1392 | 1407 | | |
1393 | 1408 | | |
1394 | 1409 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
212 | 216 | | |
213 | 217 | | |
214 | 218 | | |
| |||
394 | 398 | | |
395 | 399 | | |
396 | 400 | | |
| 401 | + | |
397 | 402 | | |
398 | 403 | | |
399 | 404 | | |
| |||
0 commit comments