From fa0799b1eeec0443f132498ef55b922b8d70e7c8 Mon Sep 17 00:00:00 2001 From: Vivek Menon Date: Mon, 5 Jan 2026 12:45:24 +1100 Subject: [PATCH] fix: prevent double-close errors on tcp handle When a connection is interrupted during an async write, the callback can attempt to close a handle that's already closing, causing: handle 0x... is already closing tcp.lua:161-162 correctly guards with is_closing(): if not client.tcp_handle:is_closing() then client.tcp_handle:close() end But client.lua:219,223 was missing this guard. Added the same defensive check to prevent the race condition. --- lua/claudecode/server/client.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/claudecode/server/client.lua b/lua/claudecode/server/client.lua index d159b72f..3188c67e 100644 --- a/lua/claudecode/server/client.lua +++ b/lua/claudecode/server/client.lua @@ -216,11 +216,15 @@ function M.close_client(client, code, reason) local close_frame = frame.create_close_frame(code, reason) client.tcp_handle:write(close_frame, function() client.state = "closed" - client.tcp_handle:close() + if not client.tcp_handle:is_closing() then + client.tcp_handle:close() + end end) else client.state = "closed" - client.tcp_handle:close() + if not client.tcp_handle:is_closing() then + client.tcp_handle:close() + end end client.state = "closing"