Skip to content

Commit

Permalink
bugfix isse cloudwu#592
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Mar 20, 2017
1 parent 80d9ec3 commit 67b0550
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
14 changes: 11 additions & 3 deletions service/debug_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ 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

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]
Expand Down
22 changes: 19 additions & 3 deletions service/debug_console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 67b0550

Please sign in to comment.