fix(terminal): memory leak in pending TermRequest StringBuilder #39333

Problem: Destroying a terminal with pending `TermRequest` events leaks
memory.

Solution: Make `emit_termrequest` the sole owner of its `pending_send`
allocation.
This commit is contained in:
Barrett Ruth
2026-04-23 06:31:24 -04:00
committed by GitHub
parent a4ad469fb1
commit 19ef632dec
2 changed files with 15 additions and 1 deletions

View File

@@ -247,6 +247,8 @@ static void emit_termrequest(void **argv)
buf_T *buf = handle_get_buffer(buf_handle);
if (!buf || buf->terminal == NULL) { // Terminal already closed.
xfree(sequence);
kv_destroy(*pending_send);
xfree(pending_send);
return;
}
Terminal *term = buf->terminal;
@@ -1232,7 +1234,6 @@ void terminal_destroy(Terminal **termpp)
kv_destroy(term->selection);
kv_destroy(term->termrequest_buffer);
vterm_free(term->vt);
xfree(term->pending.send);
multiqueue_free(term->pending.events);
xfree(term);
*termpp = NULL; // coverity[dead-store]

View File

@@ -92,4 +92,17 @@ describe(':terminal', function()
exec_lua([[return _G.osc10_response]])
)
end)
it('does not leak pending TermRequest on buffer destroy #39332', function()
-- Send all OSC sequences in one exec_lua so that the event loop does not drain between the sends.
exec_lua(function(prefix, st)
local buf = vim.api.nvim_create_buf(false, true)
local chan = vim.api.nvim_open_term(buf, {})
vim.api.nvim_create_autocmd('TermRequest', { buffer = buf, callback = function() end })
vim.api.nvim_chan_send(chan, prefix .. '7;file:///a' .. st)
vim.api.nvim_chan_send(chan, prefix .. '7;file:///b' .. st)
vim.api.nvim_buf_delete(buf, { force = true })
end, OSC_PREFIX, ST)
assert_alive()
end)
end)