Title: Promise never resolves when resolve() is called inside setTimeout from a background thread · Issue #330 · NativeScript/ios · GitHub
Open Graph Title: Promise never resolves when resolve() is called inside setTimeout from a background thread · Issue #330 · NativeScript/ios
X Title: Promise never resolves when resolve() is called inside setTimeout from a background thread · Issue #330 · NativeScript/ios
Description: A common JS pattern for small delays hangs indefinitely on iOS when the Promise is created on a background thread: async function doSomething() { // wait 25ms for some events to propagate await new Promise(resolve => setTimeout(() => res...
Open Graph Description: A common JS pattern for small delays hangs indefinitely on iOS when the Promise is created on a background thread: async function doSomething() { // wait 25ms for some events to propagate await new...
X Description: A common JS pattern for small delays hangs indefinitely on iOS when the Promise is created on a background thread: async function doSomething() { // wait 25ms for some events to propagate await new...
Opengraph URL: https://github.com/NativeScript/ios/issues/330
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Promise never resolves when resolve() is called inside setTimeout from a background thread","articleBody":"A common JS pattern for small delays hangs indefinitely on iOS when the Promise is created on a background thread:\n\n```javascript\nasync function doSomething() {\n // wait 25ms for some events to propagate\n await new Promise(resolve => setTimeout(() => resolve(), 25));\n\n console.log('never reached');\n}\n```\n\nThis happens when native code (e.g. a NativeScript plugin) invokes JS callbacks on a background thread.\n\n## Reproduction\n\nAny native plugin that calls into JS from a background thread can trigger this. Example with a WebSocket delegate:\n\n```\nNative WebSocket library (background thread)\n │\n ▼\nPlugin's native callback handler\n │ (running on background thread)\n ▼\nNativeScript bridge invokes JS callback\n │ (acquires V8 locker on this background thread)\n ▼\nJS event handler runs\n │\n ▼\nawait new Promise(resolve => {\n setTimeout(() => {\n resolve(); ← never executes\n }, 25);\n});\n```\n\nThe Promise hangs forever.\n\n## Cause\n\nPromiseProxy.cpp captures `CFRunLoopGetCurrent()` when the Promise is constructed:\nhttps://github.com/NativeScript/ios/blob/73332e13667730b3084aa3dd7973685196efa086/NativeScript/runtime/PromiseProxy.cpp#L16\n\nIf `resolve()` is later called from a different thread, it uses `CFRunLoopPerformBlock` to schedule the actual resolution on the original thread's runloop:\nhttps://github.com/NativeScript/ios/blob/73332e13667730b3084aa3dd7973685196efa086/NativeScript/runtime/PromiseProxy.cpp#L36-L37\n\nThe problem: background threads from dispatch queues or thread pools have dormant runloops. No code is calling `CFRunLoopRun()` on them - they just execute tasks and return to the pool. The scheduled block never gets processed.\n\nMeanwhile, `setTimeout` always fires on `Runtime::RuntimeLoop()`:\nhttps://github.com/NativeScript/ios/blob/73332e13667730b3084aa3dd7973685196efa086/NativeScript/runtime/Timers.cpp#L125\n\nTimers are added to that runloop here:\nhttps://github.com/NativeScript/ios/blob/73332e13667730b3084aa3dd7973685196efa086/NativeScript/runtime/Timers.cpp#L252\n\nThis is typically the main thread (captured at Runtime creation), which is different from the background thread where the Promise was created.\n\nThe flow:\n\n1. JS callback runs on background thread B (V8 locker acquired)\n2. Promise created, PromiseProxy captures thread B's runloop\n3. setTimeout schedules timer on Runtime's runloop (thread A)\n4. Callback completes, thread B returns to pool (runloop dormant)\n5. Timer fires on thread A\n6. resolve() called on thread A\n7. PromiseProxy sees different runloops, uses CFRunLoopPerformBlock to schedule on thread B\n8. Thread B's runloop is dormant - nobody is pumping it\n9. Block sits there forever, Promise never resolves\n\n## Suggested fix\n\nInstead of marshaling to the creation thread's runloop (which may be dormant), marshal to the Runtime's runloop which is always active:\n\n```javascript\n// In PromiseProxy.cpp JS source\nlet runtimeRunloop = __getRuntimeRunloop(); // new global exposing Runtime::RuntimeLoop()\n\norigFunc(value => {\n if (isFulfilled()) return;\n let res = resolve;\n markFulfilled();\n\n if (runtimeRunloop === CFRunLoopGetCurrent()) {\n res(value);\n } else {\n CFRunLoopPerformBlock(runtimeRunloop, kCFRunLoopDefaultMode, () => res(value));\n CFRunLoopWakeUp(runtimeRunloop);\n }\n}, ...);\n```\n\nThis ensures Promise resolution always happens on the Runtime's thread (where timers fire), regardless of which thread created the Promise. The Runtime's runloop is always being pumped, so the block will execute.\n\nThis would require exposing `Runtime::RuntimeLoop()` to JS, perhaps via a global function:\nhttps://github.com/NativeScript/ios/blob/73332e13667730b3084aa3dd7973685196efa086/NativeScript/runtime/Runtime.h#L27","author":{"url":"https://github.com/adrian-niculescu","@type":"Person","name":"adrian-niculescu"},"datePublished":"2025-12-18T16:07:29Z","url":"https://github.com/NativeScript/ios/issues/330"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:23e0de08-4b32-5c43-2ffe-602e3c551504 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9170:1C6C3E:19A8CA2:21CB8DF:6B0143BA |
| html-safe-nonce | 515d4c68f7422ef68688d793c5cebf5df921169d76c849fa6ef96c3c3146899a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MTcwOjFDNkMzRToxOUE4Q0EyOjIxQ0I4REY6NkIwMTQzQkEiLCJ2aXNpdG9yX2lkIjoiMjc0NzU5OTk0ODA5MzY3MDMzMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cfa9c426bb529f1b9183dcfa754fb1a56b910ef7bf124271e528d2b21fb4ff85 |
| hovercard-subject-tag | issue:3743775286 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/NativeScript/ios/330/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eeb08992fedf1064ec838c7ede94aa02ef2f9325e3eb34c99728563b41c87ab4/NativeScript/ios/issues/330 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eeb08992fedf1064ec838c7ede94aa02ef2f9325e3eb34c99728563b41c87ab4/NativeScript/ios/issues/330 |
| og:image:alt | A common JS pattern for small delays hangs indefinitely on iOS when the Promise is created on a background thread: async function doSomething() { // wait 25ms for some events to propagate await new... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | adrian-niculescu |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/NativeScript/ios git https://github.com/NativeScript/ios.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 172934359 |
| octolytics-dimension-repository_nwo | NativeScript/ios |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 172934359 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/ios |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| 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 |
| react-app-name | issues |
Links:
Viewport: width=device-width