Skip to content

[bug] Can't run Tauri on a different thread #3172

@marc2332

Description

@marc2332

Describe the bug

I can't run Tauri on a different thread, neither there is any flag/feature to do it.

Reproduction

  1. Run this https://github.com/tauri-apps/tauri/blob/next/examples/helloworld/src-tauri/src/main.rs
  2. But instead of running it in the main thread, run it like something like:
fn main() {
    std::thread::spawn(|| {
        tauri::Builder::default()
        .run(tauri::generate_context!(
          "../../examples/helloworld/src-tauri/tauri.conf.json"
        ))
        .expect("error while running tauri application");
    });
    
    std::thread::park();
}

Expected behavior

Be able to run it in a different thread, rather than the main one.

Platform and versions

Operating System - Windows, version 10.0.19043 X64
Webview2 - 96.0.1054.62

Node.js environment
  Node.js - 17.1.0
  @tauri-apps/cli - 1.0.0-beta.10

Global packages
  npm - 7.5.4
  yarn - 1.22.17

Rust environment
  rustc - 1.59.0-nightly
  cargo - 1.58.0-nightly

Stack trace

thread '<unnamed>' panicked at 'Initializing the event loop outside of the main thread is a significant cross-platform compatibility hazard. If you really, absolutely need to create an EventLoop on a different thread, please use the `EventLoopExtWindows::new_any_thread` function.', C:\Users\mespi\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.5.2\src\platform_impl\windows\event_loop.rs:152:5
stack backtrace:
   0: std::panicking::begin_panic<str>
             at /rustc/6db0a0e9a4a2f55b1a85954e114ada0b45c32e45\library\std\src\panicking.rs:525
   1: tao::platform_impl::platform::event_loop::EventLoop<enum$<tauri_runtime_wry::Message> >::new<enum$<tauri_runtime_wry::Message> >
             at C:\Users\mespi\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.5.2\src\platform_impl\windows\event_loop.rs:152
   2: tao::event_loop::EventLoop<enum$<tauri_runtime_wry::Message> >::with_user_event<enum$<tauri_runtime_wry::Message> >
             at C:\Users\mespi\.cargo\registry\src\github.com-1ecc6299db9ec823\tao-0.5.2\src\event_loop.rs:131
   3: tauri_runtime_wry::impl$36::new
             at .\tauri\core\tauri-runtime-wry\src\lib.rs:1554
   4: tauri::app::Builder<tauri_runtime_wry::Wry>::build<tauri_runtime_wry::Wry,tauri_utils::assets::EmbeddedAssets>
             at .\tauri\core\tauri\src\app.rs:895
   5: tauri::app::Builder<tauri_runtime_wry::Wry>::run<tauri_runtime_wry::Wry,tauri_utils::assets::EmbeddedAssets>
             at .\tauri\core\tauri\src\app.rs:1040
   6: degui::impl$0::run::closure$1
             at .\src\lib.rs:74

Additional context

I am working on my own tauri bindings for Deno, and bacause I don't want to block Deno's main thread, I need to run it in another thread.

Just to test if running it in another thread would give any issue, I manually edited https://github.com/tauri-apps/tao/blob/f8866a084afe18983c91c67401d8759697578a9c/src/event_loop.rs#L132

To:

pub fn with_user_event() -> EventLoop<T> {
    EventLoop {
      event_loop: platform_impl::EventLoop::new_any_thread(), // <- edited "new()" to "new_any_thread()"
      _marker: ::std::marker::PhantomData,
    }
}

So, instead of calling EventLoop::new() which will trigger the panic, it lets me run tauri on a different thread. I thought that maybe there could be some kind of flag / feature, so with_user_event() doesn't call EventLoop::new() but instead, it calls EventLoop::new_any_thread() .

I can open this issue in tao's repository if you prefer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: upstreamThis issue is blocked by upstream dependencies and we need to wait or contribute upstream fixestype: bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions