Fixed crash
This commit is contained in:
		| @@ -19,8 +19,15 @@ local function async_request(url, payload, callback) | ||||
| 	local stdout = vim.loop.new_pipe() | ||||
| 	local stderr = vim.loop.new_pipe() | ||||
| 	 | ||||
| 	-- Spawn curl process | ||||
| 	local handle = vim.loop.spawn('curl', { | ||||
| 	-- Spawn curl process with handle in broader scope | ||||
| 	local handle | ||||
| 	local function cleanup() | ||||
| 		if stdout then stdout:close() end | ||||
| 		if stderr then stderr:close() end | ||||
| 		if handle then handle:close() end | ||||
| 	end | ||||
|  | ||||
| 	handle = vim.loop.spawn('curl', { | ||||
| 		args = { | ||||
| 			'-s', | ||||
| 			'-X', 'POST', | ||||
| @@ -30,10 +37,8 @@ local function async_request(url, payload, callback) | ||||
| 		}, | ||||
| 		stdio = {nil, stdout, stderr} | ||||
| 	}, function(exit_code, signal)  -- This is the exit callback | ||||
| 		-- Close all handles | ||||
| 		stdout:close() | ||||
| 		stderr:close() | ||||
| 		handle:close() | ||||
| 		-- Clean up handles | ||||
| 		cleanup() | ||||
|  | ||||
| 		if exit_code ~= 0 then | ||||
| 			vim.schedule(function() | ||||
| @@ -54,6 +59,7 @@ local function async_request(url, payload, callback) | ||||
| 	end) | ||||
|  | ||||
| 	if not handle then | ||||
| 		cleanup() | ||||
| 		vim.schedule(function() | ||||
| 			callback(nil, "Failed to start curl") | ||||
| 		end) | ||||
| @@ -63,6 +69,7 @@ local function async_request(url, payload, callback) | ||||
| 	-- Handle stdout data | ||||
| 	stdout:read_start(function(err, chunk) | ||||
| 		if err then | ||||
| 			cleanup() | ||||
| 			vim.schedule(function() | ||||
| 				callback(nil, "Read error: " .. err) | ||||
| 			end) | ||||
| @@ -76,6 +83,7 @@ local function async_request(url, payload, callback) | ||||
| 	-- Handle stderr data | ||||
| 	stderr:read_start(function(err, chunk) | ||||
| 		if err then | ||||
| 			cleanup() | ||||
| 			vim.schedule(function() | ||||
| 				callback(nil, "Error reading stderr: " .. err) | ||||
| 			end) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user