tauri-apps/tauri

[bug] use `enigo` in tauri cause app crashed

jackeydou opened this issue · 11 comments

Describe the bug

I try to use enigo to do some keyboard event mock in my tauri app, and it will crash when the macOS api TISCopyCurrentKeyboardInputSource or TISGetInputSourceProperty execute in enigo.

I start an issue in enigo before, but we found the crash will only appear in my Tauri app.

Reproduction

  1. register a shortcut
import { isRegistered, register, unregister } from '@tauri-apps/api/globalShortcut';
export async function registerShortcut(shortcut: string, callback: (shortcut?: string) => void) {
  await register(shortcut, (shortcut) => {
    callback(shortcut);
  });
}

and there is a log in shell:

register Accelerator { id: Some(AcceleratorId(29420)), mods: ALT, key: KeyS }
  1. write a tauri command, and invoke in javascript
#[tauri::command]
async fn paste_content(window: tauri::Window) {
    let mut enigo = Enigo::new();
    let trusted = accessibility::query_accessibility_permissions();
    if trusted {
        enigo.key_down(Key::Meta);
        enigo.key_click(Key::Layout('v'));
        enigo.key_up(Key::Meta);

    }
}
invoke("paste_content").then(async _ => {
        
 })

I use accessibility to help me request the macOS accessibility permission, and it returns true.
3. the application crashed

Expected behavior

The app does not crash.

Platform and versions

Environment
  › OS: Mac OS 13.0.0 X64
  › Node.js: 16.14.0
  › npm: 8.3.1
  › pnpm: 7.9.5
  › yarn: 3.1.1
  › rustup: 1.25.2
  › rustc: 1.68.0
  › cargo: 1.68.0
  › Rust toolchain: stable-aarch64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.2.3
  › @tauri-apps/api [NPM]: 1.2.0
  › tauri [RUST]: 1.2.4,
  › tauri-build [RUST]: 1.2.1,
  › tao [RUST]: 0.15.8,
  › wry [RUST]: 0.23.4,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:1420/
  › framework: React
  › bundler: Vite

App directory structure
  ├─ dist
  ├─ node_modules
  ├─ public
  ├─ .github
  ├─ src-tauri
  ├─ api
  ├─ .git
  ├─ .vscode
  ├─ assets
  └─ src

Stack trace

There is no output in my shell when it crashed, I try to debug it and set some breakpoints, I put the screenshots [here](https://github.com/enigo-rs/enigo/pull/161#issuecomment-1460236620)

Additional context

No response

I have encountered the same problem and it is very troubling.

I have encountered the same problem and it is very troubling.

Did you find the cause of the crash and how did you fix it?

Have the same problem, though the app doesn't crash in my case

@zeenix @amrbashir could you please take a look? 🙏

@garrrikkotua why me? I don't even know what enigo is.

@garrrikkotua why me? I don't even know what enigo is.

Apologies, it was a suggestion from GitHub 😅

I don't have access to a mac unfortunately so I can't triage this issue.

This is going to sound a bit strange, but try disabling spellcheck in the HTML.

<html spellcheck="false">
    <!-- ... -->
</html>

About a year ago this was causing WebKit to crash in our MacOS variant.

A user tried it out and the problem persisted

uwejan commented

@pentamassiv Is there an update on this.
I notice app exists without any error code.
Disabeling spellchecker does not help.

No, sorry I have not looked into it. I am not familiar at all with tauri