René's URL Explorer Experiment
Title: fix: keep the Worker object rooted until its thread ends, and report the end as nsworkerended by edusperoni · Pull Request #2044 · NativeScript/android · GitHub
Open Graph Title: fix: keep the Worker object rooted until its thread ends, and report the end as nsworkerended by edusperoni · Pull Request #2044 · NativeScript/android
X Title: fix: keep the Worker object rooted until its thread ends, and report the end as nsworkerended by edusperoni · Pull Request #2044 · NativeScript/android
Description: Stacked on #2043 (feat/worker-threads) — merge that first.
What this fixes
terminate() reset the Worker object's Persistent and dropped the registry entry synchronously, while the worker thread was still winding down. From that moment the wrapper was unreachable from native, the end of the worker was unobservable from JS, and any message the worker had already queued on the parent's loop was discarded when it arrived.
Android vs iOS lifetime — what was verified before porting
The iOS change is largely about replacing finalizer resurrection with reachability. Android never had that problem, so most of it does not apply:
The Worker object is already a strong root, from construction. WorkerWrapper::poWorker_ is new Persistent(parentIsolate, workerObject) (WorkerWrapper.cpp) and is never made weak. There is no RootWorkerObject/UnrootWorkerObject to add.
The Worker object is not an ObjectManager object. CallbackHandlers::NewThreadCallback takes args.This() of the plain FunctionTemplate installed in Runtime::PrepareV8Runtime (Runtime.cpp, the Worker constructor block) and hands it to the wrapper; nothing registers it with ObjectManager or marks it for GC. So none of iOS's DataWrapper/ObjectManager refuse-and-re-weaken work has an Android counterpart.
The wrapper itself is shared_ptr-owned by the registry and by the detached thread (Start() captures shared_from_this() and BackgroundLooper holds it for the thread's whole life), so this cannot die under the thread. iOS's "read everything before publishing isDisposed_" hardening exists because a tearing-down iOS parent can delete the wrapper concurrently; that cannot happen here, and it is left alone.
Worker-thread posts already reach the parent only through its event loop. parentTasks_ is a std::weak_ptr captured on the parent's thread at construction, and PostMessageToParent, PassUncaughtExceptionFromWorkerToParent and the thread-exit post all go through it. The only reads of the parent isolate's runtime slot (Runtime::GetRuntime(parentIsolate)) happen inside lambdas that run on the parent's thread. Nothing was wrong; nothing changed.
The change
CallbackHandlers::WorkerObjectTerminateCallback no longer calls WorkerWrapper::ClearWorkerOnParent(id). terminate() only starts the wind-down.
WorkerWrapper::BackgroundLooper's final post to the parent's loop now runs a new WorkerWrapper::NotifyThreadEndedOnParent(workerId): on the parent's thread, with the parent isolate locked and entered, it resolves the wrapper by id, takes the Worker object out of poWorker_, calls WorkerEvents::EmitEnded under a TryCatch (a throwing listener is reported exactly as FireErrorOnParentWorkerObject/FireMessageOnParentWorkerObject report one — ContainUncaughtCallbackException then ReportFromEventLoopEntry), and only then calls ClearWorkerOnParent.
WorkerEvents::EmitEnded mirrors EmitError's shape: a third emitEnded callout cached in WorkerEventsState at WorkerEvents::Init.
js/worker-events.js gains emitEnded(), which dispatches a plain Event("nsworkerended") on the Worker object.
js/node-worker-threads.js: the shim's Worker listens for nsworkerended and reports the exit from there. exit (code 0) is emitted exactly once, for a self-close() as much as for terminate(), and terminate() now resolves from the same notification instead of off a microtask, so nothing the worker sent can arrive after exit.
A parent that is itself tearing down (TerminateChildren → ClearWorkerOnParent on the parent's thread) or whose loop is gone (expired parentTasks_) never delivers the notification. That is deliberate, matches iOS, and is documented as best effort.
Consumer audit for the removed clear. Nothing relied on the persistent being empty after terminate() for correctness: PostMessageToParent already returns early on isTerminating_; every error source is gated at the point of forwarding (CallWorkerScopeOnErrorHandle returns early on IsTerminating(), the unhandled-rejection path in NativeScriptException.cpp checks IsTerminating()/IsDisposed(), and BackgroundLooper's catch checks !isTerminating_), so no error from a terminated worker reaches the parent; the isTerminated private only guards a double terminate(); FireMessageOnParentWorkerObject/FireErrorOnParentWorkerObject keep their empty-persistent guards for the teardown paths that still clear early. The one observable change is the intended one: a message the worker queued before terminate() is now delivered instead of being dropped on arrival, and it is delivered before nsworkerended.
Tests
New Android-only specs in test-app/app/src/main/assets/app/tests/testWorkerLifetime.js (wired in mainpage.js), with workerLifetimeCloseWorker.js and messaging/deadlockChild.js / messaging/deadlockParent.js:
a live Worker survives GC as a WeakMap key (the ephemeron repro, kept as a regression guard; collections are driven from a task via __collect({ execution: "async" }) so conservative stack scanning does not keep the workers alive)
an unreferenced live Worker still answers messages
a terminated Worker becomes collectable — observed after nsworkerended, since the root outlives terminate() by design
a Worker that closed itself becomes collectable
node:worker_threads: exit exactly once on self-close(); exit exactly once on terminate(), after the thread ended, with terminate() resolving after it and a second terminate() resolving immediately
a terminated worker whose dropped message sentinels a port it owns still ends
Full device suite on a Pixel_3a_API_36 arm64 emulator: 1398 specs, 0 failures, 4 skipped (baseline on feat/worker-threads was 1391/0/4; the 7 new specs all ran and passed). npm run lint clean.
Deviations from iOS (ab72efc2)
Not ported: DataWrapper.h, ObjectManager.mm, RootWorkerObject/UnrootWorkerObject, EndWrapperLifetime, selfRef_. Android's persistent is strong from construction and the Worker object is not an ObjectManager object, so there is no weak handle to clear and no resurrection branch to take it off. The wrapper is kept alive by shared_ptrs, so iOS's atomic liveness token is unnecessary — the notification resolves the wrapper through the id-keyed registry instead.
Not ported: PostToRuntimeLoop → PostToLoop / mainLoop_. Android already captures parentTasks_ as a weak_ptr on the parent's thread; the isolate-slot read iOS was removing does not exist here.
Not ported: the isDisposed_ publication reordering. The thread owns a shared_ptr to the wrapper, so nothing can delete it mid-teardown.
EmitEnded lives in WorkerEvents, not Worker. Android splits the worker-events callouts into WorkerEvents.{h,cpp}; that is where EmitMessage/EmitError already are.
docs/knowledge/v8-resurrecting-finalizers.md has no Android counterpart (docs/knowledge/ holds only v8-14-migration.md), so that hunk is skipped. The js/README.md listener-bag rule is reworded to the same effect without referring to a patch document this repo does not carry.
Docs say "from the moment it is constructed" where iOS says "from the moment its thread starts", because Android's persistent is strong from construction rather than from Start().
Tests are in Android style (var/function, jasmine done) and bump jasmine.DEFAULT_TIMEOUT_INTERVAL to 30s the way the other worker suites do; the fixed waits in the node:worker_threads specs are 2000 ms rather than iOS's 800 ms for emulator slowness. The two collectability specs additionally gate on nsworkerended before observing the WeakRef, which iOS approximates with a fixed delay.
Mirrors NativeScript/ios#456.
Summary by CodeRabbit
Bug Fixes
Workers now remain available while termination is in progress and become eligible for garbage collection after their thread ends.
The exit event is reported once after the worker finishes, with exit code 0. Calls to terminate() now resolve when shutdown completes rather than immediately.
Documentation
Clarified when worker exit notifications occur, how termination results are reported, and how worker lifetime relates to garbage collection.
Open Graph Description: Stacked on #2043 (feat/worker-threads) — merge that first.
What this fixes
terminate() reset the Worker object's Persistent and dropped the registry entry synchronously, while the worker thread...
X Description: Stacked on #2043 (feat/worker-threads) — merge that first.
What this fixes
terminate() reset the Worker object's Persistent and dropped the registry entry synchronously, while the worker th...
Opengraph URL : https://github.com/NativeScript/android/pull/2044
X: @github
direct link
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:f39c3a4b-d0b5-e17c-fbc2-5782f3be743d
current-catalog-service-hash ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id D362:1F8F:92FA15:C06A3F:6B0132C5
html-safe-nonce f9c612173da58f60ad04872d7ef069d8ed57e3ec208f3796448733898ced05a7
visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzYyOjFGOEY6OTJGQTE1OkMwNkEzRjo2QjAxMzJDNSIsInZpc2l0b3JfaWQiOiI3NDM4NjQ0NzU1ODU0MDc4NjYxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac 6175c02b440c1541b74917d13870b5e94ebd188e78ef430074d3bf97a75a8358
hovercard-subject-tag pull_request:4509855144
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 ///pull_requests/show/files
fb:app_id 1401488693436528
apple-itunes-app app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2044/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 #2043 (feat/worker-threads) — merge that first.
What this fixes
terminate() reset the Worker object's Persistent and dropped the registry entry synchronously, while the worker thread...
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 :
Skip to content https://github.com/NativeScript/android/pull/2044/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid%2Fpull%2F2044%2Ffiles
GitHubRepository hosting, code review, and collaboration https://github.com/platform
CopilotAI coding agent for tasks, projects, and automated work https://github.com/features/copilot
ActionsCI/CD and automation https://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcement https://github.com/features/code-quality
CodespacesCloud development environments https://github.com/features/codespaces
Code SecurityVulnerability detection and remediation https://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak prevention https://github.com/security/advanced-security/secret-protection
All Features https://github.com/features
Documentation https://docs.github.com/en
Training https://learn.github.com/skills
Certifications https://learn.github.com/credentials
Build with Copilot https://github.com/features/ai
Customize Copilot https://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrations https://github.com/marketplace
Trending repos https://github.com/trending
GitHub Sponsors https://github.com/open-source/sponsors
Developer articles https://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place. https://github.blog/changelog/
Enterprises https://github.com/enterprise
Team https://github.com/team
Startups https://github.com/enterprise/startups
Nonprofits https://github.com/solutions/industry/nonprofits
Healthcare https://github.com/solutions/industry/healthcare
Financial services https://github.com/solutions/industry/financial-services
Manufacturing https://github.com/solutions/industry/manufacturing
Government https://github.com/solutions/industry/government
See all industries https://github.com/solutions/industry
Why GitHub https://github.com/why-github
Customer Stories https://github.com/customer-stories
Business insights https://github.com/solutions/executive-insights
Trust Center https://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code security https://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI features https://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 support https://github.com/enterprise/premium-support
Documentation https://docs.github.com/en
Resource center https://github.com/resources
Blog https://github.blog/
Events & webinars https://github.com/resources/events
Community Forum https://github.com/orgs/community/discussions
Customer Stories https://github.com/customer-stories
Integrations https://github.com/marketplace
Customer support https://help.github.com/
Training https://learn.github.com/skills
GitHub Status https://www.githubstatus.com/
About https://github.com/about
Partners https://github.com/partners
Changelog https://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub. https://githubuniverse.com/
Pricing https://github.com/pricing
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2Fandroid%2Fpull%2F2044%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=NativeScript%2Fandroid
Reload https://github.com/NativeScript/android/pull/2044/files
Reload https://github.com/NativeScript/android/pull/2044/files
Reload https://github.com/NativeScript/android/pull/2044/files
NativeScript https://github.com/NativeScript
android https://github.com/NativeScript/android
Code https://github.com/NativeScript/android
Issues106 (106) https://github.com/NativeScript/android/issues
Pull requests24 (24) https://github.com/NativeScript/android/pulls
Actions https://github.com/NativeScript/android/actions
Projects https://github.com/NativeScript/android/projects
Wiki https://github.com/NativeScript/android/wiki
Security and quality https://github.com/NativeScript/android/security
Insights https://github.com/NativeScript/android/insights
Sign up for GitHub
https://github.com/signup?return_to=%2FNativeScript%2Fandroid%2Fissues%2Fnew%2Fchoose
terms of service https://docs.github.com/terms
privacy statement https://docs.github.com/privacy
Sign in https://github.com/login?return_to=%2FNativeScript%2Fandroid%2Fissues%2Fnew%2Fchoose
edusperoni https://github.com/edusperoni
feat/worker-threads https://github.com/NativeScript/android/tree/feat/worker-threads
fix/worker-strong-lifetime https://github.com/NativeScript/android/tree/fix/worker-strong-lifetime
Conversation
1
https://github.com/NativeScript/android/pull/2044
Commits
1
https://github.com/NativeScript/android/pull/2044/commits
Checks
13
https://github.com/NativeScript/android/pull/2044/checks
Files changed
https://github.com/NativeScript/android/pull/2044/files
Please reload this page https://github.com/NativeScript/android/pull/2044/files
fix: keep the Worker object rooted until its thread ends, and report the end as nsworkerended
https://github.com/NativeScript/android/pull/2044/files#top
Show all changes
1 commit
https://github.com/NativeScript/android/pull/2044/files
59edaa4
fix: keep the Worker object rooted until its thread ends, and report …
edusperoni Sep 11, 2026
https://github.com/NativeScript/android/pull/2044/commits/59edaa4a5da8465e6d43a1c7a1715d2a91bd99f5
Clear filters
https://github.com/NativeScript/android/pull/2044/files
Please reload this page https://github.com/NativeScript/android/pull/2044/files
Please reload this page https://github.com/NativeScript/android/pull/2044/files
worker-threads.md
https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
mainpage.js
https://github.com/NativeScript/android/pull/2044/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5
deadlockChild.js
https://github.com/NativeScript/android/pull/2044/files#diff-c647851004bf5e84193038eeee500ba6249c5d6966d40d701d1c34559af266d6
deadlockParent.js
https://github.com/NativeScript/android/pull/2044/files#diff-4e3dcd6fa1f241f0a011d1357dfe8006c4d0cfc4b672931dc3106807e1d2215e
testWorkerLifetime.js
https://github.com/NativeScript/android/pull/2044/files#diff-0d7cf4e322ca69fd8c4ff4d5e36e27f89dca9bde3aae096fbacf8c590d043aee
workerLifetimeCloseWorker.js
https://github.com/NativeScript/android/pull/2044/files#diff-7cfc7bc9a7642ee6c8a04b19edb9659b9038e9a8600cd04b4f06d7c0430b6dd0
CallbackHandlers.cpp
https://github.com/NativeScript/android/pull/2044/files#diff-a784229ab7689cd9ce3d502557091158b2943269fad47c1891a15a07c355500e
WorkerEvents.cpp
https://github.com/NativeScript/android/pull/2044/files#diff-2e0498536ef0cddf4c00cfdacbfca48ca3b0c16abfdd51f8b86779cd02ec76ab
WorkerEvents.h
https://github.com/NativeScript/android/pull/2044/files#diff-b7d0c8668fa7cce6d8984b50636b8ce6e6752a9b7975c022d12c739816f7b350
WorkerWrapper.cpp
https://github.com/NativeScript/android/pull/2044/files#diff-1344a5b85b9f266cab5024f2d52426ba56e3832c074d2c139fb560bcaaabfdc7
WorkerWrapper.h
https://github.com/NativeScript/android/pull/2044/files#diff-94a94304b1fb4dd29852688ec48aa5583d6b2d6017d149c731cf5f191bf08a2e
README.md
https://github.com/NativeScript/android/pull/2044/files#diff-751334ceaf7705bb581ba52ea1f45b471007312e7194253725e94c12eb8f22fe
events.js
https://github.com/NativeScript/android/pull/2044/files#diff-15bc07870a0153b6b63d42bcfe4c35c81696d01b681d4c816f2206f5cea57f38
node-worker-threads.js
https://github.com/NativeScript/android/pull/2044/files#diff-9b33730ae64035954c1f38349d6cc5aad4133b2b9e9df06cd680893c3c3b596e
worker-events.js
https://github.com/NativeScript/android/pull/2044/files#diff-a166ef75929b5a609376de9eff572a04ab954d45e78106dc71771630d9429ef0
docs/worker-threads.md https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
View file
https://github.com/NativeScript/android/blob/59edaa4a5da8465e6d43a1c7a1715d2a91bd99f5/docs/worker-threads.md
Open in desktop
https://desktop.github.com
https://github.co/hiddenchars
https://github.com/NativeScript/android/pull/2044/{{ revealButtonHref }}
https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
https://github.com/NativeScript/android/pull/2044/files#diff-81eca78391cf7755356eded4d70ab6d0d972af537aecc12964277307636d868e
test-app/app/src/main/assets/app/mainpage.js https://github.com/NativeScript/android/pull/2044/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5
View file
https://github.com/NativeScript/android/blob/59edaa4a5da8465e6d43a1c7a1715d2a91bd99f5/test-app/app/src/main/assets/app/mainpage.js
Open in desktop
https://desktop.github.com
https://github.co/hiddenchars
https://github.com/NativeScript/android/pull/2044/{{ revealButtonHref }}
https://github.com/NativeScript/android/pull/2044/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5
https://github.com/NativeScript/android/pull/2044/files#diff-b7fd6c22bfc29391e9e3ed935d6c6194b1ad595c7592690229952b5ddb99c0d5
test-app/app/src/main/assets/app/tests/messaging/deadlockChild.js https://github.com/NativeScript/android/pull/2044/files#diff-c647851004bf5e84193038eeee500ba6249c5d6966d40d701d1c34559af266d6
View file
https://github.com/NativeScript/android/blob/59edaa4a5da8465e6d43a1c7a1715d2a91bd99f5/test-app/app/src/main/assets/app/tests/messaging/deadlockChild.js
Open in desktop
https://desktop.github.com
https://github.co/hiddenchars
https://github.com/NativeScript/android/pull/2044/{{ revealButtonHref }}
test-app/app/src/main/assets/app/tests/messaging/deadlockParent.js https://github.com/NativeScript/android/pull/2044/files#diff-4e3dcd6fa1f241f0a011d1357dfe8006c4d0cfc4b672931dc3106807e1d2215e
View file
https://github.com/NativeScript/android/blob/59edaa4a5da8465e6d43a1c7a1715d2a91bd99f5/test-app/app/src/main/assets/app/tests/messaging/deadlockParent.js
Open in desktop
https://desktop.github.com
https://github.co/hiddenchars
https://github.com/NativeScript/android/pull/2044/{{ revealButtonHref }}
Please reload this page https://github.com/NativeScript/android/pull/2044/files
Please reload this page https://github.com/NativeScript/android/pull/2044/files
https://github.com
Terms https://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacy https://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Security https://github.com/security
Status https://www.githubstatus.com/
Community https://github.community/
Docs https://docs.github.com/
Contact https://support.github.com?tags=dotcom-footer
Viewport: width=device-width
URLs of crawlers that visited me .