调用vim.loop函数触发uv__check_before_write断言失败问题
问题分析与修复
错误原因
触发uv__check_before_write: Assertion 'nbufs > 0' failed的直接原因是:当被调用的进程没有产生任何标准错误输出时,output数组为空,此时调用log_pipe:write(output)会向libuv传递一个空的缓冲区列表,违反了nbufs > 0的断言要求。
修复方案
在写入日志前先检查output是否为空,避免传入空缓冲区。同时代码里的env、logfile属于未定义变量,需要确保这些变量已提前正确初始化,否则会引发其他运行错误。
方案一:处理空缓冲区后写入
修改后的关键代码片段:
function(code) stderr:read_stop() stderr:close() local log = uv.fs_open(logfile, "a+", 0x1A4) local log_pipe = uv.new_pipe(false) log_pipe:open(log) -- 为空output设置默认内容,避免传入空缓冲区 local write_content = #output > 0 and output or {""} log_pipe:write(write_content, vim.schedule_wrap(function(err) if err then vim.notify(string.format(" Paq: Failed to spawn %s (%s)", process, pid)) end log_pipe:close() log:close() handle:close() cb(code == 0) end)) end
方案二:空缓冲区时直接跳过写入
如果不需要写入空内容,可选择直接跳过写入步骤:
function(code) stderr:read_stop() stderr:close() local log = uv.fs_open(logfile, "a+", 0x1A4) local log_pipe = uv.new_pipe(false) log_pipe:open(log) -- 提取收尾逻辑为单独函数,避免重复代码 local function finish() log_pipe:close() log:close() handle:close() cb(code == 0) end -- 无输出时直接收尾 if #output == 0 then finish() return end log_pipe:write(output, vim.schedule_wrap(function(err) if err then vim.notify(string.format(" Paq: Failed to spawn %s (%s)", process, pid)) end finish() end)) end
内容的提问来源于stack exchange,提问作者wviana
相关产品推荐
相关产品推荐

