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

[Windows] Panic when opening dialog while using ControlFlow::WaitUntil #2291

Closed
Diagamma opened this issue May 16, 2022 · 6 comments
Closed
Labels
B - bug Dang, that shouldn't have happened C - needs investigation Issue must be confirmed and researched DS - windows

Comments

@Diagamma
Copy link

I believe this is related to issues #1587, #1602, and #1611, that were all fixed by #1615.
This is essentially the same issue, except it only occurs while the event loop's control flow is set to WaitUntil. There is no panic when using Wait or Poll.
I currently work around this issue by creating message boxes from a new thread.

Here is a minimal example reproducing the bug: https://gist.github.com/53648c963f91552e7039a266e0359004
The code will spawn a message box after around a second, then winit should panic in the following second. The panic may not always occur, so reproducing the issue might require running the example multiple times. Closing the message box fast enough will also prevent the panic from showing up.

As an additional note, I have only reproduced the issue when creating the message box while handling MainEventsCleared. The panic does not show up when creating the message box while handling a KeyboardInput event. I have not tried creating message boxes while handling other events.

Here is a stacktrace of the panic:

thread 'main' panicked at 'either event handler is re-entrant (likely), or no event handler is registered (very unlikely)', C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:242:18
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\/library\std\src\panicking.rs:584
   1: core::panicking::panic_fmt
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\/library\core\src\panicking.rs:143
   2: core::panicking::panic_display<str>
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\/library\core\src\panicking.rs:73
   3: core::panicking::panic_str
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\/library\core\src\panicking.rs:56
   4: core::option::expect_failed
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\/library\core\src\option.rs:1852
   5: enum$<core::option::Option<alloc::boxed::Box<dyn$<core::ops::function::FnMut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> > >,assoc$<Output,tuple$<> > > >,alloc::alloc::Global> >, 1, 18446744073709551615, So
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\option.rs:715
   6: winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:241
   7: core::panic::unwind_safe::impl$23::call_once<tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\panic\unwind_safe.rs:271
   8: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:492
   9: std::sys::windows::os_str::Slice::from_str
  10: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:456
  11: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panic.rs:137
  12: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152
  13: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::call_event_handler<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:239
  14: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::call_redraw_events_cleared<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:404
  15: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::move_state_to<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:359
  16: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::poll<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:201
  17: winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2265
  18: core::ops::function::FnOnce::call_once<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\ops\function.rs:227
  19: core::panic::unwind_safe::impl$23::call_once<isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\panic\unwind_safe.rs:271
  20: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >,isize>
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:492
  21: std::sys::windows::os_str::Slice::from_str
  22: std::panicking::try<isize,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:456
  23: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >,isize>
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panic.rs:137
  24: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152
  25: winit::platform_impl::platform::event_loop::thread_event_target_callback<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2271
  26: DispatchMessageW
  27: NotifyWinEvent
  28: BringWindowToTop
  29: LoadBitmapA
  30: SoftModalMessageBox
  31: MessageBoxIndirectW
  32: MessageBoxTimeoutW
  33: MessageBoxW
  34: native_dialog::dialog_impl::win::message::message_box::closure$0
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\native-dialog-0.6.3\src\dialog_impl\win\message.rs:74
  35: native_dialog::dialog_impl::win::with_visual_styles
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\native-dialog-0.6.3\src\dialog_impl\win\mod.rs:19
  36: native_dialog::dialog_impl::win::message::message_box
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\native-dialog-0.6.3\src\dialog_impl\win\message.rs:73
  37: native_dialog::dialog_impl::win::message::impl$0::show
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\native-dialog-0.6.3\src\dialog_impl\win\message.rs:9
  38: native_dialog::message::MessageDialog::show_alert
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\native-dialog-0.6.3\src\message.rs:79
  39: test::main::closure$0
             at .\src\main.rs:23
  40: winit::platform_impl::platform::event_loop::impl$2::run_return::closure$0<tuple$<>,test::main::closure_env$0>
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:206
  41: alloc::boxed::impl$45::call_mut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> > >,dyn$<core::ops::function::FnMut<tuple$<enum$<winit::event::Event<tuple$<> > >,ref_mut$<enum$<winit::event_loop::ControlFlow> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\alloc\src\boxed.rs:1860
  42: winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure$0<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:245
  43: core::panic::unwind_safe::impl$23::call_once<tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\panic\unwind_safe.rs:271
  44: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:492
  45: std::sys::windows::os_str::Slice::from_str
  46: std::panicking::try<tuple$<>,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:456
  47: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panic.rs:137
  48: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,tuple$<>,winit::platform_impl::platform::event_loop::runner::impl$3::call_event_handler::closure_env$0<tuple$<> > >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152
  49: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::call_event_handler<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:239
  50: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::move_state_to<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:341
  51: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::main_events_cleared<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:227
  52: winit::platform_impl::platform::event_loop::flush_paint_messages<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:726
  53: winit::platform_impl::platform::event_loop::thread_event_target_callback::closure$0<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2091
  54: core::ops::function::FnOnce::call_once<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> >,tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\ops\function.rs:227
  55: core::panic::unwind_safe::impl$23::call_once<isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\panic\unwind_safe.rs:271
  56: std::panicking::try::do_call<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >,isize>
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:492
  57: std::sys::windows::os_str::Slice::from_str
  58: std::panicking::try<isize,core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > > >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panicking.rs:456
  59: std::panic::catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >,isize>
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\std\src\panic.rs:137
  60: winit::platform_impl::platform::event_loop::runner::EventLoopRunner<tuple$<> >::catch_unwind<tuple$<>,isize,winit::platform_impl::platform::event_loop::thread_event_target_callback::closure_env$0<tuple$<> > >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop\runner.rs:152
  61: winit::platform_impl::platform::event_loop::thread_event_target_callback<tuple$<> >
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:2271
  62: CallWindowProcW
  63: MsgWaitForMultipleObjects
  64: MsgWaitForMultipleObjects
  65: KiUserCallbackDispatcher
  66: SendMessageW
  67: SendMessageW
  68: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run_return<tuple$<>,test::main::closure_env$0>
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:221
  69: winit::platform_impl::platform::event_loop::EventLoop<tuple$<> >::run<tuple$<>,test::main::closure_env$0>
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\platform_impl\windows\event_loop.rs:191
  70: winit::event_loop::EventLoop<tuple$<> >::run<tuple$<>,test::main::closure_env$0>
             at C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.26.1\src\event_loop.rs:154
  71: test::main
             at .\src\main.rs:18
  72: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\test.exe` (exit code: 101)
@maroider maroider added B - bug Dang, that shouldn't have happened DS - windows C - needs investigation Issue must be confirmed and researched labels May 16, 2022
@Diagamma
Copy link
Author

Update: I've managed to fix this issue by applying the following patch to winit.
I'm not familiar enough with the Windows API to feel comfortable submitting a PR for this change, as I do not know if I might've introduced another bug by accident, but I've been using it with no visible issues for a couple days, so I figured I'd at least share it.

--- a/src/platform_impl/windows/event_loop.rs
+++ b/src/platform_impl/windows/event_loop.rs
@@ -2262,7 +2262,9 @@ unsafe extern "system" fn thread_event_target_callback<T: 'static>(
                     }
                 }
             }
-            userdata.event_loop_runner.poll();
+            if !userdata.event_loop_runner.should_buffer() {
+                userdata.event_loop_runner.poll();
+            }
             0
         }
         _ => winuser::DefWindowProcW(window, msg, wparam, lparam),

@dtzxporter
Copy link
Contributor

Would really like to see some effort put into this one, this is causing random issues on some windows desktops when using iced. I applied the patch, which seems to fix it.

@kchibisov
Copy link
Member

@dtzxporter is this issue still a thing on the latest master or betas? We changed a lot around the loop control.

@dtzxporter
Copy link
Contributor

I can try to test this, my issue is it's so random that it happens like 1 in every 1000 runs on my machine, but a user of my program has it happen consistently...

iced-rs@c52db20

This is the specific commit that iced is using at the moment (With iced patches) It's a bit behind, but I don't know when the changes happened.

I can attempt to update iced's deps, but it's going to be a pain to coordinate with the user regardless to see if it's fixed.

@kchibisov
Copy link
Member

yeah, their winit is very behind. I'd suggest to wait for iced to update and then we could probably iterate on that, or maybe someone familiar with windows could look into.

@danielhjacobs
Copy link

danielhjacobs commented Aug 9, 2024

Is there a reason this issue was left open? It is stated to be fixed in iced-rs#14 and it seems fixed when a workaround for this issue is removed from Ruffle:

https://github.com/ruffle-rs/ruffle/blob/db13a4e9e6de50809d63927b479676ea61841a25/desktop/src/util.rs#L260-L278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B - bug Dang, that shouldn't have happened C - needs investigation Issue must be confirmed and researched DS - windows
Development

No branches or pull requests

5 participants