Skip to content

Commit

Permalink
remove gettop() and assert, add a test case that create a socket hand…
Browse files Browse the repository at this point in the history
…le in fiber.
  • Loading branch information
AndrewTsao committed Aug 28, 2012
1 parent 93ab775 commit f36c77e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/utils.c
Expand Up @@ -180,7 +180,6 @@ uv_loop_t* luv_get_loop(lua_State *L) {

/* Initialize a new lhandle and push the new userdata on the stack. */
luv_handle_t* luv_handle_create(lua_State* L, size_t size, const char* type) {
int before = lua_gettop(L);
lua_State* mainthread;
/* Create the userdata and set it's metatable */
luv_handle_t* lhandle = (luv_handle_t*)lua_newuserdata(L, sizeof(luv_handle_t));
Expand Down Expand Up @@ -209,7 +208,6 @@ luv_handle_t* luv_handle_create(lua_State* L, size_t size, const char* type) {
}
lhandle->ref = LUA_NOREF;
lhandle->type = type;
assert(before + 1 == lua_gettop(L));
return lhandle;
}

Expand Down
8 changes: 7 additions & 1 deletion src/utils.h
Expand Up @@ -59,7 +59,13 @@ typedef struct {
uv_handle_t* handle; /* The actual uv handle. memory managed by luv */
int refCount; /* a count of all pending request to know strength */
lua_State* L; /* L and ref together form a reference to the userdata */
int threadref; /* L is always `mainthread, threadref in `mainthread */
int threadref; /* if handle is created in a coroutine(not main thread), threadref is
the reference to the coroutine in the Lua registery.
we release the reference when handle closed.
if handle is created in the main thread, threadref is LUA_NOREF.
we must hold the coroutine, because in some cases(see issue #319) that the coroutine
referenced by nothing and would collected by gc, then uv's callback touch an
invalid pointer. */
int ref; /* ref is null when refCount is 0 meaning we're weak */
const char* type;
} luv_handle_t;
Expand Down
49 changes: 49 additions & 0 deletions tests/test-fiber-bug319.lua
Expand Up @@ -28,8 +28,57 @@ fiber.new(function(wrap, wait)
end)
end
end)
collectgarbage()
local net = require('net')
local PORT = process.env.PORT or 10082
local messages = {'a','b','c'}
fiber.new(function(wrap, wait)
local server
server = net.createServer(function(client)
client:on("data", function(data)
fiber.new(function(wrap, wait)
for _, message in ipairs(messages) do
wait(function(resume)
client:write(message, function(err)
timer.setTimeout(1, resume)
end)
end)
end
client:destroy()
server:close()
end)
end)
end)
server:listen(PORT, "127.0.0.1")
server:on("error", function(err)
assert(false)
end)
wait(function(resume) process.nextTick(resume) end)
end)
collectgarbage()
local client = require("uv").Tcp:new()
client:connect("127.0.0.1", PORT)
client:on("connect", function()
client:write("hi")
local received = {}
client:on("data", function(data)
received[#received+1] = data
if data == "3:c" then
client:close()
assert(received[1] == messages[1])
assert(received[2] == messages[2])
assert(received[3] == messages[3])
end
end)
end)
client:on("error", function()
assert(false)
end)
process:on('exit', function()
assert(count == 10)
end)

0 comments on commit f36c77e

Please sign in to comment.