Bug 481381

Summary: Crash when filtering certain information from pyright LSP
Product: [Applications] kate Reporter: Alex Lowe <lengau>
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: quinten, waqar.17a
Priority: NOR    
Version First Reported In: 23.08.4   
Target Milestone: ---   
Platform: Kubuntu   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: Crash dump
Crash dump from crash when changing output type in `charmcraft/store/registry.py`
Crash when using pyright 1.1.225

Description Alex Lowe 2024-02-15 20:52:27 UTC
Created attachment 165841 [details]
Crash dump

SUMMARY
***

Some type of input from pyright appears to cause kate to segfault. I can't unfortunately figure out which input exactly, but it does appear to be related to using pyright as an LSP


STEPS TO REPRODUCE
1. Open a large project (e.g charmcraft: https://github.com/canonical/charmcraft) in Kate
2. Set pyright as your Python LSP:
```
"pyright": {
      "command": ["pyright-langserver", "--stdio"],
      "url": "https://github.com/Microsoft/pyright",
      "highlightingModeRegex": "^Python$",
      "root": ".",
      "settings": {
        "pyright": {
          "disableLanguageServices": false,
          "disableOrganizeImports": false,
          "disableTaggedHints": false
        },
        "python": {
          "analysis": {
            "diagnosticMode": "workspace",
            "logLevel": "Information",
            "typeCheckingMode": "strict"
          },
          "pythonPath": ".venv/bin/python"
        }
      }
    }
```
3. In the Diagnostics tab, filter for LSP and only for errors.
4. Find some errors and start modifying code, using tab complete, etc.

OBSERVED RESULT

Segfault

EXPECTED RESULT

No segfault

SOFTWARE/OS VERSIONS
Kate: 23.08.4
Linux/KDE Plasma:
(available in About System)
KDE Plasma Version: 5.27.10
KDE Frameworks Version: 5.113.0
Qt Version: 5.15.10

ADDITIONAL INFORMATION
Comment 1 Alex Lowe 2024-02-15 20:58:12 UTC
Created attachment 165842 [details]
Crash dump from crash when changing output type in `charmcraft/store/registry.py`

This crash dump is from a session where I was changing return type of `_extract_file` in charmcraft/store/registry.py. Pyright was reporting an invalid type form (Tuple expression not allowed in type annotation). I deleted the opening parenthesis of the tuple type and began typing "tuple". This crash is repeatable from this commit: https://github.com/canonical/charmcraft/commit/c5cae21182139db75ebd455b7398272994895c9e
Comment 2 Waqar Ahmed 2024-02-26 06:31:47 UTC
Hi, thanks for reporting the crash.

I have tried to reproduce the crash using the following commit 

https://github.com/canonical/charmcraft/commit/6374ef9e1b6d6f88d7a048e63bb91b633b44c09b

pyright version: pyright 1.1.193

Kate 23.08.4

I tried to change the method return type the same way you were doing i.e., remove `(` and started typing tuple but so far I haven't been able to reproduce the crash.

I have also tried with kate master which is built with Qt6 and no crashes there as well.

Are you using the same pyright version? Perhaps there is another case where it crashes?
Comment 3 Alex Lowe 2024-03-09 20:32:43 UTC
Thanks for responding Waqar! The pyright version you're using is quite old (2021 it seems): https://github.com/microsoft/pyright/releases/tag/1.1.193

I've tried with the oldest version I could get from pypi (1.1.225) and it still crashes for me. The crash is only reproducible when the Diagnostics tab is open, with LSP selected from the dropdown, and with Errors selected to the left of the filter box. It takes a few times messing with the annotations, but I can reliably reproduce it with all tried versions of pyright (1.1.225, 1.1.349, 1.1.352) by changing the return type of _extract_file to str, then to tuple[str, str], then back to str, then to tuple[str, str, str]. This occurs both in the current version in Kubuntu 24.04 (Version: 4:23.08.5-0ubuntu1) and in the version from snap (23.08.4 2023-12-10 (171)). I've attached another crash file using pyright 1.1.225.
Comment 4 Alex Lowe 2024-03-09 20:34:03 UTC
Created attachment 166828 [details]
Crash when using pyright 1.1.225
Comment 5 Waqar Ahmed 2024-03-18 09:50:54 UTC
*** Bug 483870 has been marked as a duplicate of this bug. ***
Comment 6 Waqar Ahmed 2024-03-18 16:34:59 UTC
Git commit eab99aae7207e8d669d0ec367a34fddd211fcbbb by Waqar Ahmed.
Committed on 18/03/2024 at 11:03.
Pushed by cullmann into branch 'master'.

diag: Fix filtering after removing previous items

When this goes wrong we end up having an invalid idx in
syncDiagnosticItems and crash (because there is no guard there atm)

M  +2    -0    apps/lib/diagnostics/diagnosticview.cpp

https://invent.kde.org/utilities/kate/-/commit/eab99aae7207e8d669d0ec367a34fddd211fcbbb