Skip to content
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

Dragging selected text panics when Tauri is run from #[tokio::main] #2838

Closed
DanielHZhang opened this issue Nov 3, 2021 · 3 comments
Closed

Comments

@DanielHZhang
Copy link

Describe the bug

Selecting text within the Tauri window and dragging the selection in any direction immediately results in a crash when Tauri is initialized from #[tokio::main]. This crash does not occur when running in a non-async fn main.

To Reproduce

Steps to reproduce the behavior:

  1. yarn create tauri-app
  2. Add tokio = { version = "1.13.0", features = ["full"] } to dependencies in Cargo.toml
  3. In src-tauri/src/main.rs, replace main function with:
#[tokio::main]
async fn main() {
  tauri::Builder::default()
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
  1. Start Tauri, select text and try dragging

Expected behavior

Drag and drop functions normally; the window does not crash.

Platform and Versions (required):

Operating System - Windows, version 10.0.19043 X64
Webview2 - 95.0.1020.40

Node.js environment
  Node.js - 16.13.0
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - 1.0.0-beta.8

Global packages
  npm - 8.1.2
  yarn - 1.22.15

Rust environment
  rustc - 1.56.1
  cargo - 1.56.0

App directory structure
/dist
/node_modules
/src
/src-tauri

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../dist
  devPath - http://localhost:8181

Tested on both Windows and Manjaro Linux; results in a crash regardless of OS.

Additional context

#[tokio::main] is added so that some asynchronous tasks can be performed in the main function prior to initializing Tauri (e.g. reading/writing to config files, which stores results in Tauri state).

Stack Trace

Running `target\debug\app.exe`
thread 'main' panicked at 'Cannot start a runtime from within a runtimk the current thread while the thread is being used to drive asynchronous tasks.', C:\Users\Daniel\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.13.0\src\runtime\enter.rs:39:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: Rust panics must be rethrown
error: process didn't exit successfully: `target\debug\app.exe` (exit 
code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)
@probablykasper
Copy link
Member

Workaround for now to run async code:

fn main() {
  let data_load_result = match tokio::runtime::Runtime::new() {
    Ok(runtime) => runtime.block_on(async {
      // load data in separate async thread
      // workaround for https://github.com/tauri-apps/tauri/issues/2838
      return my_async_function(&app_paths).await;
    }),
    Err(e) => panic!("error creating runtime"),
  };
  tauri::Builder::default()
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

probablykasper added a commit to probablykasper/kadium that referenced this issue Dec 10, 2021
@orhun
Copy link
Contributor

orhun commented Feb 5, 2022

Is this fixed? What's the recommended way of running async code in main?

@FabianLars
Copy link
Member

@orhun You can use tokio::main on the next branch now. You just need to give tauri a handle to that (something like this: tauri::async_runtime::set(tokio::runtime::Handle::current());) and you should be good to go.

Keep in mind that the next branch is really active right now as we prepare our first 1.0 release candidate. The changes compares to beta.8 are quite big, so it's not that easy to use right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants