Skip to content

Commit

Permalink
fixed #319
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewTsao committed Aug 25, 2012
1 parent 6727d4f commit 93ab775
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/utils.c
Expand Up @@ -180,7 +180,8 @@ 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 All @@ -197,8 +198,18 @@ luv_handle_t* luv_handle_create(lua_State* L, size_t size, const char* type) {
lhandle->handle->data = lhandle; /* Point back to lhandle from handle */
lhandle->refCount = 0;
lhandle->L = L;

/* if handle create in a coroutine, we need hold the coroutine */
mainthread = luv_get_main_thread(L);
if (L != mainthread) {
lua_pushthread(L);
lhandle->threadref = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
lhandle->threadref = LUA_NOREF;
}
lhandle->ref = LUA_NOREF;
lhandle->type = type;
assert(before + 1 == lua_gettop(L));
return lhandle;
}

Expand Down Expand Up @@ -243,6 +254,10 @@ void luv_handle_unref(lua_State* L, luv_handle_t* lhandle) {
/* If it's now inactive, clear the ref */
if (!lhandle->refCount) {
luaL_unref(L, LUA_REGISTRYINDEX, lhandle->ref);
if (lhandle->threadref != LUA_NOREF) {
luaL_unref(L, LUA_REGISTRYINDEX, lhandle->threadref);
lhandle->threadref = LUA_NOREF;
}
lhandle->ref = LUA_NOREF;
/* printf("makeWeak\t%s lhandle=%p handle=%p\n", lhandle->type, lhandle, lhandle->handle);*/
}
Expand Down Expand Up @@ -271,3 +286,11 @@ ares_channel luv_get_ares_channel(lua_State *L) {
lua_pop(L, 1);
return channel;
}

lua_State* luv_get_main_thread(lua_State *L) {
lua_State *main_thread;
lua_getfield(L, LUA_REGISTRYINDEX, "main_thread");
main_thread = lua_tothread(L, -1);
lua_pop(L, 1);
return main_thread;
}
2 changes: 2 additions & 0 deletions src/utils.h
Expand Up @@ -38,6 +38,7 @@ uv_loop_t* luv_get_loop(lua_State *L);

void luv_set_ares_channel(lua_State *L, ares_channel channel);
ares_channel luv_get_ares_channel(lua_State *L);
lua_State* luv_get_main_thread(lua_State *L);


void luv_push_async_error(lua_State* L, uv_err_t err, const char* source, const char* path);
Expand All @@ -58,6 +59,7 @@ 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 ref; /* ref is null when refCount is 0 meaning we're weak */
const char* type;
} luv_handle_t;
Expand Down
35 changes: 35 additions & 0 deletions tests/test-fiber-bug319.lua
@@ -0,0 +1,35 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
require("helper")
local fiber = require("fiber")
local timer = require("timer")
local count = 0
fiber.new(function(wrap, wait)
for i = 1, 10 do
timer.setTimeout(100, function()
count = count + 1
end)
end
end)
collectgarbage()
process:on('exit', function()
assert(count == 10)
end)

0 comments on commit 93ab775

Please sign in to comment.