f3d-app / f3d

Fast and minimalist 3D viewer.
https://f3d.app
BSD 3-Clause "New" or "Revised" License
2.67k stars 188 forks source link

Segfault when thumbnailing with nautilus (Headless support) #439

Closed lalten closed 1 year ago

lalten commented 1 year ago

Describe the bug F3D segfaults when Nautilus asks it to generate thumbnails.

To Reproduce Steps to reproduce the behavior:

  1. Open a directory containing files to be thumbnailed by F3D in Nautilus
  2. Observe the crash in journalctl -f

Note that I can open the files just fine in f3d directly and can also do

❯ f3d --dry-run -sta --no-background --output=out.png --resolution="256,256" ~/Downloads/Messbecher-20210813.STL

out You can find the STL here: https://www.printables.com/model/73864-washing-powder-scoop/files (but it will happen to every 3D model, not just this one)

Expected behavior I expected thumbnails to be generated and shown in the file manager.

System Information:

F3D Information Paste the content of f3d --version:

❯ f3d --version
f3d 1.3.1

F3D - A fast and minimalist 3D viewer
Version: 1.3.1.
Build date: 2022-09-26 21:20:02.
Build system: Linux x86_64.
Compiler: GNU 12.2.0.
External rendering module: ON.
Raytracing module: ON.
Exodus module: ON.
OpenCASCADE module: 7.5.3 (full support).
Assimp module: 5.2.4.
Alembic module: 1.8.3.
VTK version: 9.1.0 (build 0).
Copyright (C) 2019-2021 Kitware SAS.
Copyright (C) 2021-2022 Michael Migliore, Mathieu Westphal.
License BSD-3-Clause.
By Michael Migliore, Mathieu Westphal and Joachim Pouderoux.

Additional context

❯ coredumpctl -1 info

           PID: 86471 (f3d)
           UID: 1000 (laurenz)
           GID: 1000 (laurenz)
        Signal: 11 (SEGV)
     Timestamp: Mon 2022-09-26 23:22:41 CEST (18min ago)
  Command Line: f3d --config=thumbnail --quiet --output=/tmp/gnome-desktop-thumbnailer.png --resolution=256,256 /tmp/LGX-Lite-Simplified-V6.step
    Executable: /usr/bin/f3d
 Control Group: /user.slice/user-1000.slice/user@1000.service/session.slice/dbus.service
          Unit: user@1000.service
     User Unit: dbus.service
         Slice: user-1000.slice
     Owner UID: 1000 (laurenz)
       Boot ID: 4c979aced18544ba8adbdb95256a66c9
    Machine ID: d2a6ddbbc02a477f9211d600d9bc5086
      Hostname: tpxy
       Storage: /var/lib/systemd/coredump/core.f3d.1000.4c979aced18544ba8adbdb95256a66c9.86471.1664227361000000.zst (present)
     Disk Size: 2.8M
       Message: Process 86471 (f3d) of user 1000 dumped core.

Stacktrace (via coredumpctl -1 debug):

Core was generated by `f3d --config=thumbnail --quiet --output=/tmp/gnome-desktop-thumbnailer.png --re'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f138293d0ed in XSync () from /usr/lib/libX11.so.6
(gdb) bt full
#0  0x00007f138293d0ed in XSync () at /usr/lib/libX11.so.6
#1  0x00007f1382a503cb in vtkXRenderWindowInteractor::Initialize() () at /usr/lib/libvtkRenderingUI.so.1
#2  0x00007f138e1514d7 in f3d::detail::interactor_impl::internals::internals(f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&) (this=0x55586dc26510, options=<optimized out>, window=<optimized out>, loader=<optimized out>)
    at /usr/src/debug/f3d/library/src/interactor_impl.cxx:36
        keyPressCallback = {Object = <optimized out>}
        dropFilesCallback = {Object = <optimized out>}
#3  0x00007f138e14f2b1 in std::make_unique<f3d::detail::interactor_impl::internals, f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&>(f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&) ()
    at /usr/include/c++/12.2.0/bits/unique_ptr.h:1065
#4  f3d::detail::interactor_impl::interactor_impl(f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&) (this=this@entry=0x55586dc22260, options=..., window=..., loader=...) at /usr/src/debug/f3d/library/src/interactor_impl.cxx:299
#5  0x00007f138e13c9f7 in std::make_unique<f3d::detail::interactor_impl, f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&>(f3d::options&, f3d::detail::window_impl&, f3d::detail::loader_impl&) ()
    at /usr/include/c++/12.2.0/bits/unique_ptr.h:1064
#6  f3d::engine::engine(f3d::window::Type) (this=this@entry=0x55586dc2d670, windowType=windowType@entry=f3d::window::Type::NATIVE_OFFSCREEN) at /usr/src/debug/f3d/library/src/engine.cxx:43
#7  0x000055586bbe4129 in std::make_unique<f3d::engine, f3d::window::Type>(f3d::window::Type&&) () at /usr/include/c++/12.2.0/bits/unique_ptr.h:1064
        window = <optimized out>
        interactor = <optimized out>
        files = std::vector of length 1, capacity 1 = {"/tmp/LGX-Lite-Simplified-V6.step"}
#8  F3DStarter::Start(int, char**) (this=this@entry=0x7ffcb9a88920, argc=argc@entry=6, argv=argv@entry=0x7ffcb9a88a58) at /usr/src/debug/f3d/application/F3DStarter.cxx:129
        window = <optimized out>
        interactor = <optimized out>
        files = std::vector of length 1, capacity 1 = {"/tmp/LGX-Lite-Simplified-V6.step"}
#9  0x000055586bb88c8b in main(int, char**) (argc=6, argv=0x7ffcb9a88a58) at /usr/src/debug/f3d/application/main.cxx:13
        starter = {Internals = std::unique_ptr<F3DStarter::F3DInternals> = {get() = 0x55586dc0ef30}}
        res = 1

Attached dumpfile:

❯ coredumpctl dump -1 > f3d-crash-2022092.dump                 
❯ tar cavf f3d-crash-2022092.dump.tar.gz f3d-crash-2022092.dump

Dumpfile: f3d-crash-2022092.dump.tar.gz

mwestphal commented 1 year ago

I reproduce, nautilus must be doing something special with the libraries when using a thumbnailer, will investigate.

mwestphal commented 1 year ago

here is the backtrace:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007ff6a54f80ed in XSync () from /usr/lib/libX11.so.6
(gdb) bt
#0  0x00007ff6a54f80ed in XSync () at /usr/lib/libX11.so.6
#1  0x00007ff6ae0a33cb in vtkXRenderWindowInteractor::Initialize() () at /usr/lib/libvtkRenderingUI.so.1
#2  0x00007ff6b0a1c167 in  () at /usr/lib/libf3d.so
#3  0x00007ff6b0a19f41 in  () at /usr/lib/libf3d.so
#4  0x00007ff6b0a07687 in f3d::engine::engine(f3d::window::Type) () at /usr/lib/libf3d.so
#5  0x00005653367f9145 in std::make_unique<f3d::engine, f3d::window::Type>(f3d::window::Type&&) ()
    at /usr/include/c++/12.2.0/bits/unique_ptr.h:1065
#6  0x00005653367f6d8b in F3DStarter::Start(int, char**) (this=0x7ffe02e47b50, argc=5, argv=0x7ffe02e47c88)
    at /home/glow/dev/f3d/src/application/F3DStarter.cxx:112
#7  0x00005653367fa32c in main(int, char**) (argc=5, argv=0x7ffe02e47c88)
    at /home/glow/dev/f3d/src/application/main.cxx:13
(gdb) 
mwestphal commented 1 year ago

So this may have something to do with permissions to access Xorg from the thubnailer in nautilus, F3D cant generate an image without an Xorg server running (for now, this may come in the future). You may want to investigate in this direction.

mwestphal commented 1 year ago

Somehow similar issue in VTK with docker ?

https://gitlab.kitware.com/vtk/vtk/-/issues/17887

https://discourse.vtk.org/t/sigsegv-in-xsync-from-lib-x86-64-linux-gnu-libx11-so-6-on-ubuntu-20-04-wsl/5952

mwestphal commented 1 year ago

Turns out this is cause by nautilus using bwrap to sandbox the thumbnailers. There is no fix for this unless F3D start supporting headless rendering (through EGL or OSMESA). This may come at some point but not in the short term for sure. You can also build VTK in headless (EGL or OSMESA) and build F3D yourself in order to get a f3d binary that should work in nautilus.

Related issues: https://github.com/xournalpp/xournalpp/issues/1612

That being said, a work around that works right away is to use a virtual X server like xvfb or Xephyr. We use xvfb in our CI. By simply adding xvfb-run in the thumbnailer file like this:

Exec=xvfb-run f3d --dry-run -sta --no-background --output=%o --resolution=%s,%s %i

I was able to generate this in nautilus:

a

However, starting Xvfb comes with a big hoverhead, so this is more a hack than an actual solution.

We can keep this open until headless is supported by our binary.

mwestphal commented 1 year ago

I will add the workaround in our doc.

mwestphal commented 1 year ago

btw I opened a discussion on gnome discourse: https://discourse.gnome.org/t/about-the-use-of-bwrap-sandboxing-in-nautilus-thumbnailer/11314

mwestphal commented 1 year ago

We discussed this with @Meakk , it may be not that hard to add EGL support to F3D, we will look into it !

lalten commented 1 year ago

Thanks a lot for the quick investigation and workaround @mwestphal!

lalten commented 1 year ago

I can confirm the workaround works and I can confirm it is very slow ;P

❯ cat /usr/share/thumbnailers/f3d.thumbnailer
[Thumbnailer Entry]
Type=X-Thumbnailer
Name=f3d Thumbnailer
TryExec=f3d
Exec=xvfb-run f3d --config=thumbnail --quiet --output=%o --resolution=%s,%s %i
MimeType=application/vnd.3ds;application/gml+xml;application/dicom;model/gltf-binary;model/gltf+json;application/vnd.mhd;application/vnd.nrrd;model/obj;application/vnd.ply;application/vnd.pts;model/stl;application/x-tgif;model/vrml;application/vnd.vtk;application/vnd.vtp;application/vnd.vtu;application/vnd.vtr;application/vnd.vti;application/vnd.vts;application/vnd.vtm;model/iges;application/vnd.step;application/vnd.fbx;application/vnd.dae;image/vnd.dxf;application/vnd.off;application/vnd.abc;application/vnd.exodus
knoopx commented 1 year ago

not even the workaround did work for me :(

(gnome 43, x11 session)

(org.gnome.Nautilus:206148): GnomeDesktop-DEBUG: 18:54:42.321: Failed to launch script: X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  150 (GLX)
  Minor opcode of failed request:  3 (X_GLXCreateContext)
  Value in failed request:  0x0
  Serial number of failed request:  61
  Current serial number in output stream:  62
(org.gnome.Nautilus:206148): GnomeDesktop-DEBUG: 18:54:39.187: About to launch script: bwrap --ro-bind /usr /usr --ro-bind-try /etc/ld.so.cache /etc/ld.so.cache --symlink /usr//bin /bin --symlink /usr//lib64 /lib64 --symlink /usr//lib /lib --symlink /usr//sbin /sbin --ro-bind-try /var/cache/fontconfig /var/cache/fontconfig --setenv GST_REGISTRY_1_0 /home/knoopx/.cache/gnome-desktop-thumbnailer/gstreamer-1.0/gstreamer-1.0.registry --bind /home/knoopx/.cache/gnome-desktop-thumbnailer/gstreamer-1.0 /home/knoopx/.cache/gnome-desktop-thumbnailer/gstreamer-1.0 --ro-bind-try /etc/alternatives /etc/alternatives --proc /proc --dev /dev --chdir / --setenv GIO_USE_VFS local --unshare-all --die-with-parent --setenv G_MESSAGES_DEBUG all --bind /tmp/gnome-desktop-thumbnailer-1YI7U1 /tmp --ro-bind file.obj /tmp/gnome-desktop-file-to-thumbnail.obj --seccomp 54 xvfb-run f3d --config=thumbnail --quiet --output=/tmp/gnome-desktop-thumbnailer.png --resolution=512,512 /tmp/gnome-desktop-file-to-thumbnail.obj 
mwestphal commented 1 year ago

Thats unexpecte. Is F3D working locally, with or without xvfb-run ?

knoopx commented 1 year ago

yes, working natively and under xvfb-run but not under bwrap

mwestphal commented 1 year ago

Here is an experimental EGL version, feel free to test it in Nautilus. It works without a Xorg server here, but I need to install a nautilus setup to test it. https://transfer.sh/8li3fa/F3D-Linux-raytracing-egl.zip

mwestphal commented 1 year ago

headless version is now part of the release system and the nightly release contains it !

Download it here (the ones with headless in the name): https://github.com/f3d-app/f3d/releases/tag/nightly

We also documented the usecase here: https://f3d.app/doc/user/LIMITATIONS_AND_TROUBLESHOOTING.html#linux

However it is not possible to install both .deb release, so if you want to use the classic F3D release at the same time, you will want to extract the archive somewhere bwrap has access to and modify the .thumbnailer files (yes, they are now multiple) to point to it.

mwestphal commented 1 year ago

@lalten @knoopx