zig-webui icon indicating copy to clipboard operation
zig-webui copied to clipboard

Linking GTK3 (On Linux) in a WebUI Zig project causes instant segfault on program startup.

Open jarcopolo opened this issue 2 months ago • 2 comments

OS: I tested it both on Fedora 43 and Arch Linux Zig version 0.14.1

I wanted to add OS file picker dialogs to my project and to do that, I tried using nfd-zig and osdialog-zig.
However, when importing any of these libraries, I get instant segfault on program startup, like this:

/usr/bin/zig build run
Segmentation fault at address 0x7f1182249b28
???:?:?: 0x7f1182249b28 in ??? (???)
Unwind error at address `libwebui.so:0x7f1182249b28` (error.AddressOutOfRange), trace may be incomplete

???:?:?: 0x7f1181789aa6 in ??? (libgtk-3.so.0)
???:?:?: 0x7f1181efddaf in ??? (libglib-2.0.so.0)
???:?:?: 0x7f1181785bf8 in ??? (libgtk-3.so.0)
???:?:?: 0x7f1181789821 in ??? (libgtk-3.so.0)
/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/nativefiledialog/src/nfd_gtk.c:175:11: 0x1037ff2 in NFD_OpenDialog (/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/nativefiledialog/src/nfd_gtk.c)
    if ( !gtk_init_check( NULL, NULL ) )
          ^
/home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/src/lib.zig:25:36: 0x10e6f99 in openFileDialog (webui_gtk_crash_demo)
    const result = c.NFD_OpenDialog(if (filter != null) filter.? else null, if (default_path != null) default_path.? else null, &out_path);
                                   ^
/home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/src/main.zig:6:27: 0x10e6e38 in main (webui_gtk_crash_demo)
    if (nfd.openFileDialog(null, null)) |path_opt|
                          ^
/usr/lib/zig/std/start.zig:660:37: 0x10e7812 in main (webui_gtk_crash_demo)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x7f118140f5b4 in ??? (libc.so.6)
???:?:?: 0x7f118140f667 in ??? (libc.so.6)
???:?:?: 0x1037114 in ??? (???)
run
└─ run webui_gtk_crash_demo failure

I created a repo with minimal reproduction examples, so that you can check it:
https://github.com/jarcopolo/webui-gtk-crash-demo

Apart from that, thanks for your work on the project, on Windows everything works alright.

jarcopolo avatar Nov 17 '25 22:11 jarcopolo

I will try to reproduce this problem I will address this issue later this week.

jinzhongjia avatar Nov 18 '25 06:11 jinzhongjia

I have a feeling this is not a WebUI bug.

Issue:

  • gtk_init_check() is failed maybe because webui_gtk_crash_demo attempts to use the osdialog_file function before GTK initialization
  • Check out this nfd-zig line

Info:

WebUI internal GTK initialization is _webui_load_gtk_and_webkit(), gets called in webui_set_center() or webui_show_wv().

Solution 1:

Load and make WebUI initialization GTK first before anything else. Make sure your project is running like this.

  • Import WebUI
  • (Main Thread) Create a window
  • (Main Thread) Call webui_set_center() or webui_show_wv() to initialize GTK
  • Now you are safe to call any nfd-zig and osdialog-zig functions because GTK is initialized by WebUI

Solution 2:

Make your own GTK project, add nfd-zig and osdialog-zig for files picker, and add WebUI only as web server and JavaScript bridge

AlbertShown avatar Nov 18 '25 15:11 AlbertShown

Your system has gtk2 and gtk3

jinzhongjia avatar Nov 23 '25 12:11 jinzhongjia

your demo works ok in windows, I'm trying build it under wsl archlinux

jinzhongjia avatar Nov 23 '25 12:11 jinzhongjia

https://manus.im/share/file/923f689f-ffd2-48ed-913f-57028cab6127

You can view this document, and on my archlinux wsl, it fails to build

jinzhongjia avatar Nov 23 '25 12:11 jinzhongjia

The log reports the library osdialog-zig has strange problem Image

jinzhongjia avatar Nov 23 '25 12:11 jinzhongjia

Thank you for your testing and insights. Regarding the osdialog.a error, I also got the same warning messages, but this doesn't appear to be the problem stopping my example from running.

I attached backtraces of my faulty programs to the bottom of this post.

In the meantime, I tested on more of my devices and also made a functionally identical project in C using the same libraries. The C version also reported segfault on startup (I was using 2.5.0-beta.3 version release from 7th of March)
After I updated the Zig dependency to use latest commit (I was probably using the version from 24th of August), it is working now. Later, I built the WebUI library from source using the latest commit, put it into my C test project and it also began to work.

I can provide more information if you are interested.
Thank you for your work.

Backtrace: Zig program, Only WebUI invocation, GTK3 linked ``` Thread 1 "webui_gtk_crash" received signal SIGSEGV, Segmentation fault. 0x00000000011da190 in gdk_display_get_default () (gdb) bt #0 0x00000000011da190 in gdk_display_get_default () #1 0x00007ffff77a5529 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:525 #2 0x00007ffff7789aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=, group=, data=0x1262cf0, error=0x7fffffffdca0) at ../gtk/gtkmain.c:804 #4 0x00007ffff7403db0 in g_option_context_parse (context=context@entry=0x1278550, argc=argc@entry=0x0, argv=argv@entry=0x0, error=error@entry=0x7fffffffdca0) at ../glib/goption.c:2087 #5 0x00007ffff7785bf9 in gtk_parse_args (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:1064 #6 0x00007ffff7789822 in gtk_init_check (argc=, argv=) at ../gtk/gtkmain.c:1110 #7 0x00007ffff778986d in gtk_init (argc=, argv=) at ../gtk/gtkmain.c:1170 #8 0x00000000011331a1 in _webui_load_gtk_and_webkit () at /home/wilson2/.cache/zig/p/webui-2.5.0-beta.4-pxqD5ahSNwAE_vnS170oThHZ3blPcHQ85Ut2XHf65f1u/src/webui.c:12293 #9 0x0000000001104a51 in webui_set_center (window=1) at /home/wilson2/.cache/zig/p/webui-2.5.0-beta.4-pxqD5ahSNwAE_vnS170oThHZ3blPcHQ85Ut2XHf65f1u/src/webui.c:2918 #10 0x00000000011037fb in webui_new_window_id (num=1) at /home/wilson2/.cache/zig/p/webui-2.5.0-beta.4-pxqD5ahSNwAE_vnS170oThHZ3blPcHQ85Ut2XHf65f1u/src/webui.c:1029 #11 0x0000000001103111 in webui_new_window () at /home/wilson2/.cache/zig/p/webui-2.5.0-beta.4-pxqD5ahSNwAE_vnS170oThHZ3blPcHQ85Ut2XHf65f1u/src/webui.c:980 #12 0x00000000010fc315 in webui.newWindow () at /home/wilson2/.cache/zig/p/zig_webui-2.5.0-beta.4-M4z7zbFYAQDke_SExQn8kx1PDa9jlOdLvnVANRh0MKoX/src/webui.zig:69 #13 0x00000000010fc285 in main.main () at main.zig:6 #14 0x00000000010fc6b8 in start.callMain () at /usr/lib/zig/std/start.zig:660 #15 start.callMainWithArgs () at /usr/lib/zig/std/start.zig:620 #16 start.main (c_argc=1, c_argv=0x7fffffffe4b8, c_envp=0x7fffffffe4c8) at /usr/lib/zig/std/start.zig:635 ```
Backtrace: Zig program, NFD + WebUI invocation, GTK3 linked ``` Thread 1 "webui_gtk_crash" received signal SIGSEGV, Segmentation fault. 0x00007ffff7449b28 in gdk_display_get_default () from /home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/.zig-cache/o/86c5389185ec1e9caacf99aedadd0e51/libwebui.so (gdb) bt #0 0x00007ffff7449b28 in gdk_display_get_default () at /home/wilson2/Documents/Coding/Zig/webui-gtk-crash-demo/.zig-cache/o/86c5389185ec1e9caacf99aedadd0e51/libwebui.so #1 0x00007ffff77a5529 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:525 #2 0x00007ffff7789aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=, group=, data=0x1100de0, error=0x7fffffffdf10) at ../gtk/gtkmain.c:804 #4 0x00007ffff7e3ddb0 in g_option_context_parse (context=context@entry=0x1100d80, argc=argc@entry=0x0, argv=argv@entry=0x0, error=error@entry=0x7fffffffdf10) at ../glib/goption.c:2087 #5 0x00007ffff7785bf9 in gtk_parse_args (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:1064 #6 0x00007ffff7789822 in gtk_init_check (argc=, argv=) at ../gtk/gtkmain.c:1110 #7 0x00000000010380a3 in NFD_OpenDialog (filterList=0x0, defaultPath=0x0, outPath=0x7fffffffe0a0) at /home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/nativefiledialog/src/nfd_gtk.c:175 #8 0x00000000010e704a in lib.openFileDialog (filter=..., default_path=...) at /home/wilson2/.cache/zig/p/nfdzig-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm/src/lib.zig:25 #9 0x00000000010e6ee9 in main.main () at main.zig:6 #10 0x00000000010e7a83 in start.callMain () at /usr/lib/zig/std/start.zig:660 #11 start.callMainWithArgs () at /usr/lib/zig/std/start.zig:620 #12 start.main (c_argc=1, c_argv=0x7fffffffe4b8, c_envp=0x7fffffffe4c8) at /usr/lib/zig/std/start.zig:635 ```
Backtrace: C program, NFD + WebUI invocation, GTK3 linked ``` Thread 1 "pickerc" received signal SIGSEGV, Segmentation fault. 0x000000000044e2a8 in g_signal_connect_data () (gdb) bt #0 0x000000000044e2a8 in g_signal_connect_data () #1 0x00007ffff79a5556 in _gtk_modules_init.constprop.0 (gtk_modules_args=gtk_modules_args@entry=0x0, argv=0x0, argc=0x0) at ../gtk/gtkmodules.c:526 #2 0x00007ffff7989aa7 in do_post_parse_initialization (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:766 #3 post_parse_hook (context=, group=, data=0x4b7d20, error=0x7fffffffdee0) at ../gtk/gtkmain.c:804 #4 0x00007ffff740fdb0 in g_option_context_parse (context=context@entry=0x4cd140, argc=argc@entry=0x0, argv=argv@entry=0x0, error=error@entry=0x7fffffffdee0) at ../glib/goption.c:2087 #5 0x00007ffff7985bf9 in gtk_parse_args (argc=0x0, argv=0x0) at ../gtk/gtkmain.c:1064 #6 0x00007ffff7989822 in gtk_init_check (argc=, argv=) at ../gtk/gtkmain.c:1110 #7 0x00007ffff798986d in gtk_init (argc=, argv=) at ../gtk/gtkmain.c:1170 #8 0x000000000041d4dd in _webui_show_window () #9 0x0000000000400e48 in main () at /home/wilson2/Documents/Coding/C/pickerc/src/main.c:9 ```

jarcopolo avatar Nov 24 '25 14:11 jarcopolo

Thank you for the test, we will check it soon. Yes, we are interested in this to know if it's a bug in WebUI or in your project.

AlbertShown avatar Nov 24 '25 14:11 AlbertShown

interesting, I'm not good at gtk, maybe you @AlbertShown can resolve this?

jinzhongjia avatar Nov 24 '25 14:11 jinzhongjia

https://github.com/jarcopolo/webui_with_gtk_filepicker Created this C (with Cmake) repo to test various WebUI versions. Looks like the issue is already solved since 17 of October, all commits newer than this one don't cause GTK problems anymore: https://github.com/webui-dev/webui/commit/bbe25a58b25e2617b19db85d977189710e7769fe

jarcopolo avatar Nov 24 '25 16:11 jarcopolo