From f9dc6a375688eb3c70c5368a0160e1ac98353ea7 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 7 Apr 2021 03:25:55 +0000 Subject: [PATCH] fix SOCKET_CLOSE event after socket.close() See #1373 --- lualib/skynet/socket.lua | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lualib/skynet/socket.lua b/lualib/skynet/socket.lua index e99d3ca5..b547eb34 100644 --- a/lualib/skynet/socket.lua +++ b/lualib/skynet/socket.lua @@ -16,6 +16,7 @@ local socket_pool = setmetatable( -- store all socket object } ) +local socket_onclose = {} local socket_message = {} local function wakeup(s) @@ -117,14 +118,16 @@ end -- SKYNET_SOCKET_TYPE_CLOSE = 3 socket_message[3] = function(id) local s = socket_pool[id] - if s == nil then - driver.close(id) - return + if s then + s.connected = false + wakeup(s) + else + driver.shutdown(id) end - s.connected = false - wakeup(s) - if s.on_close then - s.on_close(id) + local cb = socket_onclose[id] + if cb then + cb(id) + socket_onclose[id] = nil end end @@ -216,6 +219,7 @@ local function connect(id, func) callback = func, protocol = "TCP", } + assert(not socket_onclose[id], "socket has onclose callback") assert(not socket_pool[id], "socket is not closed") socket_pool[id] = s suspend(s) @@ -409,6 +413,7 @@ function socket.abandon(id) if s then s.connected = false wakeup(s) + socket_onclose[id] = nil socket_pool[id] = nil end end @@ -460,9 +465,7 @@ function socket.warning(id, callback) end function socket.onclose(id, callback) - local obj = socket_pool[id] - assert(obj) - obj.on_close = callback + socket_onclose[id] = callback end return socket