DenoDDeno
Powered by
Damian ReevesD
Deno•2y ago•
11 replies
Damian Reeves

Using Deno core in an app that already had its own tokio runtime

I am using the deno_core and deno_ast crates to "Roll my own JavaScript runtime", as seen here (https://github.com/denoland/roll-your-own-javascript-runtime/blob/main/src/main.rs#L153-L159 ), but I don't know how to launch my deno runtime properly within my existing tokio runtime.

I've tried using a
Handle
Handle
and spawning a thread and using block_on, like below:

pub(crate) fn run_js(args: &RunArgs) {
    let file_path = args.file.to_owned().into_string().unwrap();
    let handle = tokio::runtime::Handle::current();
    std::thread::spawn(move || {
        // Using Handle::block_on to run async code in the new thread.
        if let Err(error) = handle.block_on(run_js(file_path.as_str())) {
            eprintln!("error: {error}");
        }
    })
    .join()
    .unwrap();
}
pub(crate) fn run_js(args: &RunArgs) {
    let file_path = args.file.to_owned().into_string().unwrap();
    let handle = tokio::runtime::Handle::current();
    std::thread::spawn(move || {
        // Using Handle::block_on to run async code in the new thread.
        if let Err(error) = handle.block_on(run_js(file_path.as_str())) {
            eprintln!("error: {error}");
        }
    })
    .join()
    .unwrap();
}


But the moment I execute the example script, it fails at
setTimeout
setTimeout
.

console.log("Waiting 2 seconds for timeout...");
setTimeout(() => {
  console.log("Hello World from timeout!");
}, 2000);
console.log("Waiting 2 seconds for timeout...");
setTimeout(() => {
  console.log("Hello World from timeout!");
}, 2000);


I end up with output like below:

[out]: "Waiting 2 seconds for timeout..."
Error:   × Main thread panicked.
  ├─▶ at /Users/my_user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.4/src/task.rs:56:3
  ╰─▶ assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
  help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.

fatal runtime error: failed to initiate panic, error 5
[out]: "Waiting 2 seconds for timeout..."
Error:   × Main thread panicked.
  ├─▶ at /Users/my_user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.4/src/task.rs:56:3
  ╰─▶ assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
  help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.

fatal runtime error: failed to initiate panic, error 5
GitHub
roll-your-own-javascript-runtime/src/main.rs at main · denoland/rol...
Contribute to denoland/roll-your-own-javascript-runtime development by creating an account on GitHub.
roll-your-own-javascript-runtime/src/main.rs at main · denoland/rol...
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

already borrowed: BorrowMutError in deno runtime
louis030195Llouis030195 / help
2y ago
thread 'tokio-runtime-worker' has overflowed its stack
begoonBbegoon / help
2y ago
how to use deno.json when using deno runtime in rust?
louis030195Llouis030195 / help
2y ago