tauri-apps / wry

Cross-platform WebView library in Rust for Tauri.
Apache License 2.0
3.51k stars 262 forks source link

[macOS] APP crash when a web custom protocol request is aborted before or during response #1189

Open idootop opened 6 months ago

idootop commented 6 months ago

Describe the bug

When initiating a register_uri_scheme_protocol request from the web side, if the web aborts the request before or during the response, the macOS APP crashes.

Reproduction

Expected behavior

Full tauri info output

[✔] Environment
    - OS: Mac OS 13.6.3 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.76.0 (07dca489a 2024-02-04)
    ✔ cargo: 1.76.0 (c84b36747 2024-01-18)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-apple-darwin (default)
    - node: 18.17.1
    - pnpm: 8.15.4
    - npm: 9.6.7

[-] Packages
    - tauri [RUST]: 2.0.0-beta.10
    - tauri-build [RUST]: 2.0.0-beta.8
    - wry [RUST]: 0.37.0
    - tao [RUST]: 0.26.1
    - @tauri-apps/api [NPM]: 2.0.0-beta.5
    - @tauri-apps/cli [NPM]: 2.0.0-beta.9

[-] App
    - build-type: bundle
    - CSP: default-src x-http: x-https: blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'
    - frontendDist: ../feiyu/dist
    - devUrl: http://localhost:3000/

Stack trace

thread 'tokio-runtime-worker' panicked at /Users/mac/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.37.0/src/wkwebview/mod.rs:262:28:
Uncaught exception <NSException: 0x10428cf30>

Additional context

Related code: https://github.com/tauri-apps/wry/blob/5789bf759ce94e4dad5ff26a08fe81521658a4e4/src/wkwebview/mod.rs#L272

图片

Related issue: https://github.com/tauri-apps/tauri/issues/9177

morajabi commented 5 months ago

Seems like this is the cause of #1142?

morajabi commented 5 months ago

Do you have an idea on how to fix this issue?

FabianLars commented 5 months ago

https://github.com/tauri-apps/wry/pull/1214

darkskygit commented 1 week ago

any update? this bug prevents me from developing on a Mac and requires me to develop in a Windows virtual machine

FabianLars commented 1 week ago

@darkskygit Would you happen to have a minimal reproduction example (or any kind of reproduction)?

darkskygit commented 1 week ago

@darkskygit Would you happen to have a minimal reproduction example (or any kind of reproduction)?

https://github.com/darkskygit/tauri-custom-uri-schema-crash

the latest commit will make tauri crash in mac

FabianLars commented 1 week ago

Does this only happen with http / https and other known/native schemes? WKWebView does not allow registering schemes that it already handles by default so it makes sense if it crashes then.

darkskygit commented 1 week ago

Does this only happen with http / https and other known/native schemes? WKWebView does not allow registering schemes that it already handles by default so it makes sense if it crashes then.

i tried to register a custom schema, and it indeed no longer causes a crash Is there any other way to forward requests under certain paths (e.g., /api/*) to Rust? Using custom paths or invoke requires a lot of changes to the front-end code, and registering http:// works well on windows

FabianLars commented 1 week ago

Not that i know of. Windows also only works by accident here. Once we drop win7 support tauri will use a different api for the custom protocol which would break your implementation.

After that you could use the request intercept api we currently use on windows for the custom protocol of course (example) but there's no equivalent on linux/macos as long as we're stuck with the current webviews.

darkskygit commented 1 week ago

Not that i know of. Windows also only works by accident here. Once we drop win7 support tauri will use a different api for the custom protocol which would break your implementation.

After that you could use the request intercept api we currently use on windows for the custom protocol of course (example) but there's no equivalent on linux/macos as long as we're stuck with the current webviews.

if it is not possible to register a custom protocol for http under linux/macos, how does tauri respond to static files? can I use the same method to register a custom handler? my current implementation can also return frontend static assets, so it is acceptable for my custom handler to respond to all requests but not only /api/*

FabianLars commented 1 week ago

Tauri serves the frontend via the tauri:// scheme via the custom protocol webview apis (the same ones that do not allow overwriting existing schemes). Requests to those schemes can be handled just fine.

my current implementation can also return frontend static assets, so it is acceptable for my custom handler to respond to all requests but not only /api/*

Then you'd have to change the url of the window to your custom scheme.

darkskygit commented 1 week ago

Tauri serves the frontend via the tauri:// scheme via the custom protocol webview apis (the same ones that do not allow overwriting existing schemes). Requests to those schemes can be handled just fine.

my current implementation can also return frontend static assets, so it is acceptable for my custom handler to respond to all requests but not only /api/*

Then you'd have to change the url of the window to your custom scheme.

Thank you for your prompt response. To help others quickly understand this problem, I write a summary of the above discussion: