fix(lsp): on detach, cancel pending foldingRange requests #31509

Problem:
1. Open a relatively large file (so the server needs some time to
   process the request).
2. Then immediately execute `:bdelete`.
3. Once the request is completed, the handler will obtain the bufstate
   of a buffer already unloaded.

    Error executing vim.schedule lua callback: ...7841_1/share/nvim/runtime/lua/vim/lsp/_folding_range.lua:119: assertion failed!
    stack traceback:
            [C]: in function 'assert'
            ...7841_1/share/nvim/runtime/lua/vim/lsp/_folding_range.lua:119: in function 'multi_handler'
            ...7841_1/share/nvim/runtime/lua/vim/lsp/_folding_range.lua:140: in function 'handler'
            ...HEAD-c137841_1/share/nvim/runtime/lua/vim/lsp/client.lua:669: in function ''
            vim/_editor.lua: in function <vim/_editor.lua:0>

Solution:
On detach, cancel all pending textDocument_foldingRange requests.
This commit is contained in:
Yi Ming
2025-02-12 22:22:59 +08:00
committed by GitHub
parent 2c629ad13f
commit be8d87014c
2 changed files with 45 additions and 5 deletions

View File

@@ -197,6 +197,11 @@ local function setup(bufnr)
-- `on_detach` also runs on buffer reload (`:e`).
-- Ensure `bufstate` and hooks are cleared to avoid duplication or leftover states.
on_detach = function()
util._cancel_requests({
bufnr = bufnr,
method = ms.textDocument_foldingRange,
type = 'pending',
})
bufstates[bufnr] = nil
api.nvim_clear_autocmds({ buffer = bufnr, group = augroup_setup })
end,