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

Support async commands with Window handle on Windows #2078

Closed
TomzBench opened this issue Jun 25, 2021 · 3 comments
Closed

Support async commands with Window handle on Windows #2078

TomzBench opened this issue Jun 25, 2021 · 3 comments

Comments

@TomzBench
Copy link

TomzBench commented Jun 25, 2021

Is your feature request related to a problem? Please describe.
I have an async [tauri::command] that includes a Window. This command compiles on Linux, but not compile on Windows. On Windows I get an error that Window is not Send and not Sync.

Describe the solution you'd like
Window object on Windows should be Send + Sync as same on Linux.

Additional context

Here is the code that compiles on Linux, but does not compile on Windows

#[tauri::command]
pub async fn update<'r>(
  io: tauri::State<'r, Io>,
  window: tauri::Window,
  serial: String,
  update: Update,
) -> Result<(), String> {
  let mut count = 0;
  let total = update.len();
  io.channel(&serial)
    .map_err(|e| e.to_string())?
    .requests(update)
    .try_for_each(|_| {
      count = count + 1;
      window
        .emit(
          "update",
          Some(UpdateProgress {
            serial: serial.clone(),
            count,
            total,
          }),
        )
        .expect("failed to emit update status");
      future::ready(Ok(()))
    })
    .map_err(|e| e.to_string())
    .await?;
  Ok(())
}

Here are some clips of the compiler errors

13 |   #[tauri::command]
   |   ^^^^^^^^^^^^^^^^^ future returned by `update` is not `Send`

   = help: within `tauri::Window`, the trait `std::marker::Sync` is not implemented for `*mut winapi::shared::windef::HWND__`
note: future is not `Send` as this value is used across an await

   = help: within `tauri::Window`, the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<tauri_runtime_wry::Message>`
note: future is not `Send` as this value is used across an await

[Windows info]

Operating System - Windows, version 10.0.19041 X64
Webview2 - 91.0.864.54

Node.js environment
  Node.js - 14.15.4
  @tauri-apps/cli - 1.0.0-beta.1 (outdated, latest: 1.0.0-beta.3)
  @tauri-apps/api - Not installed

Global packages
  npm - 6.14.10
  yarn - 1.22.10

Rust environment
  rustc - 1.53.0
  cargo - 1.53.0

App directory structure
/src
/src-tauri

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

[Linux info]

Operating System - Fedora, version 32.0.0 X64

Node.js environment
  Node.js - 14.17.0
  @tauri-apps/cli - 1.0.0-beta.1 (outdated, latest: 1.0.0-beta.3)
  @tauri-apps/api - Not installed

Global packages
  npm - 6.14.13
  yarn - 1.22.10

Rust environment
  rustc - 1.53.0
  cargo - 1.53.0

App directory structure
/src
/src-tauri

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

@TomzBench
Copy link
Author

TomzBench commented Jun 25, 2021

I believe c_void does not have Send + Sync auto traits. So you might need to wrap this handle? No idea if that is sound or not but perhaps this is why the Window is not Send on windows

  #[cfg(windows)]
  pub fn hwnd(&self) -> crate::Result<*mut std::ffi::c_void> {
    self.window.dispatcher.hwnd().map_err(Into::into)
  }

@TomzBench
Copy link
Author

As a work around, i am no longer using window.emit and driving the update feature client side using a request/response pattern. I think what is actually needed for this use case is the concept of a stream. Where instead of returning a future resolving a single response, we return a stream of responses. Maybe that would go in a different feature request

@lucasfernog
Copy link
Member

You should move the window to the try_for_each closure (probably needs to clone a window for each iteration).

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

No branches or pull requests

2 participants