commit ad85871ca133580f3e014ef37b9b2dbd46f90d9c
parent 7297e9d339c64f2f2c57cb2cc57455c161ef78e5
Author: zeertzjq <zeertzjq@outlook.com>
Date: Mon, 5 Jan 2026 19:19:19 +0800
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>
Diffstat:
2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
@@ -672,7 +672,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
@@ -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')