kovidgoyal / kitty

Cross-platform, fast, feature-rich, GPU based terminal
https://sw.kovidgoyal.net/kitty/
GNU General Public License v3.0
24.41k stars 980 forks source link

Unexpected high CPU usage in some circumstances #7436

Closed jchv closed 5 months ago

jchv commented 5 months ago

Describe the bug This is a relatively new bug I'm hitting on NixOS unstable with kitty 0.34.1 under Sway 1.9. Basically, in some circumstances, Kitty will begin using a lot of CPU, spinning parsing keymaps. "Some circumstances" mainly surrounds behavior when tabbing in and out of Kitty or starting another instance of it. I've not really narrowed down how or why. Even within a single instance, I can trigger the high CPU usage by creating a new window with Ctrl+Shift+Enter. What's even more strange is that I can stop the high CPU usage by going to another workspace, opening another Kitty instance, and exiting it. This will stop the other Kitty instances from spinning!

When looking in perf top, I can see that it is spinning inside of _xkbcommon_parse primarily. I don't have debug symbols right now (sorry) but if I attach with gdb and reproduce the issue, I get the following stack trace:

#0  0x00007fbc1654e730 in _xkbcommon_parse () from /nix/store/6wrlfjwx23kd535z192lk4h70k8ws6c5-libxkbcommon-1.5.0/lib/libxkbcommon.so.0
#1  0x00007fbc1654ff6e in parse () from /nix/store/6wrlfjwx23kd535z192lk4h70k8ws6c5-libxkbcommon-1.5.0/lib/libxkbcommon.so.0
#2  0x00007fbc1655ef4f in XkbParseString () from /nix/store/6wrlfjwx23kd535z192lk4h70k8ws6c5-libxkbcommon-1.5.0/lib/libxkbcommon.so.0
#3  0x00007fbc1656439b in text_v1_keymap_new_from_string () from /nix/store/6wrlfjwx23kd535z192lk4h70k8ws6c5-libxkbcommon-1.5.0/lib/libxkbcommon.so.0
#4  0x00007fbc16566b4b in xkb_keymap_new_from_buffer () from /nix/store/6wrlfjwx23kd535z192lk4h70k8ws6c5-libxkbcommon-1.5.0/lib/libxkbcommon.so.0
#5  0x00007fbc165b8985 in keyboardHandleKeymap.lto_priv.0 () from /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/lib/kitty/kitty/glfw-wayland.so
#6  0x00007fbc16736052 in ffi_call_unix64 () from /nix/store/ym7yrwqvzcb3i3js4x322nyd4c5k944l-libffi-3.4.6/lib/libffi.so.8
#7  0x00007fbc16733ee5 in ffi_call_int () from /nix/store/ym7yrwqvzcb3i3js4x322nyd4c5k944l-libffi-3.4.6/lib/libffi.so.8
#8  0x00007fbc16734ad8 in ffi_call () from /nix/store/ym7yrwqvzcb3i3js4x322nyd4c5k944l-libffi-3.4.6/lib/libffi.so.8
#9  0x00007fbc165997a1 in wl_closure_invoke () from /nix/store/19idnjxghxxzwmh2hrzs5j3vxw7czyza-wayland-1.22.0/lib/libwayland-client.so.0
#10 0x00007fbc16595bd9 in dispatch_event.isra () from /nix/store/19idnjxghxxzwmh2hrzs5j3vxw7czyza-wayland-1.22.0/lib/libwayland-client.so.0
#11 0x00007fbc16597524 in wl_display_dispatch_queue_pending () from /nix/store/19idnjxghxxzwmh2hrzs5j3vxw7czyza-wayland-1.22.0/lib/libwayland-client.so.0
#12 0x00007fbc165b1ebe in glfwRunMainLoop () from /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/lib/kitty/kitty/glfw-wayland.so
#13 0x00007fbc17813cdc in main_loop.lto_priv () from /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/bin/../lib/kitty/kitty/fast_data_types.so
#14 0x00007fbc18c413c2 in method_vectorcall_NOARGS () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#15 0x00007fbc18c5ad93 in PyObject_Vectorcall () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#16 0x00007fbc18af7c5c in _PyEval_EvalFrameDefault () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#17 0x00007fbc18d2b57a in _PyEval_Vector () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#18 0x00007fbc18c4ab66 in _PyObject_FastCallDictTstate () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#19 0x00007fbc18c4b227 in slot_tp_call () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#20 0x00007fbc18c015ad in _PyObject_MakeTpCall () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#21 0x00007fbc18af7c5c in _PyEval_EvalFrameDefault () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#22 0x00007fbc18d2bc9c in _PyEval_Vector.constprop.0 () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#23 0x00007fbc18d2be3a in PyEval_EvalCode () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#24 0x00007fbc18d5757d in builtin_exec () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#25 0x00007fbc18b661c2 in cfunction_vectorcall_FASTCALL_KEYWORDS () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#26 0x00007fbc18c5ad93 in PyObject_Vectorcall () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#27 0x00007fbc18af7c5c in _PyEval_EvalFrameDefault () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#28 0x00007fbc18d2b57a in _PyEval_Vector () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#29 0x00007fbc18d73878 in pymain_run_module () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#30 0x00007fbc18d796fe in Py_RunMain () from /nix/store/lpi16513bai8kg2bd841745vzk72475x-python3-3.11.9/lib/libpython3.11.so.1.0
#31 0x000000000040282d in main ()

To Reproduce Steps to reproduce the behavior:

  1. Run NixOS unstable + Sway 1.9 + Kitty
  2. Open Kitty
  3. Press Ctrl+Shift+Enter
  4. Check CPU usage in Top

Screenshots N/A

Environment details

kitty 0.34.1 created by Kovid Goyal
Linux taiga 6.8.9 #1-NixOS SMP PREEMPT_DYNAMIC Thu May  2 14:35:35 UTC 2024 x86_64

<<< Welcome to NixOS 24.05.20240507.b211b39 (x86_64) - /dev/tty >>>

Run 'nixos-help' for the NixOS manual.

DISTRIB_CODENAME=uakari
DISTRIB_DESCRIPTION="NixOS 24.05 (Uakari)"
DISTRIB_ID=nixos
DISTRIB_RELEASE="24.05"
LSB_VERSION="24.05 (Uakari)"
Running under: Wayland (sway version 1.9) missing: blur
Frozen: False
Paths:
  kitty: /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/bin/kitty
  base dir: /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/lib/kitty
  extensions dir: /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/lib/kitty/kitty
  system shell: /run/current-system/sw/bin/zsh
Loaded config files:
  /etc/xdg/kitty/kitty.conf

Config options different from defaults:
background_opacity         0.97
clipboard_control          ('write-clipboard', 'write-primary')
dim_opacity                0.75
modify_font:
    cell_height 0
    cell_width 0
select_by_word_characters  :@-./_~?&=%+#
single_window_margin_width FloatEdges(left=-1000.0, top=-1000.0, right=-1000.0, bottom=-1000.0)
tab_title_template         {title}
window_border_width        (1.0, 'pt')
Added shortcuts:
    kitty_mod+p > n →  kitten hints --type hash --program -, kitten hints --type word --program -, kitten hints --type line --program -, kitten hints --type path, kitten hints --type path --program -, kitten hints --type hyperlink, kitten hints --type linenum
Removed shortcuts:
    kitty_mod+p > f →  kitten hints --type hyperlink, kitten hints --type linenum, kitten hints --type hash --program -, kitten hints --type word --program -, kitten hints --type line --program -, kitten hints --type path, kitten hints --type path --program -
Changed shortcuts:
    kitty_mod+e →  kitten hints
Colors:
    background                 #1d1f21   
    color0                     #282a2e   
    color1                     #a54242   
    color10                    #b5bd68   
    color11                    #f0c674   
    color12                    #81a2be   
    color13                    #b294bb   
    color14                    #8abeb7   
    color15                    #c5c8c6   
    color2                     #8c9440   
    color3                     #de935f   
    color4                     #5f819d   
    color5                     #85678f   
    color6                     #5e8d87   
    color7                     #707880   
    color8                     #373b41   
    color9                     #cc6666   
    foreground                 #c5c8c6   

Important environment variables seen by the kitty process:
    PATH                                /nix/store/7bkj1dkjkan3blf13zajp9v5s5cfrh68-kitty-0.34.1/bin:/nix/store/kcc6jv90wk4l48ry5fg2kr3n376y0avg-imagemagick-7.1.1-30/bin:/nix/store/1byq5q5hnj1n9k5si3w8pd8q3rzxnbfq-ncurses-6.4-dev/bin:/run/wrappers/bin:/home/john/.local/share/flatpak/exports/bin:/var/lib/flatpak/exports/bin:/home/john/.nix-profile/bin:/nix/profile/bin:/home/john/.local/state/nix/profile/bin:/etc/profiles/per-user/john/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin
    LANG                                en_US.UTF-8
    EDITOR                              vim
    SHELL                               /run/current-system/sw/bin/zsh
    DISPLAY                             :0
    WAYLAND_DISPLAY                     wayland-1
    USER                                john
    XCURSOR_SIZE                        24
    XDG_CONFIG_DIRS                     /etc/xdg:/home/john/.local/share/flatpak/exports/etc/xdg:/var/lib/flatpak/exports/etc/xdg:/home/john/.nix-profile/etc/xdg:/nix/profile/etc/xdg:/home/john/.local/state/nix/profile/etc/xdg:/etc/profiles/per-user/john/etc/xdg:/nix/var/nix/profiles/default/etc/xdg:/run/current-system/sw/etc/xdg
    XDG_SEAT                            seat0
    XDG_SESSION_DESKTOP                 sway
    XDG_SESSION_TYPE                    wayland
    XDG_CURRENT_DESKTOP                 sway
    XDG_SESSION_CLASS                   user
    XDG_VTNR                            2
    XDG_SESSION_ID                      2
    XDG_RUNTIME_DIR                     /run/user/1000
    XDG_DATA_DIRS                       /nix/store/w7ixjb6aqw6rx7aycjn02kssi14fszk9-desktops/share:/home/john/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/home/john/.nix-profile/share:/nix/profile/share:/home/john/.local/state/nix/profile/share:/etc/profiles/per-user/john/share:/nix/var/nix/profiles/default/share:/run/current-system/sw/share

Additional context I am able to reproduce the problem with kitty --config NONE. I've been using Sway 1.9 with Kitty for quite some time and very pleased, so I doubt it is related to that. The most recent update in NixOS that is likely to be somehow related is libxkbcommon: 1.5.0 -> 1.7.0.

I have reproduced this on two different machines (with similar NixOS configurations.)

I will continue to try to debug this, but I hope maybe someone will have an idea of what might be wrong based on the evidence I've gathered.

kovidgoyal commented 5 months ago

Disable fcitx5 or set wayland_enable_ime no in kitty.conf.

jchv commented 5 months ago

Thanks for the quick response! Yep, it was the fcitx5 issue. Sorry!

I do use IME with Kitty sometimes so I opted to apply your workaround patch and it is working great. Hopefully other users might find this issue via search and not have to open another issue.

On the off chance there are other NixOS users arriving here by search, you can cherry-pick the IME fix early like this:

{ pkgs, ... }:
{
  config = {
    nixpkgs.overlays = [
      (final: prev: {
        kitty = prev.kitty.overrideAttrs (prevAttrs: {
          patches = (prevAttrs.patches or [ ]) ++ [
            (pkgs.fetchpatch {
              name = "fcitx-workaround.diff";
              url = "https://github.com/kovidgoyal/kitty/commit/aecf07bcbaa74507526d3f700954842176fef4f2.diff";
              hash = "sha256-6m4D/xX5dx0v6rhzIyTlkQyCRb2Ik2Ek3q0BrL3saTw=";
            })
          ];
        });
      })
    ];
  };
}