gtk-rs / gtk3-rs

Rust bindings for GTK 3
https://gtk-rs.org
MIT License
508 stars 90 forks source link

[BUG] Regression (and panic) in 0.15.1 #716

Closed andy128k closed 2 years ago

andy128k commented 2 years ago

A program, which subclasses gtk::Application and does not invoke gtk::init explicitly, panics with

thread 'main' panicked at 'GTK has to be initialized first'

Backtrace

$ RUST_BACKTRACE=full cargo run --bin basic_subclass
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/basic_subclass`
thread 'main' panicked at 'GTK has to be initialized first', /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/gtk-0.15.1/src/subclass/application.rs:99:13
stack backtrace:
   0:     0x556fe5f6369c - std::backtrace_rs::backtrace::libunwind::trace::h093d4af0eabdfc15
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x556fe5f6369c - std::backtrace_rs::backtrace::trace_unsynchronized::h2b90813d74c759ca
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x556fe5f6369c - std::sys_common::backtrace::_print_fmt::hfaa8856bf3eca13f
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x556fe5f6369c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h0cbaef3adcb5a454
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x556fe5f7dbec - core::fmt::write::h35a8eb836b847360
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/fmt/mod.rs:1149:17
   5:     0x556fe5f60e05 - std::io::Write::write_fmt::h45f2b8390f189782
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/io/mod.rs:1697:15
   6:     0x556fe5f64df0 - std::sys_common::backtrace::_print::h56f62073b0e62985
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x556fe5f64df0 - std::sys_common::backtrace::print::h152fba05ec38941b
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x556fe5f64df0 - std::panicking::default_hook::{{closure}}::ha3121a0b8482251f
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:211:50
   9:     0x556fe5f649a5 - std::panicking::default_hook::hde5d78c11ae3b8f6
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:228:9
  10:     0x556fe5f654a4 - std::panicking::rust_panic_with_hook::he6f55c3e7ed1777c
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:606:17
  11:     0x556fe5f64f52 - std::panicking::begin_panic_handler::{{closure}}::h4b51effcc76f0c14
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:500:13
  12:     0x556fe5f63b64 - std::sys_common::backtrace::__rust_end_short_backtrace::haae2fe666128308b
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:139:18
  13:     0x556fe5f64ee9 - rust_begin_unwind
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:498:5
  14:     0x556fe5f07be1 - core::panicking::panic_fmt::h6434c641853e4979
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/panicking.rs:107:14
  15:     0x556fe5f1a536 - gtk::subclass::application::<impl glib::subclass::types::IsSubclassable<T> for gtk::auto::application::Application>::class_init::h220e9a05d13d49bc
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/gtk-0.15.1/src/subclass/application.rs:99:13
  16:     0x556fe5f1dbd3 - glib::subclass::types::ClassStruct::class_init::hb7895f3ed4b0aa88
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.2/src/subclass/types.rs:135:13
  17:     0x556fe5f121b9 - glib::subclass::types::class_init::h82e3c16a7e68442f
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.2/src/subclass/types.rs:802:9
  18:     0x7fa495999cb8 - type_class_init_Wm
                               at ./debian/build/deb/../../../gobject/gtype.c:2297:5
  19:     0x7fa495999cb8 - g_type_class_ref
                               at ./debian/build/deb/../../../gobject/gtype.c:3012:5
  20:     0x556fe5f46446 - glib::object::Class<T>::from_type::h3377a9cc2c30a45b
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.2/src/object.rs:3776:23
  21:     0x556fe5f4410e - glib::object::Object::with_type::hb8cae465f7341190
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.2/src/object.rs:1224:25
  22:     0x556fe5f15462 - glib::object::Object::new::h84499e85c0039a8a
                               at /home/andy/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.15.2/src/object.rs:1209:12
  23:     0x556fe5f1804e - basic_subclass::simple_application::SimpleApplication::new::h122133fd6554f83c
                               at /home/andy/tmp/examples/basic_subclass/simple_application/mod.rs:14:9
  24:     0x556fe5f1b1b9 - basic_subclass::main::hd73435a68ad7e233
                               at /home/andy/tmp/examples/basic_subclass/main.rs:9:15
  25:     0x556fe5f1df3b - core::ops::function::FnOnce::call_once::hc6f6e16cd5bbf620
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/ops/function.rs:227:5
  26:     0x556fe5f1b29e - std::sys_common::backtrace::__rust_begin_short_backtrace::h7a53e240ef583a53
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/sys_common/backtrace.rs:123:18
  27:     0x556fe5f0b9c1 - std::rt::lang_start::{{closure}}::hdd48ca258476c1e4
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:145:18
  28:     0x556fe5f6332b - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h7422298f811ee14d
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/ops/function.rs:259:13
  29:     0x556fe5f6332b - std::panicking::try::do_call::hcba55cf6d5b5533e
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:406:40
  30:     0x556fe5f6332b - std::panicking::try::h0b2a05128a4ee609
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:370:19
  31:     0x556fe5f6332b - std::panic::catch_unwind::he1deef49e02fb06c
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panic.rs:133:14
  32:     0x556fe5f6332b - std::rt::lang_start_internal::{{closure}}::hf44e73ef18e45ffd
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:128:48
  33:     0x556fe5f6332b - std::panicking::try::do_call::h894daf8a782b48f4
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:406:40
  34:     0x556fe5f6332b - std::panicking::try::hd3e4f8d86f3a7fb5
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panicking.rs:370:19
  35:     0x556fe5f6332b - std::panic::catch_unwind::h2e69404746fb3d50
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/panic.rs:133:14
  36:     0x556fe5f6332b - std::rt::lang_start_internal::hec7f1b06f38d8409
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:128:20
  37:     0x556fe5f0b990 - std::rt::lang_start::h18494e64debb07cd
                               at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/rt.rs:144:17
  38:     0x556fe5f1b21c - main
  39:     0x7fa4949177ed - __libc_start_main
                               at ./csu/../csu/libc-start.c:332:16
  40:     0x556fe5f0830a - _start
  41:                0x0 - <unknown>
bilelmoussaoui commented 2 years ago

How this worked before ? Pretty sure it didn't. We need a way to register a hook into gio::ApplicationImpl to mark gtk as intialiazed before the startup vfunc is run and the code doesn't do that, there is an open issue on gtk4-rs for the same thing .

bilelmoussaoui commented 2 years ago

See also https://github.com/gtk-rs/gtk4-rs/issues/236

andy128k commented 2 years ago

@A6GibKm it definitely IS a regression. Gtk promises that GtkApplication does initialization.

It’s important to note that if you use GtkApplication, the application class will take care of initializing GTK for you, as well as spinning the main loop.

Bug is caused by a state rt::INITIALIZED in gtk-rs be out of sync with Gtk's state.

bilelmoussaoui commented 2 years ago

Please look at the issue I attached it explains why it never worked before.

andy128k commented 2 years ago

@bilelmoussaoui The issue you've linked explains why rt::INITIALIZED was not reliable (and still is). It does not contradict to my statement that 0.15.x is broken.

A6GibKm commented 2 years ago

I cannot say for gtk3 as i havent used it in some time. But I assure you it never worked in gtk4-rs.

andy128k commented 2 years ago

@A6GibKm It works(-ed) in gtk4-rs. I have a gtk4 branch of the same app (but cannot merge in because of https://github.com/gtk-rs/gtk4-rs/issues/584).

sdroege commented 2 years ago

The workaround would be to call gtk::set_initialized at the right place in your code.

For solving this properly, maybe we should just initialize gtk ourselves before calling startup(). @bilelmoussaoui what do you think?

bilelmoussaoui commented 2 years ago

@sdroege We already do https://github.com/gtk-rs/gtk3-rs/blob/master/gtk/src/subclass/application.rs#L92? or do you suggest calling 'gtk::init' there?

sdroege commented 2 years ago

Yes, calling gtk::init() before calling imp.startup(). That's ugly but at least solves it once and for all.

bilelmoussaoui commented 2 years ago

@sdroege or maybe we could use something similar to what we did for gtk_widget_class_install_action and store a function to be called before gio::ApplicationImp::startup ? it shouldn't be hard to do, i can give it a look this weekend

sdroege commented 2 years ago

It has to happen after GtkApplication::startup ran, but I have an idea.

If someone has a testcase for me then I can make this work probably.

sdroege commented 2 years ago

See https://github.com/gtk-rs/gtk3-rs/pull/723

sdroege commented 2 years ago

This is fixed in 0.15.2 now