-
I've made a imple repository showing the error, the essence is very simple use case: use boa_engine::{Context, Source};
#[tokio::main]
async fn main() {
let j1 = tokio::spawn(worker());
tokio::try_join!(j1).unwrap();
}
async fn worker() {
let context = make_context();
let boa_context = tokio::sync::RwLock::new(context);
some_worker_func(&boa_context).await;
}
pub fn make_context<'context>() -> Context<'context> {
let mut context = Context::default();
let src2 = Source::from_bytes(r#"var two = 2;"#);
let _ = context.eval(src2).unwrap();
context
}
pub async fn some_worker_func(
boa_context: &tokio::sync::RwLock<boa_engine::Context<'_>>,
) -> () {
println!("do nothing");
} This fails with many erros like this:
Is this by design? What's the best way to go around this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Short answer: Please use Long answer: The JS execution model pretty much restricts all implementors of the spec to be single-threaded (with respect to the interpreter, but there are other ways to share work between threads like Workers), which is why Boa extensively uses A Boa context is local to the current thread, so if you want to wrap it in a tokio task, you'll need to use the capabilities of tokio to run |
Beta Was this translation helpful? Give feedback.
-
@jedel1043 thank you very much for the detailed explanation. Separate "thank you" for a timely one, since I've started reworking the whole thing to use a |
Beta Was this translation helpful? Give feedback.
Short answer: Please use
tokio::task::spawn_local
instead.Long answer: The JS execution model pretty much restricts all implementors of the spec to be single-threaded (with respect to the interpreter, but there are other ways to share work between threads like Workers), which is why Boa extensively uses
!Send
types and thread locals to maximize performance.A Boa context is local to the current thread, so if you want to wrap it in a tokio task, you'll need to use the capabilities of tokio to run
!Send
futures:task::spawn
for simple tasks andLocalSet
for task groups.