Closed NightBlaze closed 7 months ago
Hi, you need to be in a async
fn to call spawn functions.
pub async fn spawn_test() {
spawn(async {});
}
etc
I will update doc later to explicitly mention this.
Hi! Thanks for the response. Adding async
works for all except spawn_local
. Adding async
to function which calls spawn_local
has no effect and still returns an error. I'm new to Rust and think that I missed some silly thing.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PanicException(`spawn_local` called from outside of a `task::LocalSet`Backtrace [{ fn: "std::backtrace::Backtrace::create" }, { fn: "std::backtrace::Backtrace::capture" }, { fn: "flutter_rust_bridge::misc::panic_backtrace::PanicBacktrace::setup::{{closure}}" }, { fn: "std::panicking::rust_panic_with_hook" }, { fn: "std::panicking::begin_panic_handler::{{closure}}" }, { fn: "std::sys_common::backtrace::__rust_end_short_backtrace" }, { fn: "_rust_begin_unwind" }, { fn: "core::panicking::panic_fmt" }, { fn: "tokio::task::local::spawn_local_inner" }, { fn: "tokio::task::local::spawn_local" }, { fn: "rust_lib::api::simple::spawn_local_test::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}::{{closure}}::{{closure}}::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}::{{closure}}" }, { fn: "<flutter_rust_bridge::handler::implementation:<…>
Looking at https://docs.rs/tokio/latest/tokio/task/fn.spawn_local.html's "example" section, it seems that Tokio (the runtime flutter_rust_bridge uses by default - but you can customize to use other runtimes) requires some extra code.
Copy of the example in that doc:
use std::rc::Rc;
use tokio::task;
#[tokio::main]
async fn main() {
let nonsend_data = Rc::new("my nonsend data...");
let local = task::LocalSet::new();
// Run the local task set.
local.run_until(async move {
let nonsend_data = nonsend_data.clone();
task::spawn_local(async move {
println!("{}", nonsend_data);
// ...
}).await.unwrap();
}).await;
}
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.
Describe the bug
There is a short docs about
spawn_*
funcs here https://cjycode.com/flutter_rust_bridge/guides/cross-platform/async But it's unclear how to call them.I tried to call then directly but have an error. At the same time I can see tokio threads (is it means that tokio runtime setted up?).
Steps to reproduce
pub fn spawn_blocking_with_test() { spawn_blocking_with( || {}, FLUTTER_RUST_BRIDGE_HANDLER.thread_pool()); }
pub fn spawn_local_test() { spawn_local(async {}); }
Future main() async {
await RustLib.init();
// await spawnTest();
// await spawnBlockingWithTest();
// await spawnLocalTest();
runApp(const MyApp());
}
Expected behavior
No response
Generated binding code
No response
OS
macOS
Version of
flutter_rust_bridge_codegen
2.0.0-dev.22
Flutter info
Version of
clang++
-> % clang++ --version Apple clang version 15.0.0 (clang-1500.1.0.2.5) Target: x86_64-apple-darwin23.2.0 Thread model: posix
Additional context
No response