Skip to content

Requirement of async main function #22

Open
@Raphiiko

Description

@Raphiiko

Hi there, I've been working on migrating my application to Tauri v2, and have been running into some difficulty specifically with migrating this plugin.

It seems that in c4b5ca3, the usage of tauri::async_runtime was swapped out, from which point on this plugin requires the application's main function to be made async and annotated with #[tokio::main].

As the application I am migrating blocks on async code in its setup function, this seems to cause some issues.

The recommended way to run async code in Tauri's setup function, seems to be as follows:

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_aptabase::Builder::new("<YOUR_APP_KEY>").build())
        .setup(|app| {
            tauri::async_runtime::block_on(async {
                // Run your async setup code
            });
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

However, as this example stands, the aptabase plugin will not initialize:

thread 'main' panicked at src\main.rs:44:31:
there is no reactor running, must be called from the context of a Tokio 1.x runtime

This is because the main function now has to be made async:

#[tokio::main]
async fn run() {
  ...

This is where my problem starts: Because the main function is now async, I can no longer seem to block on the async code in the setup handler:

Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.       

Honestly, I'm quite stumped on how to solve this problem. My application relies on this async code as part of its setup, and as it stands I am stuck on an earlier commit and cannot migrate to the latest version of this plugin. I'm sure this change was made for a reason, but as no other first party Tauri plugin seems to require an async main function I cannot quite put my finger on why.

Any advice on how I could tackle this? Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions