Repository: neovim
Issue: ui_attach: ill-formed redraw event with Neovide and noice.nvim
Labels: bug,api,ui,lua,ui-extensibility

### Describe the bug

Related to https://github.com/folke/noice.nvim/issues/17, https://github.com/neovide/neovide/issues/1751, and maybe #21265
Neovide version (patched for debugging): https://github.com/ryo33/neovide/commit/db6ba6d783e098614521b3b0e7299fcc22209a50
Noice.nvim version: https://github.com/folke/noice.nvim/commit/d8a1f3056ad713b5d471048f8d029264828e22c0
nvim-notify version: https://github.com/rcarriga/nvim-notify/commit/bdd647f61a05c9b8a57c83b78341a0690e9c29d7
nui.nvim version: https://github.com/MunifTanjim/nui.nvim/commit/d147222a1300901656f3ebd5b95f91732785a329

## Abstract

Using Neovide GUI with noice.nvim suddenly crashes occasionally. I've found that, in most cases, it's caused by Neovim emitting an Ill-formed redraw event and subsequent rpc messages are broken (see Appendix 2.) Some people report the sudden crash in the same case, and [one reported the almost same backtrace as mine](https://github.com/neovide/neovide/issues/1751#issuecomment-1435671936).

The event from `neovide_backtrace.log` with printing in [ron format](https://github.com/ron-rs/ron) at: `(File: src/bridge/handler.rs; Line: 77, Column: 25)`
```ron
[
    "msg_show",
    [
        [],
        [
            true,
        ],
        [
            "msg_showcmd",
            [
                [],
            ],
        ],
    ],
]
```

## The situation

- Neovide call `ui_attach` with `ext_linegrid` and `rgb`.
- noice.nvim call `ui_attach` with `ext_messages`, `ext_cmdline`, and `ext_popupmenu`.
- noice.nvim sometimes trigger redraw while handling events.
  - https://github.com/neovide/neovide/issues/1751#issuecomment-1435915499
  - https://github.com/neovide/neovide/issues/1751#issuecomment-1436498838

## Appendix 1.

Starting Neovim with `nvim -u NONE` and running the following lua code without `--` in front of `print("hello")` crashes neovim silently. This might be related to this issue. If someone tells me that this is an unexpected behavior, I'll open another issue.

```lua
local first = true
local ns = vim.api.nvim_create_namespace "test"
vim.ui_attach(ns, {
  ext_messages = true,
}, function(event, kind, content, replace_last)
  if first then
    -- print("hello")
    first = false
  end
end)
```

Edited: I noticed there is #21265

## Appendix 2.

To ensure that the MessagePack and RPC implementation Neovide uses isn't doing wrong, I write the following Python script:

```python
import sys
import pprint
from pynvim import attach
path = sys.argv[1]
nvim = attach("socket", path = path)
nvim.ui_attach(1000, 1000, True, ext_linegrid = True)
pp = pprint.PrettyPrinter(indent=2, width=40)
def on_notify(name, args):
    if name != "redraw":
        return
    str = pp.pformat(args)
    if "'msg_show'" not in str:
        return
    print(str)

def on_request(name, args):
    pass

nvim.run_loop(on_request, on_notify)
```

It prints:

<details>
  <summary>Click me</summary>

```
[ ['cmdline_hide', [1]],
  [ 'msg_show',
    [ 'emsg',
      [[4, 'E32: No file name']],
      False]],
  ['mode_change', ['normal', 0]],
  ['mouse_on', []],
  ['flush', []]]
[ [ 'win_viewport',
    [ 2,
      <Window(handle=1000)>,
      0,
      10,
      1,
      0,
      6]],
  [ 'grid_line',
    [1, 2, 0, [[' ', 0]]],
    [1, 3, 0, [[' ', 0]]],
    [1, 5, 0, [['a', 0]]],
    [1, 6, 0, [['~', 7], [' ', 7, 71]]],
    [1, 7, 0, [['~', 7], [' ', 7, 71]]],
    [1, 8, 0, [['~', 7], [' ', 7, 71]]],
    [1, 26, 54, [['2', 9]]]],
  [ 'hl_attr_define',
    [ 383,
      { 'background': 16711935,
        'foreground': 11140968},
      { 'background': 13,
        'foreground': 0},
      []],
    [ 384,
      { 'background': 16711935,
        'foreground': 5203794},
      { 'background': 13,
        'foreground': 0},
      []]],
  [ 'grid_line',
    [ 1,
      0,
      20,
      [ ['╭', 65],
        ['─', 65, 50],
        ['╮']]],
    [1, 1, 20, [['│', 65]]],
    [1, 1, 71, [['│', 65]]],
    [1, 2, 20, [['│', 65]]],
    [1, 2, 71, [['│', 65]]],
    [1, 3, 20, [['│', 65]]],
    [1, 3, 71, [['│', 65]]],
    [ 1,
      4,
      20,
      [ ['╰', 65],
        ['─', 65, 50],
        ['╯']]],
    [ 1,
      1,
      21,
      [ [' ', 41],
        ['\uf057', 343],
        [' '],
        ['E'],
        ['r', 343, 2],
        ['o'],
        ['r'],
        [' ', 343, 5],
        [' ', 41, 31],
        ['2', 343, 2],
        [':'],
        ['4'],
        ['3'],
        [' ', 41]]],
    [1, 2, 21, [['━', 342, 50]]],
    [ 1,
      3,
      21,
      [ ['E', 382],
        ['3'],
        ['2'],
        [':'],
        [' '],
        ['N'],
        ['o'],
        [' '],
        ['f'],
        ['i'],
        ['l'],
        ['e'],
        [' '],
        ['n'],
        ['a'],
        ['m'],
        ['e'],
        [' ', 41, 33]]]],
  [ 'msg_show',
    ['', [[0, '3 fewer lines']], True]],
  ['msg_showmode', [[]]],
  [ 'win_viewport',
    [ 2,
      <Window(handle=1000)>,
      0,
      7,
      1,
      0,
      6]],
  [ 'grid_line',
    [ 1,
      5,
      20,
      [ ['╭', 67],
        ['─', 67, 50],
        ['╮']]],
    [ 1,
      6,
      20,
      [ ['│', 67],
        [' ', 41],
        ['\uf05a', 383],
        [' '],
        ['M'],
        ['e'],
        ['s', 383, 2],
        ['a'],
        ['g'],
        ['e'],
        ['s'],
        [' ', 41, 33],
        ['2', 383, 2],
        [':'],
        ['4'],
        ['3'],
        [' ', 41],
        ['│', 67]]],
    [ 1,
      7,
      20,
      [ ['│', 67],
        ['━', 384, 50],
        ['│', 67]]],
    [ 1,
      8,
      20,
      [ ['│', 67],
        ['3', 41],
        [' '],
        ['f'],
        ['e'],
        ['w'],
        ['e'],
        ['r'],
        [' '],
        ['l'],
        ['i'],
        ['n'],
        ['e'],
        ['s'],
        [' ', 41, 37],
        ['│', 67]]],
    [ 1,
      9,
      20,
      [ ['╰', 67],
        ['─', 67, 50],
        ['╯']]]],
  ['grid_cursor_goto', [1, 26, 55]],
  ['flush', []]]
[ [ 'msg_show',
    [ [],
      [True],
      ['msg_showcmd', [[]]]]],
  ['grid_cursor_goto', [1, 1, 0]],
  ['mode_change', ['normal', 0]],
  ['flush', []],
  [ 2,
    'redraw',
    [ [ 'grid_line',
        [ 1,
          5,
          20,
          [ ['╭', 67],
            ['─', 67, 50],
            ['╮']]],
        [1, 6, 20, [['│', 67]]],
        [1, 6, 71, [['│', 67]]],
        [1, 7, 20, [['│', 67]]],
        [1, 7, 71, [['│', 67]]],
        [1, 8, 20, [['│', 67]]],
        [1, 8, 71, [['│', 67]]],
        [ 1,
          9,
          20,
          [ ['╰', 67],
            ['─', 67, 50],
            ['╯']]],
        [ 1,
          6,
          21,
          [ [' ', 41],
            ['\uf05a', 383],
            [' '],
            ['M'],
            ['e'],
            ['s', 383, 2],
            ['a'],
            ['g'],
            ['e'],
            ['s'],
            [' ', 41, 33],
            ['2', 383, 2],
            [':'],
            ['4'],
            ['3'],
            [' ', 41]]],
        [1, 7, 21, [['━', 384, 50]]],
        [ 1,
          8,
          21,
          [ ['3', 41],
            [' '],
            ['f'],
            ['e'],
            ['w'],
            ['e'],
            ['r'],
            [' '],
            ['l'],
            ['i'],
            ['n'],
            ['e'],
            ['s'],
            [' ', 41, 37]]],
        [ 1,
          0,
          20,
          [ ['╭', 65],
            ['─', 65, 50],
            ['╮']]],
        [1, 1, 20, [['│', 65]]],
        [1, 1, 71, [['│', 65]]],
        [1, 2, 20, [['│', 65]]],
        [1, 2, 71, [['│', 65]]],
        [1, 3, 20, [['│', 65]]],
        [1, 3, 71, [['│', 65]]],
        [ 1,
          4,
          20,
          [ ['╰', 65],
            ['─', 65, 50],
            ['╯']]],
        [ 1,
          1,
          21,
          [ [' ', 41],
            ['\uf057', 343],
            [' '],
            ['E'],
            ['r', 343, 2],
            ['o'],
            ['r'],
            [' ', 343, 5],
            [' ', 41, 31],
            ['2', 343, 2],
            [':'],
            ['4'],
            ['3'],
            [' ', 41]]],
        [1, 2, 21, [['━', 342, 50]]],
        [ 1,
          3,
          21,
          [ ['E', 382],
            ['3'],
            ['2'],
            [':'],
            [' '],
            ['N'],
            ['o'],
            [' '],
            ['f'],
            ['i'],
            ['l'],
            ['e'],
            [' '],
            ['n'],
            ['a'],
            ['m'],
            ['e'],
            [' ', 41, 33]]]],
      ['msg_showmode', [[]]],
      [ 'win_viewport',
        [ 2,
          <Window(handle=1000)>,
          0,
          7,
          1,
          0,
          6]],
      ['flush', []]]]]
```

</details>

### Steps to reproduce

neovide -- -u NONE
:packadd nvim-notify
:packadd nui.nvim
:packadd noice.nvim
:lua require("noice").setup {}
" 1. input several lines
" 2. type 'd2k' in normal mode
" repeat 1 and 2 a few times 

### Expected behavior

Neovim emits a well-formed redraw event.

### Neovim version (nvim -v)

NVIM v0.9.0-dev-1004+gbfe6b4944-dirty

### Vim (not Nvim) behaves the same?

no

### Operating system/version

Ubuntu 22.04.1 LTS

### Terminal name/version

GNOME Terminal Version 3.44.0 for GNOME 42

### $TERM environment variable

xterm-256color

### Installation

homebrew (linuxbrew)