fix(terminal): crash when TermClose switches back to terminal buffer

Problem:  Crash when deleting terminal buffer and TermClose switches
          back to the terminal buffer.
Solution: Set b_locked_split.

Co-authored-by: Sean Dewar <6256228+seandewar@users.noreply.github.com>
(cherry picked from commit ad85871ca1)
This commit is contained in:
zeertzjq
2026-01-05 19:19:19 +08:00
committed by github-actions[bot]
parent 2cc78732fc
commit ea871923eb
2 changed files with 14 additions and 0 deletions

View File

@@ -660,7 +660,10 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last, bool i
buf->b_nwindows = nwindows;
if (buf->terminal) {
buf->b_locked_split++;
buf_close_terminal(buf);
buf->b_locked_split--;
// Must check this before calling buf_freeall(), otherwise is_curbuf will be true
// in buf_freeall() but still false here, leading to a 0-line buffer.
if (buf == curbuf && !is_curbuf) {

View File

@@ -57,6 +57,17 @@ describe('autocmd TermClose', function()
assert_alive()
end)
it('TermClose switching back to terminal buffer', function()
local buf = api.nvim_get_current_buf()
api.nvim_open_term(buf, {})
command(('autocmd TermClose * buffer %d | new'):format(buf))
eq(
'TermClose Autocommands for "*": Vim(buffer):E1546: Cannot switch to a closing buffer',
pcall_err(command, 'bwipe!')
)
assert_alive()
end)
it('triggers when fast-exiting terminal job stops', function()
command('autocmd TermClose * let g:test_termclose = 23')
command('terminal')