denoland / deno_core

The core engine at the heart of Deno
MIT License
253 stars 85 forks source link

Run deno_core in multi thread on linux cause core dumped #812

Open AnranS opened 1 month ago

AnranS commented 1 month ago
use std::sync::{mpsc, Arc, Mutex};

#[tokio::main]
async fn main() {
  let (tx, rx) = mpsc::channel::<String>();
  let rx = Arc::new(Mutex::new(rx));
  let mut handles = Vec::default();
  for index in 0..8 {
    let qq = rx.clone();
    let handle = std::thread::spawn(move || {
      let js_code = "(function (){
                let sum = 0;
                for(let i = 0;i<1000000000;i++){
                    sum+=1;
                }
                console.log('=====> sum', sum);
                return sum;
            })()";
      let mut js_runtime = deno_core::JsRuntime::new(Default::default());
      loop {
        let xx = { qq.lock().unwrap().recv() };
        match xx {
          Ok(d) => {
            let runtime = tokio::runtime::Builder::new_current_thread()
              .enable_all()
              .build()
              .unwrap();
            runtime.block_on(async {
              println!("====> receive data: {:?} -- thread: {:?}", d, index);
              _ = js_runtime.execute_script("js", js_code);
              println!("======> code exec finish");
            });
          }
          Err(e) => {
            println!("====> error: {:?}", e);
          }
        }
      }
    });
    handles.push(handle);
  }

  for index in 0..100 {
    tx.send(index.to_string()).unwrap();
  }
  for handle in handles {
    handle.join().unwrap();
  }
}

This code is ok run on macos bug core dump on linux. Err: ======> begin task: 1 ======> begin task: 0 [1] 737334 segmentation fault (core dumped) cargo run --package deno_core --example mulit_thread

lucacasonato commented 1 month ago

Can you instantiate the single threaded runtime once, wrapping also the JsRuntime instantiation? I am not sure deno_core likes moving between tokio runtimes.

AnranS commented 1 month ago

Can you instantiate the single threaded runtime once, wrapping also the JsRuntime instantiation? I am not sure deno_core likes moving between tokio runtimes.

I find core dump cause by below code in v8:

  /// Runs the script returning the resulting value. It will be run in the
  /// context in which it was created (ScriptCompiler::CompileBound or
  /// UnboundScript::BindToCurrentContext()).
  #[inline]
  pub fn run<'s>(
    &self,
    scope: &mut HandleScope<'s>,
  ) -> Option<Local<'s, Value>> {
    unsafe {
      scope.cast_local(|sd| v8__Script__Run(self, sd.get_current_context()))
    }
  }

If I run the js_runtime in single thread,it's ok.

jaudiger commented 1 month ago

I'm not sure if I also encountered this problem, but since a recent update of deno_core, I'm now having this runtime exception:

thread 'main' panicked at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.10/src/task.rs:56:3:
assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
[3.35s] 0 / 0+ jobs complete
   0:     0x555558a2b745 - std::backtrace_rs::backtrace::libunwind::trace::h1a07e5dba0da0cd2
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x555558a2b745 - std::backtrace_rs::backtrace::trace_unsynchronized::h61b9b8394328c0bc
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x555558a2b745 - std::sys_common::backtrace::_print_fmt::h1c5e18b460934cff
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x555558a2b745 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1e1a1972118942ad
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x555558a5b20b - core::fmt::rt::Argument::fmt::h07af2b4071d536cd
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/fmt/rt.rs:165:63
   5:     0x555558a5b20b - core::fmt::write::hc090a2ffd6b28c4a
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/fmt/mod.rs:1157:21
   6:     0x555558a2723f - std::io::Write::write_fmt::h8898bac6ff039a23
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/io/mod.rs:1832:15
   7:     0x555558a2b51e - std::sys_common::backtrace::_print::h4e80c5803d4ee35b
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x555558a2b51e - std::sys_common::backtrace::print::ha96650907276675e
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x555558a2cb29 - std::panicking::default_hook::{{closure}}::h215c2a0a8346e0e0
  10:     0x555558a2c86d - std::panicking::default_hook::h207342be97478370
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:298:9
  11:     0x555558a2cfc3 - std::panicking::rust_panic_with_hook::hac8bdceee1e4fe2c
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:795:13
  12:     0x555558a2ce6b - std::panicking::begin_panic_handler::{{closure}}::h00d785e82757ce3c
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:656:13
  13:     0x555558a2bc09 - std::sys_common::backtrace::__rust_end_short_backtrace::h1628d957bcd06996
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:171:18
  14:     0x555558a2cbd7 - rust_begin_unwind
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
  15:     0x5555558e1243 - core::panicking::panic_fmt::hdc63834ffaaefae5
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14
  16:     0x5555558e12ec - core::panicking::panic::h75b3c9209f97d725
[3.45s] 0 / 0+ jobs complete
  17:     0x555555883abb - deno_unsync::task::spawn::h3d4b45d6e4bac497
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.10/src/task.rs:56:3
  18:     0x555555883abb - deno_core::runtime::op_driver::futures_unordered_driver::FuturesUnorderedDriver<C>::spawn_task::h86233caecc35174d
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/op_driver/futures_unordered_driver.rs:111:37
  19:     0x55555605ef6d - deno_core::runtime::op_driver::futures_unordered_driver::FuturesUnorderedDriver<C>::ensure_task::hb57d870c90b5c30e
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/op_driver/futures_unordered_driver.rs:101:7
  20:     0x55555605ef6d - deno_core::runtime::op_driver::futures_unordered_driver::FuturesUnorderedDriver<C>::spawn::hb867395fbc18560f
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/op_driver/futures_unordered_driver.rs:118:10
  21:     0x55555605ef6d - <deno_core::runtime::op_driver::futures_unordered_driver::FuturesUnorderedDriver<C> as deno_core::runtime::op_driver::OpDriver<C>>::submit_op_fallible::h5e38d39f60f07cbc
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/op_driver/futures_unordered_driver.rs:152:26
  22:     0x555555d5a6c4 - deno_core::runtime::op_driver::OpDriver::submit_op_fallible_scheduling::h3b66a7572f8985f8
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/op_driver/mod.rs:110:30
  23:     0x555555d5a6c4 - deno_core::runtime::ops::map_async_op_fallible::h8e82c55b669cf249
                               at /home/container/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_core-0.294.0/runtime/ops.rs:60:19
  24:     0x555555d5a6c4 - brioche_core::script::op_brioche_bake_all::op_brioche_bake_all::slow_function_impl::h866d23a13198d95b
                               at /workspace/crates/brioche-core/src/script.rs:187:1
  25:     0x555555d5a6c4 - brioche_core::script::op_brioche_bake_all::op_brioche_bake_all::v8_fn_ptr::hfc35036d7274a845
                               at /workspace/crates/brioche-core/src/script.rs:187:1
  26:     0x5555577db6df - Builtins_CallApiCallbackGeneric
fatal runtime error: failed to initiate panic, error 5
Aborted

Tokio is configured to create a multithreaded context.