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

[bug] plugin_websocket: When WebSocket accepts a large number of concurrent messages, multiple clone windows may cause crashes #2186

Open
muwoo opened this issue Dec 10, 2024 · 4 comments
Labels
bug Something isn't working plugin: websocket

Comments

@muwoo
Copy link

muwoo commented Dec 10, 2024

The code in question is here:
https://github.com/tauri-apps/tauri-plugin-websocket/blob/dev/src/lib.rs#L96

read.for_each(move |message| {
    let window_ = window.clone();
    // ...
})

This problem is inevitable in my local testing: once there are a large number of ws messages, the application will crash. The problem currently located is caused by this.

@FabianLars FabianLars added bug Something isn't working plugin: websocket labels Dec 10, 2024
@FabianLars
Copy link
Member

I assume this is tauri-apps/tauri#8177 actually and not the window.clone() line. Unless of course you have a panic message for us that tells us more?

@muwoo
Copy link
Author

muwoo commented Dec 11, 2024

@FabianLars
thanks for your reply!

I assume this is tauri-apps/tauri#8177 actually and not the window.clone() line. Unless of course you have a panic message for us that tells us more?

I don't think it's caused by this problem.

Because I try to get window in the following way, the crash does not happen.

    let window_arc = Arc::new(Mutex::new(window));

    read.for_each(move |message| {
      let window_ = Arc::clone(&window_arc); 
      // ...
   })

The program crashes directly without panic, so we can only try to catch the exception through Windows' SetUnhandledExceptionFilter. This is the relevant information captured:

Backtrace [
    { fn: "std::backtrace_rs::backtrace::dbghelp::trace", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs", line: 131 },
    { fn: "std::backtrace_rs::backtrace::trace_unsynchronized", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\..\..\backtrace\src\backtrace\mod.rs", line: 66 },
    { fn: "std::backtrace::Backtrace::create", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\backtrace.rs", line: 331 },
    { fn: "std::backtrace::Backtrace::force_capture", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\backtrace.rs", line: 312 },
    { fn: "mcs_platform_latest::utils::setup::handle", file: ".\src\utils\setup.rs", line: 88 },
    { fn: "ImeExtension" },
    { fn: "GetHandleVerifier" },
    { fn: "UnhandledExceptionFilter" },
    { fn: "memset" },
    { fn: "_C_specific_handler" },
    { fn: "_chkstk" },
    { fn: "RtlRaiseException" },
    { fn: "KiUserExceptionDispatcher" },
    { fn: "tauri_runtime_wry::impl$49::run::closure$0<enum2$<tauri::EventLoopMessage>,tauri::app::impl$18::run::closure_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> >,mcs_platform_latest::main::closure_env$4> >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-runtime-wry-0.14.9\src\lib.rs", line: 2300 },
    { fn: "tao::platform_impl::platform::event_loop::impl$2::run_return::closure$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$49::run::closure_env$0<enum2$<tauri::EventLoopMessage>,tauri::app::impl$18::run::closure_", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 234 },
    { fn: "alloc::boxed::impl$48::call_mut<tuple$<enum2$<tao::event::Event<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >,ref_mut$<enum2$<tao::event_loop::ControlFlow> > >,dyn$<core::ops::function::FnMut<tuple$<enum2$<tao::event::Event<enu", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\alloc\src\boxed.rs", line: 2022 },
    { fn: "tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 250 },
    { fn: "core::panic::unwind_safe::impl$23::call_once<tuple$<>,tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\core\src\panic\unwind_safe.rs", line: 272 },
    { fn: "std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >,tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panicking.rs", line: 552 },
    { fn: "tauri::endpoints::dialog::_::impl$0::deserialize::impl$16::expecting" },
    { fn: "std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > > >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panicking.rs", line: 516 },
    { fn: "std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >,tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panic.rs", line: 142 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::catch_unwind<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tuple$<>,tao::platform_impl::plat", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 156 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::call_event_handler<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 242 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::call_new_events<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 396 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::move_state_to<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 331 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::send_event<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 223 },
    { fn: "tao::platform_impl::platform::event_loop::ThreadMsgTargetSubclassInput<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::send_event<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 127 },
    { fn: "tao::platform_impl::platform::event_loop::thread_event_target_callback::closure$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 2226 },
    { fn: "core::ops::function::FnOnce::call_once<tao::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >,tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\core\src\ops\function.rs", line: 250 },
    { fn: "core::panic::unwind_safe::impl$23::call_once<windows::Windows::Win32::Foundation::LRESULT,tao::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\core\src\panic\unwind_safe.rs", line: 272 },
    { fn: "std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >,windows::Windows::Win32", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panicking.rs", line: 552 },
    { fn: "std::panicking::try::do_catch<core::panic::unwind_safe::AssertUnwindSafe<tokio::runtime::task::harness::cancel_task::closure_env$0<enum2$<tauri::hooks::impl$7::respond_async_serialized::async_block_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessa" },
    { fn: "std::panicking::try<windows::Windows::Win32::Foundation::LRESULT,core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMess", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panicking.rs", line: 516 },
    { fn: "std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<tao::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > > >,windows::Windows::Win32::Fo", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\panic.rs", line: 142 },
    { fn: "tao::platform_impl::platform::event_loop::runner::EventLoopRunner<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::catch_unwind<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,windows::Windows::Win32::Foundati", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop\runner.rs", line: 156 },
    { fn: "tao::platform_impl::platform::event_loop::thread_event_target_callback<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 2275 },
    { fn: "DefSubclassProc" },
    { fn: "DSA_Create" },
    { fn: "CallWindowProcW" },
    { fn: "DispatchMessageW" },
    { fn: "windows::Windows::Win32::UI::WindowsAndMessaging::DispatchMessageW", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\windows-0.39.0\src\Windows\Win32\UI\WindowsAndMessaging\mod.rs", line: 2671 },
    { fn: "tao::platform_impl::platform::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run_return<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$49::run::closure_env$0<en", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 264 },
    { fn: "tao::platform_impl::platform::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$49::run::closure_env$0<enum2$<ta", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\platform_impl\windows\event_loop.rs", line: 218 },
    { fn: "tao::event_loop::EventLoop<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > > >::run<enum2$<tauri_runtime_wry::Message<enum2$<tauri::EventLoopMessage> > >,tauri_runtime_wry::impl$49::run::closure_env$0<enum2$<tauri::EventLoopMessage>,ta", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tao-0.16.5\src\event_loop.rs", line: 179 },
    { fn: "tauri_runtime_wry::impl$49::run<enum2$<tauri::EventLoopMessage>,tauri::app::impl$18::run::closure_env$0<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> >,mcs_platform_latest::main::closure_env$4> >", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-runtime-wry-0.14.9\src\lib.rs", line: 2299 },
    { fn: "tauri::app::App<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> > >::run<tauri_runtime_wry::Wry<enum2$<tauri::EventLoopMessage> >,mcs_platform_latest::main::closure_env$4>", file: "C:\Users\admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-1.7.1\src\app.rs", line: 868 },
    { fn: "mcs_platform_latest::main", file: ".\src\main.rs", line: 83 },
    { fn: "core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\core\src\ops\function.rs", line: 250 },
    { fn: "core::hint::black_box", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\core\src\hint.rs", line: 286 },
    { fn: "std::sys_common::backtrace::__rust_begin_short_backtrace<void (*)(),tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\sys_common\backtrace.rs", line: 155 },
    { fn: "std::rt::lang_start::closure$0<tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\rt.rs", line: 166 },
    { fn: "std::rt::lang_start_internal::closure$2", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\rt.rs", line: 148 },
    { fn: "std::panicking::try::do_call", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\panicking.rs", line: 552 },
    { fn: "std::panicking::try", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\panicking.rs", line: 516 },
    { fn: "std::panic::catch_unwind", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\panic.rs", line: 142 },
    { fn: "std::rt::lang_start_internal", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library\std\src\rt.rs", line: 148 },
    { fn: "std::rt::lang_start<tuple$<> >", file: "/rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce\library\std\src\rt.rs", line: 165 },
    { fn: "main" },
    { fn: "invoke_main", file: "D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl", line: 78 },
    { fn: "__scrt_common_main_seh", file: "D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl", line: 288 },
    { fn: "BaseThreadInitThunk" },
    { fn: "RtlUserThreadStart" },
]

@muwoo
Copy link
Author

muwoo commented Dec 11, 2024

@FabianLars
We also encountered a crash problem before, which felt a bit similar to this.

tauri-apps/tauri#11125

The previous crash was because one of our functions was called multiple times concurrently. It is very simple to reproduce this problem. The approximate code is as follows:

function clickFn() {
    appWindow.unminimize();
    appWindow.show();
    appWindow.setFocus();
}

setInterval(() => clickFn(), 10);
setInterval(() => clickFn(), 100);

After running the program, wait for 1-5 minutes and it will crash.

Of course, this is a problem with our code, but it will occasionally trigger a crash, and the crash log is similar. Later, we modified this part of the code so that concurrent calls would not occur and the crash would disappear.

These are some problems we have encountered recently.

I suspect it has nothing to do with emit. It may be caused by a large number of calls to window handles.

@muwoo
Copy link
Author

muwoo commented Dec 11, 2024

Because I have also tested a large number of emit, it will not trigger a crash. It will only occur when a large number of concurrent window operations are called.

Maybe after operating the window, we need to emit the event to the front end. When troubleshooting, it is easy to attribute the cause of the problem to emit.

This is what we have discovered through extensive experiments, and if you need us to provide some other examples, we will be happy to provide them. Thanks alot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin: websocket
Projects
None yet
Development

No branches or pull requests

2 participants