From 1906da52dbc9876046ec9866a5aae25309d7587e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 1 Feb 2026 21:29:19 +0800 Subject: fix(lua): close vim.defer_fn() timer if vim.schedule() failed (#37647) Problem: Using vim.defer_fn() just before Nvim exit leaks luv handles. Solution: Make vim.schedule() return an error message if scheduling failed. Make vim.defer_fn() close timer if vim.schedule() failed. --- src/nvim/lua/executor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 78d4515057..b76244cc2d 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -407,17 +407,20 @@ static int nlua_schedule(lua_State *const lstate) return lua_error(lstate); } + lua_pushnil(lstate); // If main_loop is closing don't schedule tasks to run in the future, // otherwise any refs allocated here will not be cleaned up. if (main_loop.closing) { - return 0; + lua_pushliteral(lstate, "main loop is closing"); + return 2; } LuaRef cb = nlua_ref_global(lstate, 1); // Pass along UI event handler to disable on error. multiqueue_put(main_loop.events, nlua_schedule_event, (void *)(ptrdiff_t)cb, (void *)(ptrdiff_t)ui_event_ns_id); - return 0; + lua_pushnil(lstate); + return 2; } // Dummy timer callback. Used by f_wait(). -- cgit