kovidgoyal/kitty

panic in `kitten diff` when running under shpool and scrolling with mouse wheel

willangley opened this issue · 5 comments

Describe the bug

When I use shpool for session persistence, I'm unable to scroll with the mouse wheel in kitten diff; it panics.

To Reproduce

  1. willangley@local ~ % kitten ssh remote

  2. willangley@remote:~$ cd /tmp

  3. One-time: create some files to diff.

    willangley@remote:/tmp$ cat >>0.txt
    zero
    willangley@remote:/tmp$ cat >>1.txt
    zero
    one
  4. Diff the files with kitty:
    willangley@remote:/tmp$ kitty +kitten diff 0.txt 1.txt

  5. Scroll up and down using page up, page down: kitty will beep.

  6. Scroll up and down using the mouse wheel: kitty will beep.

  7. Exit the kitten with q.

  8. Attach to shpool:
    willangley@remote:/tmp$ shpool attach bug

  9. Return to the temporary directory:
    shpool:bug willangley@remote:~$ cd /tmp

  10. Diff the files with kitty:
    shpool:bug willangley@remote:/tmp$ kitty +kitten diff 0.txt 1.txt

  11. Scroll up and down using page up, page down: kitty will beep.

  12. Scroll up or down using the mouse wheel; kitty will panic, and should not.

    Panicked with error: runtime error: integer divide by zero
    Stacktrace (most recent call first):
    kitty/tools/tui/loop.pixel_to_cell
        kitty/tools/tui/loop/mouse.go:215
    kitty/tools/tui/loop.decode_sgr_mouse
        kitty/tools/tui/loop/mouse.go:262
    kitty/tools/tui/loop.MouseEventFromCSI
        kitty/tools/tui/loop/mouse.go:279
    kitty/tools/tui/loop.(*Loop).handle_csi
        kitty/tools/tui/loop/run.go:92
    kitty/tools/wcswidth.(*EscapeCodeParser).dispatch_esc_code
        kitty/tools/wcswidth/escape-code-parser.go:153
    kitty/tools/wcswidth.(*EscapeCodeParser).dispatch_byte
        kitty/tools/wcswidth/escape-code-parser.go:291
    kitty/tools/wcswidth.(*EscapeCodeParser).ParseByte
        kitty/tools/wcswidth/escape-code-parser.go:95
    kitty/tools/wcswidth.(*EscapeCodeParser).Parse
        kitty/tools/wcswidth/escape-code-parser.go:106
    kitty/tools/tui/loop.(*Loop).dispatch_input_data
        kitty/tools/tui/loop/read.go:25
    kitty/tools/tui/loop.(*Loop).run
        kitty/tools/tui/loop/run.go:496
    kitty/tools/tui/loop.(*Loop).Run
        kitty/tools/tui/loop/api.go:282
    kitty/kittens/diff.main
        kitty/kittens/diff/main.go:163
    kitty/kittens/diff.create_cmd.func1
        kitty/kittens/diff/cli_generated.go:15
    kitty/tools/cli.(*Command).ExecArgs
        kitty/tools/cli/command.go:546
    kitty/tools/cli.(*Command).Exec
        kitty/tools/cli/command.go:564
    main.main
        kitty/tools/cmd/main.go:34
    runtime.main
        runtime/proc.go:267
    Press any key to exit.
    

Screenshots

KittenDiffBug.mp4

Environment details

kitty 0.33.1 (a5fea33757) created by Kovid Goyal
Darwin ███████████████████████████████████ 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64
ProductName:		macOS ProductVersion:		14.4.1 BuildVersion:		23E224
Frozen: True
Paths:
  kitty: /Applications/kitty.app/Contents/MacOS/kitty
  base dir: /Applications/kitty.app/Contents/Resources/kitty
  extensions dir: /Applications/kitty.app/Contents/Resources/Python/lib/kitty-extensions
  system shell: /bin/zsh
Loaded config files:
  /Users/willangley/.config/kitty/kitty.conf

Config options different from defaults:
background_opacity 0.8
font_family        Menlo
font_size          14.0
Removed shortcuts:
	cmd+, →  edit_config_file
	cmd+enter →  new_window
	cmd+h →  hide_macos_app
	cmd+k →  clear_terminal to_cursor active
	cmd+m →  minimize_macos_window
	cmd+n →  new_os_window
	cmd+q →  quit
	cmd+t →  new_tab
	cmd+w →  close_tab
	ctrl+cmd+, →  load_config_file
	ctrl+cmd+f →  toggle_fullscreen
	opt+cmd+h →  hide_macos_other_apps
	opt+cmd+r →  clear_terminal reset active
	opt+cmd+s →  toggle_macos_secure_keyboard_entry
	shift+cmd+/ →  open_url https://sw.kovidgoyal.net/kitty/
	shift+cmd+[ →  previous_tab
	shift+cmd+] →  next_tab
	shift+cmd+d →  close_window
	shift+cmd+w →  close_os_window
Changed shortcuts:
	kitty_mod+f1 →  new_window_with_cwd

Important environment variables seen by the kitty process:
	PATH                                /Applications/kitty.app/Contents/MacOS:/usr/bin:/bin:/usr/sbin:/sbin
	LANG                                en_US.UTF-8
	SHELL                               /bin/zsh
	USER                                willangley

Additional context

I've installed shpool using its Debian package on the affected machine, and have not customized its config.

This is a bug in shpool it is not setting the full window size struct
(the latter two pixel size fields to be precise) to the kernel via
TIOCWINSZ for tty's it creates.

That said, I will fix the kitten to at least not panic, though mouse
interactions will not work.

Thank you for the prompt investigation and fix, @kovidgoyal!

+1 about the quick diagnosis @kovidgoyal!

I'm the shpool maintainer and I've started looking into fixing this. It is pretty easy to just forward the xpixel and ypixel fields along side the rows and column fields, but one weird thing is that I notice that man ioctl_tty on my system claims that the fields are unused. In particular, here is the relevant bit of the man page

   Get and set window size
       Window sizes are kept in the kernel, but not used by the kernel (except in the case of virtual consoles, where the  kernel  will  update
       the window size when the size of the virtual console changes, for example, by loading a new font).

       TIOCGWINSZ
              Argument: struct winsize *argp

              Get window size.

       TIOCSWINSZ
              Argument: const struct winsize *argp

              Set window size.

       The struct used by these ioctls is defined as

           struct winsize {
               unsigned short ws_row;
               unsigned short ws_col;
               unsigned short ws_xpixel;   /* unused */
               unsigned short ws_ypixel;   /* unused */
           };

       When the window size changes, a SIGWINCH signal is sent to the foreground process group.

are those /* unused */ comments just mistaken? I think this is why I elided these fields in the first place.

yes, they are mistaken. These days many terminals fill them in,
including the venerable xterm. And they are used by terminal programs
that need pixel sizes for example to display graphics or have pixel
precision mouse handling.

Ok good to know. Thanks! Shpool will now forward the pixel widths correctly so it should play nice with kitty.