gpt4 book ai didi

c - 协程恢复上的 Lua 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:18 25 4
gpt4 key购买 nike

我有代码

fun = function() 
coroutine.resume(co); -- here it segfaults
return true;
end ;

-- evtimer is my binding to libev
-- timer = evtimer.new(1,1, fun); -- this works ok and prints "co N" every second


co = coroutine.create(function ()
timer = evtimer.new(1,1, fun); --this segfaults


for i=1,100000 do
print("co", i)
coroutine.yield(111);
end
end)
-- timer = evtimer.new(1,1, fun); -- this works too

coroutine.resume(co);

timer:start(); --start timer
-- here main thread ends and libev event loop starts. Timer callback is called by event loop

当计时器在协程内创建时的变体在 lua 核心内恢复时出现段错误。回溯:

Program received signal SIGSEGV, Segmentation fault.
0x000000000066ec00 in ?? ()
(gdb) backtrace
#0 0x000000000066ec00 in ?? ()
#1 0x0000000000413430 in resume (L=0x6841a0, ud=0x6d8ac0) at ./src/myprogram/lua/ldo.c:522
#2 0x00000000004120e9 in luaD_rawrunprotected (L=0x6841a0, f=0x4132da <resume>, ud=0x6d8ac0)
at ./src/myprogram/lua/ldo.c:131
#3 0x00000000004134ef in lua_resume (L=0x6841a0, from=0x6841a0, nargs=0)
at ./src/myprogram/lua/ldo.c:543
#4 0x0000000000429b4e in auxresume (L=0x6841a0, co=0x6841a0, narg=0)
at ./src/myprogram/lua/lcorolib.c:31
#5 0x0000000000429c5a in luaB_coresume (L=0x6841a0) at ./src/myprogram/lua/lcorolib.c:53
#6 0x0000000000412b19 in luaD_precall (L=0x6841a0, func=0x6d8aa0, nresults=2)
at ./src/myprogram/lua/ldo.c:319
#7 0x0000000000424b2b in luaV_execute (L=0x6841a0) at ./src/myprogram/lua/lvm.c:709
#8 0x0000000000412f8a in luaD_call (L=0x6841a0, func=0x6d8a70, nResults=1, allowyield=0)
at ./src/myprogram/lua/ldo.c:402
#9 0x000000000040c77a in f_call (L=0x6841a0, ud=0x7fffffffe000) at ./src/myprogram/lua/lapi.c:923
#10 0x00000000004120e9 in luaD_rawrunprotected (L=0x6841a0, f=0x40c73e <f_call>, ud=0x7fffffffe000)
at ./src/myprogram/lua/ldo.c:131
#11 0x0000000000413722 in luaD_pcall (L=0x6841a0, func=0x40c73e <f_call>, u=0x7fffffffe000,
old_top=112, ef=0) at ./src/myprogram/lua/ldo.c:603
#12 0x000000000040c84a in lua_pcallk (L=0x6841a0, nargs=0, nresults=1, errfunc=0, ctx=0, k=0x0)
at ./src/myprogram/lua/lapi.c:949
#13 0x0000000000433a54 in lua_script_pcall (ls=0x6841a0, nargs=0, nresults=1)
at ./src/myprogram//lua_script.c:41
#14 0x00000000004353b0 in lua_evtimer_callback (loop=0x66cc20 <default_loop_struct>, w=0x684938,
revents=256) at ./src/myprogram//evtimer_lualib.c:21
#15 0x0000000000406d41 in ev_invoke_pending (loop=0x66cc20 <default_loop_struct>)
at ./src/myprogram/libev/ev.c:2994
#16 0x000000000040763b in ev_run (loop=0x66cc20 <default_loop_struct>, flags=0)
at ./src/myprogram/libev/ev.c:3394

它在第 522(lua 5.2) 行出现段错误 n = (ci->u.c.k)(L);/ 继续调用 */

有没有人熟悉lua core?为什么会这样?

最佳答案

问题是我将当前 Lua_State 保存在 evtimer.new 中,并在调用 evtimer.new 的线程上下文中调用定时器回调(使用 pcall)。在这种情况下,当计时器尝试调用回调时线程没有恢复,这导致 lua 内部状态损坏我通过在 C 端使用 lua_resume 来修复它来恢复线程而不是调用 lua 回调

关于c - 协程恢复上的 Lua 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com