to-be-closed (#1261)

This commit is contained in:
hong
2020-12-01 11:51:35 +08:00
committed by GitHub
parent 5248d443dc
commit 693b962b43
2 changed files with 63 additions and 1 deletions

View File

@@ -14,12 +14,20 @@ local tunpack = table.unpack
local traceback = debug.traceback
local cresume = coroutine.resume
local coroutine_close = coroutine.close
local running_thread = nil
local init_thread = nil
local function coroutine_ret(co, ok, ...)
if not ok then
coroutine_close(co)
end
return ok, ...
end
local function coroutine_resume(co, ...)
running_thread = co
return cresume(co, ...)
return coroutine_ret(co, cresume(co, ...))
end
local coroutine_yield = coroutine.yield
local coroutine_create = coroutine.create
@@ -329,6 +337,7 @@ function suspend(co, result, command)
if command == "SUSPEND" then
return dispatch_wakeup()
elseif command == "QUIT" then
coroutine_close(co)
-- service exit
return
elseif command == "USER" then
@@ -523,6 +532,11 @@ function skynet.exit()
c.send(address, skynet.PTYPE_ERROR, session, "")
end
end
for session, co in pairs(session_id_coroutine) do
if type(co) == "thread" then
coroutine_close(co)
end
end
for resp in pairs(unresponse) do
resp(false)
end

48
test/testtobeclosed.lua Normal file
View File

@@ -0,0 +1,48 @@
local skynet = require "skynet"
local function new_test(name)
return setmetatable({}, { __close = function(...)
skynet.error(...)
end, __name = "closemeta:" .. name})
end
local i = 0
skynet.dispatch("lua", function()
i = i + 1
if i==2 then
local c<close> = new_test("dispatch_error")
error("dispatch_error")
else
local c<close> = new_test("dispatch_wait")
skynet.wait()
end
end)
skynet.start(function()
local c<close> = new_test("skynet.exit")
skynet.fork(function()
local a<close> = new_test("stack_raise_error")
error("raise error")
end)
skynet.fork(function()
local a<close> = new_test("session_id_coroutine_wait")
skynet.wait()
end)
skynet.fork(function()
local a<close> = new_test("session_id_coroutine_call")
skynet.call(skynet.self(), "lua")
end)
skynet.fork(function()
skynet.call(skynet.self(), "lua")
end)
skynet.sleep(100)
skynet.fork(function()
local a<close> = new_test("no_running")
skynet.wait()
end)
skynet.exit()
end)
--[[
testtobeclosed
]]