Repository: flxzt/rnote (language: rust)

Issue:
Crash/Freeze using it on a touchscreen with a stylus (Hyprland)
**Describe the bug**  
When using RNote on my HP Envy Convertible with an HP Stylus Pen, the program often randomly crashes or becomes partly unresponsive. I have uploaded a video to demonstrate both cases, since it's like a 50/50 chance of which one happens:

**To Reproduce**  
I cannot reliably reproduce the error, it happens somewhat randomly. The most reliable way for me is to bring the stylus close to the screen periodically and zoom/pan around. This video shows both cases happening (sorry for the sketchy recording, I had to hold my phone while doing this to record haha):

https://github.com/flxzt/rnote/assets/55491906/c6687be1-d6a0-4d1a-9b65-4291c98f961a

The first time, the program just crashes to desktop, the second time it freezes, not allowing me to pan/zoom anymore (I can still tap on the buttons though). When I try similar things in e.g. Firefox by just panning around and putting the stylus near the screen, Firefox has no problem with it and continues as usual when the stylus is removed.

**Expected behavior**  
The program staying responsive and usable.

**Console Output**  
When running with flatpak + the env flags:

```
(rnote:2): Gdk-WARNING **: 23:22:46.096: Failed to read portal settings: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface “org.freedesktop.portal.Settings” on object at path /org/freedesktop/portal/desktop

(rnote:2): Gtk-WARNING **: 23:22:47.866: Creating a portal monitor failed: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface “org.freedesktop.portal.Inhibit” on object at path **/org/freedesktop/portal/desktop
```

When running as a Arch package, I additionally get a Segmentation fault printed to the console.

**Desktop (please complete the following information):**  
 - OS: Arch Linux
 - App Version: 0.8.2
 - Installation Source: Flatpak, Arch package
 - Desktop Environment: Hyprland 0.29.1



Discussion / hints:
I get very similar behaviour on a similar machine (also HP ENVY using stylus). Also crashes with SEGFAULT when using the native package. Flatpack does not solve the issuse sadly.

* OS: Manjaro Linux `6.5.7-2-MANJARO`
* App Version: Commit `92bfa54357733a85b393cc7138247b88c677e9dd`
* Install Source: git / Manjaro repos / Flatpack all show the issue
* Desktop Environment: Gnome 45.0 on Wayland

gdb gets me this backtrace suggesting a cause in the gesture-handling for two-finger-long-press.
```
Thread 1 "rnote" received signal SIGSEGV, Segmentation fault.
0x00007ffff6e315fe in gdk_event_get_event_type () from /usr/lib/libgtk-4.so.1
(gdb) bt
#0  0x00007ffff6e315fe in gdk_event_get_event_type () at /usr/lib/libgtk-4.so.1
#1  0x00007ffff6c63ef8 in  () at /usr/lib/libgtk-4.so.1
#2  0x00007ffff6b47ba7 in gtk_gesture_set_sequence_state () at /usr/lib/libgtk-4.so.1
#3  0x00007ffff6b4a17f in gtk_gesture_set_state () at /usr/lib/libgtk-4.so.1
#4  0x00005555558124d9 in gtk4::auto::gesture::GestureExt::set_state<gtk4::auto::gesture_zoom::GestureZoom> (self=<optimized out>, state=...)
    at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gtk4-0.7.3/src/auto/gesture.rs:208
#5  0x0000555555823482 in rnote::canvaswrapper::imp::{impl#3}::setup_input::{closure#11} (gesture=0x7fffffffbfd0, _event_sequence=<error reading variable: Cannot access memory at address 0x0>)
    at crates/rnote-ui/src/canvaswrapper.rs:451
#6  gtk4::auto::gesture::GestureExt::connect_end::end_trampoline<gtk4::auto::gesture_zoom::GestureZoom, rnote::canvaswrapper::imp::{impl#3}::setup_input::{closure_env#11}>
    (this=<optimized out>, sequence=<optimized out>, f=<optimized out>) at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gtk4-0.7.3/src/auto/gesture.rs:309
#7  0x00007ffff7ebb4f7 in g_cclosure_marshal_VOID__BOXEDv () at /usr/lib/libgobject-2.0.so.0
#8  0x00007ffff7ed6b73 in  () at /usr/lib/libgobject-2.0.so.0
#9  0x00007ffff7ed6c77 in g_signal_emit_valist () at /usr/lib/libgobject-2.0.so.0
#10 0x00007ffff7ed6d34 in g_signal_emit () at /usr/lib/libgobject-2.0.so.0
#11 0x00007ffff6b478a8 in  () at /usr/lib/libgtk-4.so.1
#12 0x00007ffff6b48353 in  () at /usr/lib/libgtk-4.so.1
#13 0x00007ffff6c6626a in  () at /usr/lib/libgtk-4.so.1
#14 0x00007ffff6c66485 in  () at /usr/lib/libgtk-4.so.1
#15 0x00007ffff6eb6826 in  () at /usr/lib/libgtk-4.so.1
#16 0x00007ffff6c64083 in  () at /usr/lib/libgtk-4.so.1
#17 0x00007ffff6b47ba7 in gtk_gesture_set_sequence_state () at /usr/lib/libgtk-4.so.1
#18 0x00007ffff6c63cd1 in  () at /usr/lib/libgtk-4.so.1
#19 0x00007ffff6b47ba7 in gtk_gesture_set_sequence_state () at /usr/lib/libgtk-4.so.1
#20 0x00005555557d4eae in gtk4::auto::gesture::GestureExt::set_sequence_state<gtk4::auto::gesture_long_press::GestureLongPress> (self=<optimized out>, sequence=0x7fffffffc8c8, state=...)
    at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gtk4-0.7.3/src/auto/gesture.rs:197
#21 0x0000555555823939 in rnote::canvaswrapper::imp::{impl#3}::setup_input::{closure#20} (gesture=0x7fffffffc8c0, event_sequence=<error reading variable: Cannot access memory at address 0x0>)
    at crates/rnote-ui/src/canvaswrapper.rs:576
#22 gtk4::auto::gesture::GestureExt::connect_end::end_trampoline<gtk4::auto::gesture_long_press::GestureLongPress, rnote::canvaswrapper::imp::{impl#3}::setup_input::{closure_env#20}>
    (this=<optimized out>, sequence=0x1, f=<optimized out>) at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gtk4-0.7.3/src/auto/gesture.rs:309
#23 0x00007ffff7eb76c0 in g_closure_invoke () at /usr/lib/libgobject-2.0.so.0
#24 0x00007ffff7ee5a36 in  () at /usr/lib/libgobject-2.0.so.0
#25 0x00007ffff7ed6a42 in  () at /usr/lib/libgobject-2.0.so.0
#26 0x00007ffff7ed6c77 in g_signal_emit_valist () at /usr/lib/libgobject-2.0.so.0
#27 0x00007ffff7ed6d34 in g_signal_emit () at /usr/lib/libgobject-2.0.so.0
#28 0x00007ffff6b478a8 in  () at /usr/lib/libgtk-4.so.1
#29 0x00007ffff6b4861b in  () at /usr/lib/libgtk-4.so.1
#30 0x00007ffff6b4e050 in  () at /usr/lib/libgtk-4.so.1
#31 0x00007ffff6c6626a in  () at /usr/lib/libgtk-4.so.1
#32 0x00007ffff6c66485 in  () at /usr/lib/libgtk-4.so.1
#33 0x00007ffff6eb6826 in  () at /usr/lib/libgtk-4.so.1
#34 0x00007ffff6b8ba55 in  () at /usr/lib/libgtk-4.so.1
#35 0x00007ffff6c759cc in  () at /usr/lib/libgtk-4.so.1
#36 0x00007ffff6ddae20 in  () at /usr/lib/libgtk-4.so.1
#37 0x00007ffff6e4ecec in  () at /usr/lib/libgtk-4.so.1
#38 0x00007ffff7ed6b73 in  () at /usr/lib/libgobject-2.0.so.0
#39 0x00007ffff7ed6c77 in g_signal_emit_valist () at /usr/lib/libgobject-2.0.so.0
#40 0x00007ffff7ed6d34 in g_signal_emit () at /usr/lib/libgobject-2.0.so.0
#41 0x00007ffff6ead5b0 in  () at /usr/lib/libgtk-4.so.1
--Type <RET> for more, q to quit, c to continue without paging--c
#42 0x00007ffff6dedfee in  () at /usr/lib/libgtk-4.so.1
#43 0x00007ffff7db0f19 in  () at /usr/lib/libglib-2.0.so.0
#44 0x00007ffff7e0f2b7 in  () at /usr/lib/libglib-2.0.so.0
#45 0x00007ffff7daf112 in g_main_context_iteration () at /usr/lib/libglib-2.0.so.0
#46 0x00007ffff7ad1af6 in g_application_run () at /usr/lib/libgio-2.0.so.0
#47 0x00005555557cfbd0 in gio::application::ApplicationExtManual::run_with_args<rnote::app::RnApp, alloc::string::String> (args=&[alloc::string::String](size=1) = {...}, self=<optimized out>)
    at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gio-0.18.2/src/application.rs:29
#48 gio::application::ApplicationExtManual::run<rnote::app::RnApp> (self=<optimized out>) at _mesonbuild/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/gio-0.18.2/src/application.rs:22
#49 0x00005555557576b6 in rnote::main () at crates/rnote-ui/src/main.rs:78
```

I assume the problem lies with setting the sequence-state on gestures around `canvaswrapper.rs:450` or `:558`

I can reproduce this reliably, so I would be able to test or get better traces if needed :) I use rnote regularly for grading student assignments and its perfect for that :heart: Thanks for implementing auto-save that really auto-saved my butt a couple of times.


**EDIT:**
In case this helps debugging, I just tried commenting out the handlers for the `touch_two_finger_long_press_gesture`. This fixes the crashing for me but obviously is not a proper solution
Hey, thanks a lot for investigating this.

My intuition would be that is has something to do with this code [here](https://github.com/flxzt/rnote/blob/11308dd72d1a48900d1e3367ff0233f1693bcdfe/crates/rnote-ui/src/canvaswrapper.rs#L593). I tried to be careful because I also experienced a segfault but obviously not careful enough. IIRC this was needed to make the gesture work in combination with the other installed gestures.
With a possible fix attention must also be paid to not regress issue #595 (as mentioned in the code comment)

Fix patch:
diff --git a/crates/rnote-ui/src/canvaswrapper.rs b/crates/rnote-ui/src/canvaswrapper.rs
index 8920475c9e..a2fcddfaa3 100644
--- a/crates/rnote-ui/src/canvaswrapper.rs
+++ b/crates/rnote-ui/src/canvaswrapper.rs
@@ -589,31 +589,6 @@ mod imp {
                         .handle_pressed_shortcut_key(ShortcutKey::TouchTwoFingerLongPress, Instant::now());
                     canvaswrapper.canvas().emit_handle_widget_flags(widget_flags);
                 }));
-
-                self.touch_two_finger_long_press_gesture.connect_end(
-                    clone!(@weak obj as canvaswrapper => move |gesture, event_sequence| {
-                        // Only deny the sequence that is actually handled.
-                        // Because this gesture is grouped with the zoom gesture, denying all
-                        // sequences within the group ( by calling `set_state()` ) might result in a segfault in certain cases
-                        if let Some(es) = event_sequence {
-                            // setting event sequences states directly is deprecated,
-                            // but it is not clear how to refactor it while not regressing the fix 4c33594 for #595
-                            #[allow(deprecated)]
-                            gesture.set_sequence_state(es, EventSequenceState::Denied);
-                        }
-                    }),
-                );
-
-                self.touch_two_finger_long_press_gesture.connect_cancel(
-                    clone!(@weak obj as canvaswrapper => move |gesture, event_sequence| {
-                        if let Some(es) = event_sequence {
-                            // setting event sequences states directly is deprecated,
-                            // but it is not clear how to refactor it while not regressing the fix 4c33594 for #595
-                            #[allow(deprecated)]
-                            gesture.set_sequence_state(es, EventSequenceState::Denied);
-                        }
-                    }),
-                );
             }
 
             {


Test patch: