Skip to content

Commit

Permalink
disallow :close when invoked from a thread resumed by cqueue_step (se…
Browse files Browse the repository at this point in the history
…e issue #175)
  • Loading branch information
wahern committed Dec 15, 2016
1 parent a515ed2 commit 96bc2f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
15 changes: 15 additions & 0 deletions regress/175-cq-close-segfault.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
_=[[
. "${0%%/*}/regress.sh"
exec runlua "$0" "$@"
]]
require"regress".export".*"

local cq = cqueues.new()
local ok, why = cq:wrap(function ()
cq:close()
end):loop()
check(not ok, "expected loop to fail")
check(tostring(why):find"cqueue running", "unexpected error (%s)", tostring(why))

say"OK"
19 changes: 16 additions & 3 deletions src/cqueues.c
Original file line number Diff line number Diff line change
Expand Up @@ -1489,12 +1489,25 @@ static int cqueue_new(lua_State *L) {
} /* cqueue_new() */


static int cqueue__gc(lua_State *L) {
struct callinfo I;
static int cqueue_close(lua_State *L) {
struct cqueue *Q = cqs_checkudata(L, 1, 1, CQUEUE_CLASS);
struct callinfo I;

/* disallow :close when invoked from a thread resumed by cqueue_step */
luaL_argcheck(L, !Q->cstack || !cstack_isrunning(Q->cstack, Q), 1, "cqueue running");

cqueue_enter_nothrow(L, &I, 1, Q);
cqueue_destroy(L, Q, &I);

return 0;
} /* cqueue_close() */


static int cqueue__gc(lua_State *L) {
struct cqueue *Q = cqs_checkudata(L, 1, 1, CQUEUE_CLASS);
struct callinfo I;

cqueue_enter_nothrow(L, &I, 1, Q);
cqueue_destroy(L, Q, &I);

return 0;
Expand Down Expand Up @@ -2872,7 +2885,7 @@ static const luaL_Reg cqueue_methods[] = {
{ "pollfd", &cqueue_pollfd },
{ "events", &cqueue_events },
{ "timeout", &cqueue_timeout },
{ "close", &cqueue__gc },
{ "close", &cqueue_close },
{ NULL, NULL }
}; /* cqueue_methods[] */

Expand Down

0 comments on commit 96bc2f2

Please sign in to comment.