-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple JSRuntime
in a single tokio runtime
#708
Comments
I've prepared a minimal reproducible example: new Promise(async r => {
let i = 0;
while (r < 100) {
await Deno.core.opAsync('random_number');
i += 1;
}
r();
}); use deno_core::{extension, op2, PollEventLoopOptions};
use deno_core::{JsRuntime, RuntimeOptions};
use rand::Rng;
use tokio::runtime::{self, Runtime};
#[op2(async)]
async fn random_number() -> i32 {
let r: i32 = rand::random();
tokio::time::sleep(std::time::Duration::from_millis(rand::thread_rng().gen_range(0..100))).await;
r
}
extension! {
utils,
ops = [random_number]
}
const SCRIPT: &'static str = include_str!("./script.js");
fn main() {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<usize>();
for id in 0..100 {
std::thread::spawn(move || {
let rt = runtime::Builder::new_current_thread().enable_all().build().unwrap();
rt.block_on(async {
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![utils::init_ops()],
..RuntimeOptions::default()
});
println!("[{id}] before execute_script");
runtime.execute_script("lol", SCRIPT).unwrap();
println!("[{id}] after execute_script");
runtime.run_event_loop(Default::default()).await.unwrap();
println!("[{id}] after run_event_loop");
});
println!("[{id}] dropping");
});
}
} |
I have only been studying Deno for a short time and have similar ideas as you. Do you know how to implement this issue ? |
@sahandevs Thank you, I found that the runtime and opstate of the worker are globally shared, while 'Deno. cwd()' references the global opstate. The main process of deno itself is also a worker, so you can implement your requirements from the worker level. |
@sahandevs I have found an open-source project based on deno_core, which you can refer to: edge-runtime |
I was experimenting with
deno
anddeno_core
to implement a runtime similar to what workerd has. Basically for different script it creates different isolates.At first i tried creating a single tokio runtime and create worker like this:
it works fine while running or adding new scripts, but if the
MainWorker
drops, i get the following segfault:and the output of the debug version of v8 is:
after some investigation i saw the comment in deno code that "we enter isolates on creation" and "exit on drop". I assumed this would conflict a lot with the setup i had so i decided to create a new thread per script with the following setup:
which resulted in the following segfault:
after some trial and error i've discovered adding
unsafe { V8::dispose() };
before creating the worker fixes the issue. but reading theV8::dispose
and seeing the use of static i feel I shouldn't do this.Now for my main question! I was wondering if such setups like creating multiple isolates and using them in the process or even better in the same tokio runtime/thread is possible in deno?
The text was updated successfully, but these errors were encountered: