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

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

Open
idootop opened this issue Mar 13, 2024 · 13 comments · May be fixed by #1214
Open

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

idootop opened this issue Mar 13, 2024 · 13 comments · May be fixed by #1214

Comments

@idootop
Copy link

idootop commented Mar 13, 2024

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

  • Register a register_asynchronous_uri_scheme_protocol.
  • From the web side, initiate a request using this protocol and abort the request before the response.
  • The APP crashes 100% of the time.

Expected behavior

  • Before responding, the responder should check if the request has been canceled. If the request has been canceled, do not send the response.
  • Or, during the response, catch exceptions to prevent the APP from crashing and quitting abruptly when the request is terminated.

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:

let () = msg_send![task, didReceiveResponse: response];

图片

Related issue: tauri-apps/tauri#9177

@morajabi
Copy link
Contributor

morajabi commented Apr 5, 2024

Seems like this is the cause of #1142?

@morajabi
Copy link
Contributor

morajabi commented Apr 8, 2024

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

@FabianLars
Copy link
Member

#1214

@darkskygit

This comment was marked as off-topic.

@FabianLars

This comment was marked as off-topic.

@darkskygit

This comment was marked as off-topic.

@FabianLars

This comment was marked as off-topic.

@darkskygit

This comment was marked as off-topic.

@FabianLars

This comment was marked as off-topic.

@darkskygit
Copy link

darkskygit commented Sep 4, 2024

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
Copy link
Member

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
Copy link

darkskygit commented Sep 4, 2024

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:

  • if register a handler to native protocol (e.g. http, https), this will make webview crash in macos
  • if want to integrate a front-end project that includes rust api server, you can follow these guide:
    1. register a custom protocol
       pub fn run() {
           tauri::Builder::default()
               .plugin(tauri_plugin_shell::init())
               .register_asynchronous_uri_scheme_protocol("custom_schema", |app, req, resp| {
                           let path = req.uri().path();
                           // response static files or api response here
                           resp.respond(
                               Response::builder()
                                   .status(StatusCode::BAD_REQUEST)
                                   .header(CONTENT_TYPE, TEXT_PLAIN.essence_str())
                                   .body("failed to generate response".as_bytes().to_vec())
                                   .unwrap(),
                           );
               })
               .invoke_handler(tauri::generate_handler![])
               .run(tauri::generate_context!())
               .expect("error while running tauri application");
       }
    2. change default url in tauri.conf.json:
      {
          "build": {
              "frontendDist": "custom_protocol://localhost"
          }
      }

@thewh1teagle
Copy link
Contributor

thewh1teagle commented Dec 9, 2024

Is there any update? I experience the same crash on macos with custom protocol. Once the response returned, it crash.
For me it crash if I read file inside the custom protocol callback. (only read without unwrap it no matter where in the callback)

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