ohos-rs / example

A set of exmaples with ohos-rs for HarmonyOS
MIT License
7 stars 1 forks source link

rust子线程调用Call或者call_without_args 发生崩溃 #25

Closed liujingxuan closed 1 week ago

liujingxuan commented 1 week ago

在rust中创建子线程,调用Call或者call_without_args崩溃,异常堆栈如下: 11-14 17:43:43.285 19505-19588 C03F00/com.qun...s/ArkCompiler com.qunar.hos F [default] [CheckThread:207] Fatal: ecma_vm cannot run in multi-thread! thread:19593 currentThread:19588 11-14 17:43:43.285 19505-19588 C03F00/MUSL-SIGCHAIN com.qunar.hos E signal_chain_handler call 2 rd sigchain action for signal: 6 sca_sigaction=5a97b29874 noreturn=0 FREEZE_signo_6 thread_list_lock_status:-1 tl_lock_count=0 tl_lock_waiters=0 tl_lock_tid_fail=-1 tl_lock_count_tid=-1 通过查找,这个函数可能没有使用napi_call_threadsafe_function,还是我使用又问题啊。 `pub fn call_noargs_arkts_func(method:String) ->String{ let global_runtime_clone = Arc::clone(&GLOBAL_RUNTIME); let mut runtime_guard = global_runtime_clone.lock().unwrap(); if runtime_guard.is_none() { *runtime_guard = Some(ArkRuntime::new().unwrap()); } let runtime = runtime_guard.as_ref().unwrap(); let module = runtime.load_with_info("entry/src/main/ets/Test", "com.example.qptest/entry").unwrap(); let ret: String = module.call_withoutargs::<, String>(&method).unwrap(); return ret;

}`

github-actions[bot] commented 1 week ago

@richerfu Please check this issue, thanks.

richerfu commented 1 week ago

新版本加入了对 env 调用的多线程检测导致崩溃。实际上示例代码在之前的版本是个擦边球的实现,加载的模块和函数不能跨线程调用。

liujingxuan commented 1 week ago

好的好的,感谢啊。那我如何在rust子线程中调用arkts代码呢

liujingxuan commented 1 week ago

初始化和加载Module放到主线程,call方法调用放到子线程是否可行呢

richerfu commented 1 week ago

均在子线程执行即可。

liujingxuan commented 1 week ago

好的好的,感谢啊,我画蛇添足了。每次新new runtime就行了。二维码过期了,可以更新下吗

richerfu commented 1 week ago

已更新 晚些时候更新示例