Repository: tauri
Issue: [bug] Crash on MacOS when navigating with a pending invoke
Labels: type: bug,priority: 1 high,platform: macOS,status: needs triage

### Describe the bug

On MacOS there appears to be a race condition when an asynchronous invoke request is in progress while the webview is navigating to a new page. This leads to a panic in `wry::wkwebview::class::url_scheme_handler::start_task::{{closure}}::response::{{closure}}` which causes an immediate abort because it is in an `AssertUnwindSafe` context.

### Reproduction

Reproduction is available here: https://github.com/tactile-eng/tauri-repro

Simply run the app and it should crash after a few seconds. The only code that has changed from the template is in `src/main.js`.

### Expected behavior

Tauri should not crash.

### Full `tauri info` output

```text
> tauri "info"


[✔] Environment
    - OS: Mac OS 15.2.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.83.0 (90b35a623 2024-11-26)
    ✔ cargo: 1.83.0 (5ffbef321 2024-10-29)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.20.5
    - pnpm: 9.15.2
    - npm: 10.8.2

[-] Packages
    - tauri 🦀: 2.2.1
    - tauri-build 🦀: 2.0.5
    - wry 🦀: 0.48.0
    - tao 🦀: 0.31.1
    - tauri-cli 🦀: 1.5.3
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli : 2.2.3

[-] Plugins
    - tauri-plugin-opener 🦀: 2.2.3
    - @tauri-apps/plugin-opener : not installed!

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src
```


### Stack trace

```text
2025-01-10 08:50:56.119 tauri-repro[4081:3055403] +[IMKClient subclass]: chose IMKClient_Modern
2025-01-10 08:50:56.213 tauri-repro[4081:3055403] +[IMKInputSession subclass]: chose IMKInputSession_Modern
thread 'tokio-runtime-worker' panicked at core/src/panicking.rs:221:5:
panic in a function that cannot unwind
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:112:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:122:5
   3: core::panicking::panic_nounwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:221:5
   4: core::panicking::panic_cannot_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:310:5
   5: objc2::exception::try_no_ret::try_objc_execute_closure
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:196:9
   6: rust_objc_sys_0_3_try_catch_exception
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc-sys-0.3.5/extern/exception.m:14:9
   7: objc2::exception::try_no_ret
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:232:28
   8: objc2::exception::catch
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:291:27
   9: objc2::runtime::message_receiver::MessageReceiver::send_message
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/runtime/message_receiver.rs:25:15
  10: objc2::__macro_helpers::msg_send::MsgSend::send_message
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/__macro_helpers/msg_send.rs:27:31
  11: objc2_web_kit::generated::__WKURLSchemeTask::WKURLSchemeTask::didFinish
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/macros/extern_protocol.rs:239:14
  12: wry::wkwebview::class::url_scheme_handler::start_task::{{closure}}::response::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.48.0/src/wkwebview/class/url_scheme_handler.rs:260:19
  13: core::ops::function::FnOnce::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  14: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  15: objc2::exception::catch::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:289:27
  16: objc2::exception::try_no_ret::try_objc_execute_closure
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:202:13
  17: rust_objc_sys_0_3_try_catch_exception
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc-sys-0.3.5/extern/exception.m:14:9
  18: objc2::exception::try_no_ret
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:232:28
  19: objc2::exception::catch
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/exception.rs:291:27
  20: wry::wkwebview::class::url_scheme_handler::start_task::{{closure}}::response
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.48.0/src/wkwebview/class/url_scheme_handler.rs:259:17
  21: wry::wkwebview::class::url_scheme_handler::start_task::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.48.0/src/wkwebview/class/url_scheme_handler.rs:275:23
  22: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  23: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:2454:9
  24: wry::RequestAsyncResponder::respond
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.48.0/src/lib.rs:345:5
  25: tauri_runtime_wry::create_webview::{{closure}}::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-runtime-wry-2.3.0/src/lib.rs:4402:36
  26: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  27: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:2454:9
  28: tauri::app::UriSchemeResponder::respond
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/app.rs:2116:5
  29: tauri::ipc::protocol::get::{{closure}}::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/ipc/protocol.rs:58:7
  30: tauri::ipc::protocol::get::{{closure}}::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/ipc/protocol.rs:141:19
  31: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  32: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:2454:9
  33: tauri::webview::Webview<R>::on_message::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/webview/mod.rs:1342:11
  34: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  35: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:2454:9
  36: tauri::ipc::InvokeResolver<R>::return_result
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/ipc/mod.rs:452:5
  37: tauri::ipc::InvokeResolver<R>::respond_async_serialized::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-2.2.1/src/ipc/mod.rs:347:7
  38: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/future/future.rs:123:9
  39: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:331:17
  40: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/loom/std/unsafe_cell.rs:16:9
  41: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:320:13
  42: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:532:19
  43: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  44: std::panicking::try::do_call
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:557:40
  45: ___rust_try
  46: std::panicking::try
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:520:19
  47: std::panic::catch_unwind
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:358:14
  48: tokio::runtime::task::harness::poll_future
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:520:18
  49: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:209:27
  50: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:154:15
  51: tokio::runtime::task::raw::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:271:5
  52: tokio::runtime::task::raw::RawTask::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:201:18
  53: tokio::runtime::task::LocalNotified<S>::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/mod.rs:449:9
  54: tokio::runtime::scheduler::multi_thread::worker::Context::run_task::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:596:13
  55: tokio::runtime::coop::with_budget
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/coop.rs:107:5
  56: tokio::runtime::coop::budget
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/coop.rs:73:5
  57: tokio::runtime::scheduler::multi_thread::worker::Context::run_task
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:595:9
  58: tokio::runtime::scheduler::multi_thread::worker::Context::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:546:24
  59: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:511:21
  60: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context/scoped.rs:40:9
  61: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context.rs:180:26
  62: std::thread::local::LocalKey<T>::try_with
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:283:12
  63: std::thread::local::LocalKey<T>::with
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:260:9
  64: tokio::runtime::context::set_scheduler
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context.rs:180:9
  65: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:506:9
  66: tokio::runtime::context::runtime::enter_runtime
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/context/runtime.rs:65:16
  67: tokio::runtime::scheduler::multi_thread::worker::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:498:5
  68: tokio::runtime::scheduler::multi_thread::worker::Launch::launch::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/scheduler/multi_thread/worker.rs:464:45
  69: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/task.rs:42:21
  70: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:331:17
  71: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/loom/std/unsafe_cell.rs:16:9
  72: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/core.rs:320:13
  73: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:532:19
  74: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  75: std::panicking::try::do_call
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:557:40
  76: ___rust_try
  77: std::panicking::try
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:520:19
  78: std::panic::catch_unwind
             at /Users/alexmoon/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:358:14
  79: tokio::runtime::task::harness::poll_future
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:520:18
  80: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:209:27
  81: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/harness.rs:154:15
  82: tokio::runtime::task::raw::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:271:5
  83: tokio::runtime::task::raw::RawTask::poll
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/raw.rs:201:18
  84: tokio::runtime::task::UnownedTask<S>::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/task/mod.rs:486:9
  85: tokio::runtime::blocking::pool::Task::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:161:9
  86: tokio::runtime::blocking::pool::Inner::run
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:511:17
  87: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /Users/alexmoon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.43.0/src/runtime/blocking/pool.rs:469:13
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
```


### Additional context

_No response_