tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.76k stars 2.45k forks source link

[bug] Deadlock when sending events with tracing enabled (v2 beta) #9453

Open dceddia opened 5 months ago

dceddia commented 5 months ago

Describe the bug

[ moving this here from Discord ]

I'm getting a deadlock when I try to resize the window (or re-focus it, or any other action that sends a window event from the main thread to itself).

See the video for some more details, but this is the gist:

It seems like the deadlock is caused when one of my (not main) threads runs a window.emit('whatever', ...), which acquires a lock on the webview and holds it until the script has been run (I think - not 100% sure on what it waits for). Then when I resize the window, that tries to acquire a lock on the webview too, which is already locked by the emitted event. Then it just hangs forever because it can't get the lock until the main thread runs the emitted JS thing, which will never happen because it's paused waiting for the lock πŸ™ƒ That's my theory anyway.

I noticed it seemed to start happening when I added the tauri-devtools plugin. With Fabian's suggestion in Discord, I was able to create a minimal reproduction with just the tauri tracing feature enabled. I can't get it to happen when tracing is not enabled.

Fabian thought this might be related: https://github.com/tauri-apps/tauri/pull/9429

https://github.com/tauri-apps/tauri/assets/505704/4f4f5b9d-ad1d-4dce-a030-93c341e90798

Reproduction

https://github.com/dceddia/tauri-tracing-freeze

Expected behavior

Ideally no deadlock πŸ˜„

Full tauri info output

This is from the minimal repro:

Projects/tauri-tracing-freeze % pnpm tauri info

> tauri-tracing-freeze@0.0.0 tauri /Users/dceddia/Projects/tauri-tracing-freeze
> tauri "info"

[βœ”] Environment
    - OS: Mac OS 14.2.1 X64
    βœ” Xcode Command Line Tools: installed
    βœ” rustc: 1.77.1 (7cf61ebde 2024-03-27)
    βœ” cargo: 1.77.1 (e52e36006 2024-03-26)
    βœ” rustup: 1.27.0 (bbb9276d2 2024-03-08)
    βœ” Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 20.8.0
    - pnpm: 8.11.0
    - yarn: 1.22.21
    - npm: 10.1.0

[-] Packages
    - tauri [RUST]: 2.0.0-beta.14
    - tauri-build [RUST]: 2.0.0-beta.11
    - wry [RUST]: 0.39.0
    - tao [RUST]: 0.27.0
    - tauri-cli [RUST]: 1.4.0
    - @tauri-apps/api [NPM]: 2.0.0-beta.7
    - @tauri-apps/cli [NPM]: 2.0.0-beta.12

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

There's no crash, but check out the video for some relevant stack traces while execution was paused.

Additional context

No response

Ovenoboyo commented 1 month ago

Minimal reproduction: https://github.com/Ovenoboyo/tauri-tracing-deadlock

^ The code spawns a thread which loops and keeps emitting events to the webview The webview on receiving the event calls an invoke command

With the tracing feature active, the main thread is stuck on acquiring the lock since the thread with tracing is still waiting for a message from the webview.