From ea871923ebbe1cc943f78fae275f7f5d38f272af Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 5 Jan 2026 19:19:19 +0800 Subject: [PATCH] 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 ad85871ca133580f3e014ef37b9b2dbd46f90d9c) --- src/nvim/buffer.c | 3 +++ test/functional/autocmd/termxx_spec.lua | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 3623eeb846..c44166a868 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -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) { diff --git a/test/functional/autocmd/termxx_spec.lua b/test/functional/autocmd/termxx_spec.lua index e06e632482..4a24db1bbd 100644 --- a/test/functional/autocmd/termxx_spec.lua +++ b/test/functional/autocmd/termxx_spec.lua @@ -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')