Title: fix(worker): drop callbacks from closing workers by edusperoni · Pull Request #2108 · NativeScript/android · GitHub
Open Graph Title: fix(worker): drop callbacks from closing workers by edusperoni · Pull Request #2108 · NativeScript/android
X Title: fix(worker): drop callbacks from closing workers by edusperoni · Pull Request #2108 · NativeScript/android
Description: Description Supersedes #2068 with a different approach to the same two crashes. Race on worker shutdown. RunMainThreadEntry read a worker's isolate from the callback cache, released cacheMutex_, and only then took a v8::Locker on it. A worker shutting down in that window (DestroyRuntime → Dispose) freed the isolate first, and the main thread crashed in v8::Locker::Initialize. #2068 fixed this by making the worker wait for in-flight main-thread holds before disposing. That keeps the isolate alive for a caller that will then only find its entry gone: the main thread still locks and enters a worker that was told to terminate, and teardown waits on main-thread progress. This PR drops the call instead: Each worker owns an IsolateGate (std::mutex + open), held by shared_ptr in the cache entry so it outlives the isolate. The main thread takes the gate's mutex, returns if the gate is closed, and releases it only once it owns the worker's Locker. Worker shutdown closes the gate before it takes its teardown Locker, while holding no Locker. Once closed, the main thread never touches the isolate. The worker never waits for the main thread beyond the Locker contention that already existed. Right after closing the gate, shutdown takes and releases the worker's Locker, which waits out a callback that already owned it. Without that, such a callback kept running beside the inspector teardown and the Java detach. The gate is per worker and never taken while holding that worker's Locker, so there is no lock-order cycle. A global mutex would deadlock against a worker that calls __runOnMainThread while holding its own Locker. Main-isolate entries (multithreaded JS) carry no gate and are unchanged. Throwing worker callbacks. Under uncaughtErrorPolicy: "throw", a worker callback's uncaught error was rethrown on the main thread as a Java exception. That exception resolved a handle from the worker isolate against the main one and crashed (SIGSEGV in NativeScriptException::ReThrowToJava). Worse, it would have taken the app down for an error that belongs to the worker. It now goes through the worker's own error path (CallWorkerScopeOnErrorHandle: the worker's onerror, then the parent's Worker object), as the worker's message dispatch already does. Under the default report policy nothing changes. Does your commit message include the wording below to reference a specific issue in this repo? No issue; supersedes #2068. Related Pull Requests #2068 (superseded) Does your pull request have unit tests? No new spec. The race window is a few instructions wide, and the throw policy can't run in the device suite because it crashes the app on purpose (see testUncaughtErrorPolicy.js). Verified manually with a focused repro on an arm64 API 35 emulator, with usleep(200000) injected in RunMainThreadEntry right after the entry lookup: Repro main this PR Worker posts __runOnMainThread(cb), busy-waits 50 ms, then close() (x5) SIGSEGV in v8::Locker::Initialize no crash, cb dropped uncaughtErrorPolicy: "throw", worker's __runOnMainThread callback throws SIGSEGV in NativeScriptException::ReThrowToJava parent Worker.onerror gets Uncaught Error: boom-from-worker-callback, app alive Worker posts a callback that busy-waits 300 ms, then close()s (no injected sleep) teardown continues while the callback runs (from the code, not measured) gate closes at +0 ms, callback runs +6 to +307 ms, teardown resumes at +307 ms The full device suite passes (runtestsAndVerifyResults: 1600/1600, rebased on #2113). Summary by CodeRabbit Bug Fixes Worker callbacks are safely discarded when a worker is shutting down, preventing them from running against a disposed runtime. Worker shutdown waits for in-flight main-thread callbacks before continuing cleanup. Uncaught exceptions from worker callbacks now follow the worker’s error-handling path instead of being raised as Java exceptions on the main thread. Callbacks from the main runtime retain their existing exception behavior.
Open Graph Description: Description Supersedes #2068 with a different approach to the same two crashes. Race on worker shutdown. RunMainThreadEntry read a worker's isolate from the callback cache, released cacheMutex_...
X Description: Description Supersedes #2068 with a different approach to the same two crashes. Race on worker shutdown. RunMainThreadEntry read a worker's isolate from the callback cache, released cacheMu...
Opengraph URL: https://github.com/NativeScript/android/pull/2108
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:574508da-dabe-2916-d021-5dc3578b3070 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D224:785A9:167DBA6:1C87EA4:6B013588 |
| html-safe-nonce | 8996be25c69ee7bdf3b752285ebd46f7b67a690c8b8fd2bc9d846085a2e6589a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjI0Ojc4NUE5OjE2N0RCQTY6MUM4N0VBNDo2QjAxMzU4OCIsInZpc2l0b3JfaWQiOiI1OTAzODgwODg4ODk4MzY4OTA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | bbb7fce252745ba127f817248b19a1c1b04025e9b75d433946f68af1e8b70755 |
| hovercard-subject-tag | pull_request:5267546928 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2108/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description Supersedes #2068 with a different approach to the same two crashes. Race on worker shutdown. RunMainThreadEntry read a worker's isolate from the callback cache, released cacheMutex_... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width