Skip to content

[bug] Creating a new window through a menu event handler results in app freeze #3110

@mihirsamdarshi

Description

@mihirsamdarshi

Describe the bug

I have an event handler attached to on_menu_event that is supposed to create a new window by pulling the window attached to the event (let window = event.window.clone()) and then calls window.create_window(...).

However, this freezes on macOS, resulting in the spinning wheel of death.

Reproduction

In order to reproduce this issue, I created a new project with yarn create tauri-app, and choose a vanilla JS application.

I changed my Tauri dep in Cargo.toml to be { git = "https://github.com/tauri-apps/tauri", features = ["api-all"] }, and ran cargo update

I then added the following code to my main.rs:

#![cfg_attr(
  all(not(debug_assertions), target_os = "windows"),
  windows_subsystem = "windows"
)]

use tauri::{Menu, WindowMenuEvent, Wry, Submenu, CustomMenuItem, WindowUrl, WindowBuilder};
use url::Url;


/// Creates the main menu of the application
pub fn create() -> Menu {
    let test_menu = Menu::new()
        .add_item(CustomMenuItem::new("new_window", "Create new window"));

    Menu::new()
        .add_submenu(Submenu::new("Test", test_menu))

}

/// Handles the various events that come from the application's menu
pub fn handler(event: WindowMenuEvent<Wry>) {
    match event.menu_item_id() {
        "new_window" => {
            let mut window = event.window().clone();
            let external_url = Url::parse("https://www.google.com").unwrap();
            window
                .create_window(
                    "acknowledgements".to_string(),
                    WindowUrl::External(external_url),
                    move |window_builder, webview_attributes| {
                        (
                            window_builder.resizable(false).center().visible(true),
                            webview_attributes,
                        )
                    },
                )
                .unwrap();
        }
        _ => {}
    }
}


fn main() {
  tauri::Builder::default()
      .menu(create())
      .on_menu_event(handler)
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Expected behavior

A new window should open

Platform and versions

Operating System - Mac OS, version 12.1.0 X64

Node.js environment
  Node.js - 14.17.5
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - 1.0.0-beta.8

Global packages
  npm - 8.0.0
  yarn - 1.22.17

Rust environment
  rustc - 1.57.0
  cargo - 1.57.0

App directory structure
/dist
/node_modules
/src-tauri

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../dist
  devPath - ../dist

Stack trace

Spindump/Sampled process:

Main thread call graph:
    2313 Thread_6171478   DispatchQueue_1: com.apple.main-thread  (serial)
    + 2313 start  (in dyld) + 462  [0x1164db4fe]
    +   2313 main  (in new-tauri-app) + 22  [0x10544cbf6]
    +     2313 std::rt::lang_start::h118e13e5ef4df79d  (in new-tauri-app) + 46  [0x10528885e]  rt.rs:145
    +       2313 std::rt::lang_start_internal::h59ad95b3bebc83b2  (in new-tauri-app) + 31  [0x105b0b88f]
    +         2313 std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::hba408c8aa5a21c36  (in new-tauri-app) + 20  [0x105288884]  rt.rs:146
    +           2313 std::sys_common::backtrace::__rust_begin_short_backtrace::h1bd899283b32bb22  (in new-tauri-app) + 17  [0x1053af5d1]  backtrace.rs:123
    +             2313 core::ops::function::FnOnce::call_once::h91ec077f3e48ff75  (in new-tauri-app) + 14  [0x1051969ee]  function.rs:227
    +               2313 new_tauri_app::main::h275fa4c6c08d0b3d  (in new-tauri-app) + 4282  [0x10544cb3a]  main.rs:45
    +                 2313 tauri::app::Builder$LT$R$GT$::run::h1af9169918993299  (in new-tauri-app) + 206  [0x10526686e]  app.rs:1098
    +                   2313 tauri::app::App$LT$R$GT$::run::h1b19cc6258effd53  (in new-tauri-app) + 332  [0x10526652c]  app.rs:480
    +                     2313 _$LT$tauri_runtime_wry..Wry$u20$as$u20$tauri_runtime..Runtime$GT$::run::hcfc9e4d14499f782  (in new-tauri-app) + 729  [0x105286689]  lib.rs:1866
    +                       2313 tao::event_loop::EventLoop$LT$T$GT$::run::h6020cc257c124fd3  (in new-tauri-app) + 108  [0x1052644bc]  event_loop.rs:152
    +                         2313 tao::platform_impl::platform::event_loop::EventLoop$LT$T$GT$::run::h38565947eb256f56  (in new-tauri-app) + 61  [0x10526536d]  event_loop.rs:147
    +                           2313 tao::platform_impl::platform::event_loop::EventLoop$LT$T$GT$::run_return::h1836615d480cd2e7  (in new-tauri-app) + 1327  [0x1052650bf]  event_loop.rs:180
    +                             2313 objc::message::platform::send_unverified::h705b28df8b27a529  (in new-tauri-app) + 92  [0x10583b62c]
    +                               2313 _$LT$$LP$$RP$$u20$as$u20$objc..message..MessageArguments$GT$::invoke::hbac3386416a16e2f  (in new-tauri-app) + 70  [0x10583b0e6]
    +                                 2313 -[NSApplication run]  (in AppKit) + 586  [0x7ff82134f5a9]
    +                                   2313 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]  (in AppKit) + 1411  [0x7ff82135d1b8]
    +                                     2313 _DPSNextEvent  (in AppKit) + 886  [0x7ff82135eb4c]
    +                                       2313 _BlockUntilNextEventMatchingListInModeWithFilter  (in HIToolbox) + 70  [0x7ff827561fe5]
    +                                         2313 ReceiveNextEventCommon  (in HIToolbox) + 587  [0x7ff827562247]
    +                                           2313 RunCurrentEventLoopInMode  (in HIToolbox) + 292  [0x7ff8275624f1]
    +                                             2313 CFRunLoopRunSpecific  (in CoreFoundation) + 563  [0x7ff81e92f5dd]
    +                                               2313 __CFRunLoopRun  (in CoreFoundation) + 1156  [0x7ff81e930122]
    +                                                 2313 __CFRunLoopDoObservers  (in CoreFoundation) + 543  [0x7ff81e930b54]
    +                                                   2313 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__  (in CoreFoundation) + 23  [0x7ff81e930cb7]
    +                                                     2313 tao::platform_impl::platform::observer::control_flow_end_handler::h9c5abaeaf1c7a44d  (in new-tauri-app) + 48  [0x1057cb360]
    +                                                       2313 tao::platform_impl::platform::observer::control_flow_handler::hf4b1969ebb8be054  (in new-tauri-app) + 249  [0x1057cb1a9]
    +                                                         2313 tao::platform_impl::platform::event_loop::stop_app_on_panic::h3aa67c77341bc49e  (in new-tauri-app) + 78  [0x10581345e]
    +                                                           2313 std::panic::catch_unwind::h8edbd3edd1d82694  (in new-tauri-app) + 21  [0x1057eff35]
    +                                                             2313 std::panicking::try::h57503dc5ef63dae1  (in new-tauri-app) + 94  [0x1058177ee]
    +                                                               2313 __rust_try  (in new-tauri-app) + 29  [0x1058251bd]
    +                                                                 2313 std::panicking::try::do_call::h75ee17fc935d3cda  (in new-tauri-app) + 54  [0x105817966]
    +                                                                   2313 tao::platform_impl::platform::observer::control_flow_handler::_$u7b$$u7b$closure$u7d$$u7d$::h06557f3c70c46066  (in new-tauri-app) + 40  [0x1057cb208]
    +                                                                     2313 tao::platform_impl::platform::observer::control_flow_end_handler::_$u7b$$u7b$closure$u7d$$u7d$::he8ba43ca355a1dcd  (in new-tauri-app) + 92  [0x1057cb3cc]
    +                                                                       2313 tao::platform_impl::platform::app_state::AppState::cleared::h1998e99a5d589602  (in new-tauri-app) + 982  [0x1057e0f76]
    +                                                                         2313 tao::platform_impl::platform::app_state::Handler::handle_nonuser_event::h0451528e2bad49c9  (in new-tauri-app) + 680  [0x1057df118]
    +                                                                           2313 _$LT$tao..platform_impl..platform..app_state..EventLoopHandler$LT$T$GT$$u20$as$u20$tao..platform_impl..platform..app_state..EventHandler$GT$::handle_nonuser_event::h413d327bcca7bda1  (in new-tauri-app) + 80  [0x10543df30]  app_state.rs:102
    +                                                                             2313 tao::platform_impl::platform::app_state::EventLoopHandler$LT$T$GT$::with_callback::h9c164b2f474565af  (in new-tauri-app) + 636  [0x10543e6bc]  app_state.rs:81
    +                                                                               2313 _$LT$tao..platform_impl..platform..app_state..EventLoopHandler$LT$T$GT$$u20$as$u20$tao..platform_impl..platform..app_state..EventHandler$GT$::handle_nonuser_event::_$u7b$$u7b$closure$u7d$$u7d$::h0629cfa21eb77342  (in new-tauri-app) + 374  [0x10543e0b6]  app_state.rs:103
    +                                                                                 2313 _$LT$tauri_runtime_wry..Wry$u20$as$u20$tauri_runtime..Runtime$GT$::run::_$u7b$$u7b$closure$u7d$$u7d$::haec9b1ff4bb87b62  (in new-tauri-app) + 401  [0x105286981]  lib.rs:1867
    +                                                                                   2313 tauri_runtime_wry::handle_event_loop::hfd72fd8fd7e632bf  (in new-tauri-app) + 2280  [0x1056ee188]
    +                                                                                     2313 _$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..Fn$LT$Args$GT$$GT$::call::hcadac97b28736cbd  (in new-tauri-app) + 33  [0x10576af41]
    +                                                                                       2313 tauri::window::Window$LT$R$GT$::on_menu_event::_$u7b$$u7b$closure$u7d$$u7d$::hb9f8a1b36c784d17  (in new-tauri-app) + 287  [0x105252c1f]  window.rs:344
    +                                                                                         2313 tauri::manager::WindowManager$LT$R$GT$::attach_window::_$u7b$$u7b$closure$u7d$$u7d$::h7de45a91d749b886  (in new-tauri-app) + 496  [0x10532d4e0]  manager.rs:717
    +                                                                                           2313 _$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..Fn$LT$Args$GT$$GT$::call::hec299f0faa6a8d4b  (in new-tauri-app) + 118  [0x10543c9c6]  boxed.rs:1705
    +                                                                                             2313 core::ops::function::Fn::call::h1aa850eda33a46f3  (in new-tauri-app) + 58  [0x105192eea]  function.rs:70
    +                                                                                               2313 new_tauri_app::handler::h662433b3c0c8ef8f  (in new-tauri-app) + 404  [0x10544b9f4]  main.rs:26
    +                                                                                                 2313 tauri::window::Window$LT$R$GT$::create_window::h6a9fb0f74648dbd3  (in new-tauri-app) + 599  [0x105252917]  window.rs:201
    +                                                                                                   2313 tauri::sealed::ManagerBase::create_new_window::h7523e8c531f0555d  (in new-tauri-app) + 1039  [0x10525134f]  lib.rs:383
    +                                                                                                     2313 _$LT$tauri_runtime_wry..WryDispatcher$u20$as$u20$tauri_runtime..Dispatch$GT$::create_window::h19e4df1a63357a12  (in new-tauri-app) + 707  [0x1056e63d3]
    +                                                                                                       2313 tauri_runtime_wry::send_user_message::hfa6318960340bfc1  (in new-tauri-app) + 730  [0x1056df93a]
    +                                                                                                         2313 tauri_runtime_wry::handle_user_message::h0019cb984a6b0b68  (in new-tauri-app) + 816  [0x1056e9530]
    +                                                                                                           2313 _$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..FnOnce$LT$Args$GT$$GT$::call_once::h5ab36d4260b7d47a  (in new-tauri-app) + 56  [0x10576b058]
    +                                                                                                             2313 core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h3f94ba7279f13c3e  (in new-tauri-app) + 40  [0x1056ca978]
    +                                                                                                               2313 _$LT$tauri_runtime_wry..WryDispatcher$u20$as$u20$tauri_runtime..Dispatch$GT$::create_window::_$u7b$$u7b$closure$u7d$$u7d$::hf58f7e3f6187b6de  (in new-tauri-app) + 139  [0x1056e672b]
    +                                                                                                                 2313 tauri_runtime_wry::create_webview::h655641e97840737a  (in new-tauri-app) + 1539  [0x1056f1143]
    +                                                                                                                   2313 std::sync::mutex::Mutex$LT$T$GT$::lock::he0a6e560b5cf14e2  (in new-tauri-app) + 36  [0x1056dcd44]
    +                                                                                                                     2313 std::sys_common::mutex::MovableMutex::raw_lock::he918ec6ad8ecd0b8  (in new-tauri-app) + 20  [0x1056bd374]
    +                                                                                                                       2313 std::sys::unix::mutex::Mutex::lock::he06a9db342524a0a  (in new-tauri-app) + 29  [0x1056bd39d]
    +                                                                                                                         2313 _pthread_mutex_firstfit_lock_slow  (in libsystem_pthread.dylib) + 204  [0x7ff81e865d58]
    +                                                                                                                           2313 _pthread_mutex_firstfit_lock_wait  (in libsystem_pthread.dylib) + 76  [0x7ff81e867e8f]
    +                                                                                                                             2313 __psynch_mutexwait  (in libsystem_kernel.dylib) + 10  [0x7ff81e82fcf2]

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions