From 67b055009db0f56f33e4cd0b7334bb7484dc35be Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 20 Mar 2017 16:25:15 +0800 Subject: [PATCH] bugfix isse #592 --- service/debug_agent.lua | 14 +++++++++++--- service/debug_console.lua | 22 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/service/debug_agent.lua b/service/debug_agent.lua index b4c4667d..451e2215 100644 --- a/service/debug_agent.lua +++ b/service/debug_agent.lua @@ -10,9 +10,13 @@ function CMD.start(address, fd) skynet.error(string.format("Attach to :%08x", address)) local handle channel, handle = debugchannel.create() - skynet.call(address, "debug", "REMOTEDEBUG", fd, handle) - -- todo hook - skynet.ret(skynet.pack(nil)) + local ok, err = pcall(skynet.call, address, "debug", "REMOTEDEBUG", fd, handle) + if not ok then + skynet.ret(skynet.pack(false, "Debugger attach failed")) + else + -- todo hook + skynet.ret(skynet.pack(true)) + end skynet.exit() end @@ -20,6 +24,10 @@ function CMD.cmd(cmdline) channel:write(cmdline) end +function CMD.ping() + skynet.ret() +end + skynet.start(function() skynet.dispatch("lua", function(_,_,cmd,...) local f = CMD[cmd] diff --git a/service/debug_console.lua b/service/debug_console.lua index d76a1e58..2db38e46 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -91,7 +91,7 @@ end local function console_main_loop(stdin, print) print("Welcome to skynet console") skynet.error(stdin, "connected") - pcall(function() + local ok, err = pcall(function() while true do local cmdline = socket.readline(stdin, "\n") if not cmdline then @@ -109,6 +109,9 @@ local function console_main_loop(stdin, print) end end end) + if not ok then + skynet.error(stdin, err) + end skynet.error(stdin, "disconnected") socket.close(stdin) end @@ -262,8 +265,11 @@ function COMMANDX.debug(cmd) local address = adjust_address(cmd[2]) local agent = skynet.newservice "debug_agent" local stop - skynet.fork(function() + local term_co = coroutine.running() + local function forward_cmd() repeat + -- notice : It's a bad practice to call socket.readline from two threads (this one and console_main_loop), be careful. + skynet.call(agent, "lua", "cmd", "ping") -- detect agent alive, if agent exit, raise error local cmdline = socket.readline(cmd.fd, "\n") cmdline = cmdline and cmdline:gsub("(.*)\r$", "%1") if not cmdline then @@ -272,9 +278,19 @@ function COMMANDX.debug(cmd) end skynet.send(agent, "lua", "cmd", cmdline) until stop or cmdline == "cont" + end + skynet.fork(function() + pcall(forward_cmd) + skynet.wakeup(term_co) end) - skynet.call(agent, "lua", "start", address, cmd.fd) + local ok, err = skynet.call(agent, "lua", "start", address, cmd.fd) stop = true + -- wait for fork coroutine exit. + skynet.wait(term_co) + + if not ok then + error(err) + end end function COMMAND.logon(address)