Title: feat: MessagePort, MessageChannel, BroadcastChannel, and node:worker_threads by edusperoni · Pull Request #2043 · NativeScript/android · GitHub
Open Graph Title: feat: MessagePort, MessageChannel, BroadcastChannel, and node:worker_threads by edusperoni · Pull Request #2043 · NativeScript/android
X Title: feat: MessagePort, MessageChannel, BroadcastChannel, and node:worker_threads by edusperoni · Pull Request #2043 · NativeScript/android
Description: Stacked on #2040 (feat/dom-exception-serializable) — merge that first.
What this adds
Native MessagePort / MessageChannel / BroadcastChannel / MessageEvent (all lazy globals — zero boot cost), node:worker_threads, and Worker + worker global scope as real EventTargets.
The native core is Node's node_messaging design without libuv: an isolate-free PortData (mutex-guarded queue, sibling-group entanglement) under a per-isolate NativeMessagePort whose wake primitive is a coalesced EventLoop::PostInternal — producers never take a foreign isolate's Locker. Pairwise channels and named broadcast groups share one SiblingGroup mechanism (the pairwise-vs-broadcast close difference is a single guard, as in Node). Ports transfer through postMessage (including Worker.postMessage) and structuredClone as host-object tag 2: index in-stream, PortData out-of-band, nothing detached until the whole graph has serialized, received ports pre-constructed before ReadValue. A transferred port carries its queued backlog and drains after adoption on a later turn, per spec.
worker.onmessage / scope onmessage are now HTML event-handler IDL attributes (defineEventHandler, position-fixed ordering interleaving with addEventListener), and delivery dispatches real MessageEvents with event.ports populated. First message listener starts a port; receiveMessageOnPort does forced sync drains.
docs/worker-threads.md has the full real-vs-shim table and every documented deviation. Highlights: real MessageChannel/MessagePort/BroadcastChannel/receiveMessageOnPort/threadId/isMainThread/set-/getEnvironmentData/markAsUntransferable/markAsUncloneable; parentPort is a bridge; Worker is a thin emitter wrapper that rejects unsupported options loudly and forwards the rest of the option bag (so androidPriority reaches the runtime's own constructor); postMessageToThread/moveMessagePortToContext throw; locks absent.
Fixed in passing
Worker error propagation (pre-existing bugs). CallWorkerScopeOnErrorHandle forwarded BOTH the scope handler's thrown error and the original. A throwing scope handler now forwards its own error once and nothing else, in all three worker error paths — the scope handler, the entry-rejection reporter in WorkerWrapper.cpp, and the unhandled-rejection tracker in NativeScriptException.cpp. A worker with no scope onerror still reaches the parent.
Parent-side delivery is a real cancelable ErrorEvent on the Worker EventTarget, so worker.addEventListener('error') works, in registration order; handled = preventDefault() or a truthy onerror return. error is null (only primitives cross isolates); stackTrace is a documented NS extension. An error the Worker object leaves unhandled is dispatched as an ErrorEvent on the parent's global scope per HTML, and logged if nothing handles it there.
EventLoop::Shutdown moves the dropped lanes out and destroys them after releasing the mutex. A dropped message carrying a transferred port sentinels the port's sibling, which posts to the sibling's loop; when that sibling belonged to the isolate shutting down, the post re-entered the held (non-recursive) mutex. The invariant is recorded in the class comment.
ConcurrentQueue::Terminate empties the queue and destroys the messages outside both locks, and a push racing it is dropped under the queue mutex. Ports and buffers transferred to a worker terminated before its entry settled were pinned for the wrapper's lifetime and the sibling never received close.
AbortSignal#onabort refactored onto the shared defineEventHandler (−42 lines).
Post-write revalidation now covers buffers as well as ports: a getter that detached a listed ArrayBuffer while the graph was written used to hand the receiver zero bytes silently.
Deserialize records which adopted ports the stream referenced; callers that surface no port list (structuredClone, receiveMessageOnPort) close the rest on arrival, and a read that fails after adoption closes every port it adopted.
Tests
Full device suite on a Pixel_3a_API_36 arm64 emulator: 1391 specs / 0 failures / 4 skipped (baseline before this change: 1216 / 0 / 4). npm run lint clean.
All five shared messaging suites ran (confirmed in the results XML, not pending): MessageChannel 45, MessageEvent 29, NodeWorkerThreads 36, BroadcastChannel 17, WorkerEvents 15. Plus 17 Android-only specs in tests/testMessaging.js (transfer-list edges, handler-attribute enabling, MessagePort.onclose, the empty-name BroadcastChannel group, the parentPort emitter surface, the two worker error paths, and the AbortSignal handler-attribute GC accounting) and 3 messaging canary specs in testRuntimeImplementedAPIs.js. The 4 skips are the pre-existing ones; the known __time flake did not fire.
Deviations from NativeScript/ios#454
No g_states registry in Messaging.cpp. iOS needs a process-wide Isolate* -> MessagingState* map because its Caches is invalidated before CloseAllPorts runs. On Android Runtime::DestroyRuntime releases RuntimeState in its very last statement, long after CloseAllPorts, so RuntimeState::For
Open Graph Description: Stacked on #2040 (feat/dom-exception-serializable) — merge that first. What this adds Native MessagePort / MessageChannel / BroadcastChannel / MessageEvent (all lazy globals — zero boot cost), node...
X Description: Stacked on #2040 (feat/dom-exception-serializable) — merge that first. What this adds Native MessagePort / MessageChannel / BroadcastChannel / MessageEvent (all lazy globals — zero boot cost), node...
Opengraph URL: https://github.com/NativeScript/android/pull/2043
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:9093d895-bb0f-509f-b929-af7b48fa75c0 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 99BC:A20F9:14E5423:1BB710A:6B0128B2 |
| html-safe-nonce | 9fd92146ef45a00fe9dfadf6f1e0e26e2ded40555374fb5106cc710a6bc77b60 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OUJDOkEyMEY5OjE0RTU0MjM6MUJCNzEwQTo2QjAxMjhCMiIsInZpc2l0b3JfaWQiOiI2MTgxOTA3MDU0MTE0OTczODc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2ad3bf3987ee80344ca79dc7b9e0fceb0f59995ca8e7eadb59361c279ea499ad |
| hovercard-subject-tag | pull_request:4509779083 |
| 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/2043/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 | Stacked on #2040 (feat/dom-exception-serializable) — merge that first. What this adds Native MessagePort / MessageChannel / BroadcastChannel / MessageEvent (all lazy globals — zero boot cost), node... |
| 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