lua-users home
lua-l archive

Re: Bug with coroutines?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Thiago L. once stated:
> 
> On 12/06/2014 17:53, Andrew Starks wrote:
> 
> >In both cases, the main Lua state is closed/ interrupted. The 
> >coroutine never receives the signal because in order for that to 
> >happen, the main Lua state would need to pass it off to that thread. 
> >Instead, it simply closes, which means that the thread is collected, 
> >but the return value is never read or processed.
> >
> >I can say with a pretty good level of certainty that it's not a bug, 
> >unless you think that Lua should pass EINTR to its coroutine, but 
> >given that it's not an OS thread, that would be weird.
> >
> >
> >-Andrew
> Well it should keep track of the active coroutine at least...
Well, the code in question, from lua.c:
	static void lstop (lua_State *L, lua_Debug *ar) {
	 (void)ar; /* unused arg. */
	 lua_sethook(L, NULL, 0, 0);
	 luaL_error(L, "interrupted!");
	}
	static void laction (int i) {
	 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
	 terminate process (default action) */
	 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT,1);
	}
	/* ... */
	static int docall (lua_State *L, int narg, int clear) {
	 int status;
	 int base = lua_gettop(L) - narg; /* function index */
	 lua_pushcfunction(L, traceback); /* push traceback function */
	 lua_insert(L, base); /* put it under chunk and args */
	 signal(SIGINT, laction);
	 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
	 signal(SIGINT, SIG_DFL);
	 lua_remove(L, base); /* remove traceback function */
	 /* force a complete garbage collection in case of errors */
	 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
	 return status;
	}
	/* ... */
 To me, it looks like a wierd interaction between lua_pcall() and
coroutines. When I run the test code, ^C once does nothing; ^C twice takes
me back to the Unix prompt. 
 I'm also surprised that lua_sethook() is thread safe. Frankly, I've been
bitten by calling C functions from signal handlers (one of the toughest bugs
I've had to track down was due to calling functions from a signal handler)
so I'm surprised this works. 
 -spc

AltStyle によって変換されたページ (->オリジナル) /