Open jsen- opened 1 year ago
I'm not aware of any v8 limitations on multiple isolates in one thread, but I would strongly recommend against it as we may end up internalizing the the tokio runtime inside of the JsRuntime for performance reasons at some time in the future.
JsRealm
is intended to separate scripts from each other (they use a v8::Context internally), but realms are still in development and may have some sharp edges w.r.t. performance and security. For now, the best supported way to run parallel isolates is one-per-thread.
Thank you for the JsRealm
tip. I don't see the performance nor security as an issue (all scripts will be written internally and will mostly be waiting for API responses). There will be 0 - 300 of them running in parallel, though.
I tried it and it works, but the implementation is kinda crazy :slightly_smiling_face: and I'm still missing the ability to "pause" and "cancel" individual JsRealm
s.
Feel free to close this issue (unless you'd like to keep it open with regards to the segfault). Thanks again
@jsen- I think your problem maybe related to this issue: https://github.com/denoland/rusty_v8/issues/639 .
The root cause is, v8::Isolate::new() included a call of v8::Isolate->Enter() and the drop() method of Isolate included a call of v8::Isolate->Exit(), which required isolates in a thread must destruct in last-in-first-out order.
If this is your case, then the simplest way to fix it is just use FuturesOrdered and its push_front() method to ensure JsRuntimes destruct in reversed order of create. But I recommend you just remove the Enter() and Exit() pair in rusty_v8 source code, as it has no sense in single thread embedding situation.
And as a deno_core user, I really don't like the idea to encapsulate tokio and thread-related things into deno_core. This way will limit the usage of deno_core in a very narrow scope. I beg you core developers to re-consider this idea @mmastrac .
We at https://github.com/dust-tt/dust are running in the same issues. We would like to run isolated pieces of JS code running deno but moving to recent version of deno we run into seg faults as the JsRuntime gets dropped.
The natural next step was to move to one jsRuntime shared through channels but it requires isolatioon and it seems that the Realms API was removed?
Just popping in to 2nd @abadcafe
Inlining the tokio runtime into JsRuntime would definitely narrow the core's possible uses and hurt downstream crates
Same issue here. We tried using FuturesOrdered
but that didn't work. For reference https://github.com/denoland/rusty_v8/issues/1628
I was trying to run multiple instances of
deno_core::JsRuntime
within one tokio task, but got:tested on:
rustc 1.71.1 (eb26296b5 2023-08-03)
andrustc 1.73.0-nightly (1b198b3a1 2023-08-13)
the fault occurs in v8IsolateNew()
Is this supposed to work? Is there a good other way to run multiple completely isolated scripts in parallel? (I'd prefer to avoid creating new thread per runtime, however running multiple scripts/modules within the same runtime would be fine, as long as each has a separate
Global
object)