From 4b5fbab12941b5bc550636312e06ceecdfa78c72 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 7 Jun 2018 17:27:21 +0800 Subject: [PATCH] recycle coroutine bugfix --- lualib/skynet.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a9a2b5c2..5208c5f8 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -134,8 +134,6 @@ local function co_create(f) skynet.address(session_coroutine_address[co]), source.source, source.linedefined)) end - f = nil - coroutine_pool[#coroutine_pool+1] = co -- coroutine exit local tag = session_coroutine_tracetag[co] if tag then @@ -148,12 +146,19 @@ local function co_create(f) session_coroutine_id[co] = nil end + -- recycle co into pool + f = nil + coroutine_pool[#coroutine_pool+1] = co + -- recv new main function f f = coroutine_yield "SUSPEND" f(coroutine_yield()) end end) else + -- pass the main function f to coroutine, and restore running thread + local running = running_thread coroutine_resume(co, f) + running_thread = running end return co end