fix: stop timer for retry on canceling inflight request (#2637)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
brook hong
2025-08-24 16:31:57 +08:00
committed by GitHub
parent 8a2ed691c0
commit 2791db9984

View File

@@ -747,6 +747,16 @@ function M.curl(opts)
return active_job return active_job
end end
local retry_timer = nil
local function stop_retry_timer(is_abort)
if retry_timer then
retry_timer:stop()
pcall(function() retry_timer:close() end)
retry_timer = nil
if is_abort then Utils.info("Retry aborted due to user requested cancellation.") end
end
end
---@param opts AvanteLLMStreamOptions ---@param opts AvanteLLMStreamOptions
function M._stream(opts) function M._stream(opts)
-- Reset the cancellation flag at the start of a new request -- Reset the cancellation flag at the start of a new request
@@ -982,8 +992,6 @@ function M._stream(opts)
} }
) )
local timer = vim.loop.new_timer()
if timer then
local retry_count = stop_opts.retry_after local retry_count = stop_opts.retry_after
Utils.info("Rate limit reached. Retrying in " .. retry_count .. " seconds", { title = "Avante" }) Utils.info("Rate limit reached. Retrying in " .. retry_count .. " seconds", { title = "Avante" })
@@ -1001,8 +1009,7 @@ function M._stream(opts)
end end
if retry_count <= 0 then if retry_count <= 0 then
timer:stop() stop_retry_timer(false)
pcall(function() timer:close() end)
Utils.info("Restarting stream after rate limit pause") Utils.info("Restarting stream after rate limit pause")
M._stream(opts) M._stream(opts)
@@ -1011,8 +1018,9 @@ function M._stream(opts)
end end
end end
timer:start(0, 1000, vim.schedule_wrap(function() countdown() end)) stop_retry_timer(false)
end retry_timer = uv.new_timer()
if retry_timer then retry_timer:start(0, 1000, vim.schedule_wrap(function() countdown() end)) end
return return
end end
return opts.on_stop(stop_opts) return opts.on_stop(stop_opts)
@@ -1173,6 +1181,7 @@ function M.cancel_inflight_request()
LLMToolHelpers.confirm_popup:cancel() LLMToolHelpers.confirm_popup:cancel()
LLMToolHelpers.confirm_popup = nil LLMToolHelpers.confirm_popup = nil
end end
stop_retry_timer(true)
api.nvim_exec_autocmds("User", { pattern = M.CANCEL_PATTERN }) api.nvim_exec_autocmds("User", { pattern = M.CANCEL_PATTERN })
end end