Skip to content

Fix OHOS Worker crash and harden JsHelper error handling#3431

Merged
shlzxjp merged 6 commits into
mainfrom
bugfix/thunderllei_ohos_worker_crash
May 18, 2026
Merged

Fix OHOS Worker crash and harden JsHelper error handling#3431
shlzxjp merged 6 commits into
mainfrom
bugfix/thunderllei_ohos_worker_crash

Conversation

@leiyue123
Copy link
Copy Markdown
Collaborator

当 OHOS 应用从 Worker 线程导入 libpag 时,会触发 "ecma_vm cannot run in multi-thread" 致命崩溃。

根因:JsHelper 里 ConstructorRefMap 是进程级全局表、仅以 class name 为 key 共享所有 napi_env。Worker 线程进入 SetConstructor 重复注册同名 class 时,会用 Worker 自己的 env 去调用 napi_delete_reference 释放主线程 env 创建的 napi_ref,跨 EcmaVM 访问触发 abort。

本 PR 的修复分两层:

一、核心修复

  • 将 ConstructorRefMap 按 napi_env 分区,并加互斥锁保护所有读写。
  • 新增 CleanupConstructorRefs,通过 napi_add_env_cleanup_hook 在每个 env 首次插入时注册;hook 在 env 自身线程上 teardown 期间执行,在该线程上 napi_delete_reference 是安全的。这样既避免了跨线程释放,又防止了 env 销毁后的 ref 泄漏。
  • SetConstructor 改为文件内 static。

二、顺手把相关错误路径补齐(review 过程中发现的配套问题)

  • napi_add_env_cleanup_hook 注册失败时,erase 刚插入的空 entry 并返回 false,避免残留项导致 firstInsert 再也不为 true。
  • napi_create_reference 失败时不再把 nullptr 缓存进 map,避免后续 napi_delete_reference(nullptr) 和 napi_get_reference_value(nullptr) 的未定义行为。
  • DefineClass 现在检查 SetConstructor 的返回值并以 napi_generic_failure 向上传播,避免出现 class 已导出但构造器未缓存的静默模块级故障。
  • 顶层 Init 在任何子模块失败时返回 nullptr,让 import pag 在 JS 侧抛错,而非静默降级成一个残缺的模块。

改动仅限 src/platform/ohos/JsHelper.cpp 与 src/platform/ohos/JPAG.cpp,不影响其它平台。

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.55%. Comparing base (2973416) to head (efce473).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #3431    +/-   ##
========================================
  Coverage   81.55%   81.55%            
========================================
  Files         558      558            
  Lines       51407    51407            
  Branches    13875    13987   +112     
========================================
+ Hits        41924    41927     +3     
+ Misses       6603     6600     -3     
  Partials     2880     2880            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shlzxjp shlzxjp merged commit 25d735f into main May 18, 2026
9 checks passed
@shlzxjp shlzxjp deleted the bugfix/thunderllei_ohos_worker_crash branch May 18, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants