Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix crash in loop_gc #654

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ static int luv_is_closing(lua_State* L) {
return 1;
}

static void luv_handle_free(uv_handle_t* handle);

static void luv_close_cb(uv_handle_t* handle) {
lua_State* L;
luv_handle_t* data = (luv_handle_t*)handle->data;
if (!data) return;
L = data->ctx->L;
luv_call_callback(L, data, LUV_CLOSED, 0);
luv_unref_handle(L, data);
if(data->ref > 0) {
luv_call_callback(L, data, LUV_CLOSED, 0);
luv_unref_handle(L, data);
} else {
luv_handle_free(handle);
}
}

static int luv_close(lua_State* L) {
Expand Down Expand Up @@ -127,19 +133,24 @@ static void luv_gc_cb(uv_handle_t* handle) {
static int luv_handle_gc(lua_State* L) {
uv_handle_t** udata = (uv_handle_t**)lua_touserdata(L, 1);
uv_handle_t* handle = *udata;
luv_handle_t* data = (luv_handle_t*)handle->data;

// Only cleanup if the handle hasn't been cleaned up yet.
if (handle) {
if (data->ref == LUA_NOREF) {
if (!uv_is_closing(handle)) {
// If the handle is not closed yet, close it first before freeing memory.
uv_close(handle, luv_gc_cb);
uv_close(handle, luv_handle_free);
}
else {
// Otherwise, free the memory right away.
luv_handle_free(handle);
}
// Mark as cleaned up by wiping the dangling pointer.
*udata = NULL;
} else {
// os.exit maybe cause gc before close_cb
// use LUA_REFNIL to tell close_cb to free memory.
data->ref = LUA_REFNIL;
}

return 0;
Expand Down
1 change: 1 addition & 0 deletions src/lhandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ static void luv_call_callback(lua_State* L, luv_handle_t* data, luv_callback_id

static void luv_unref_handle(lua_State* L, luv_handle_t* data) {
luaL_unref(L, LUA_REGISTRYINDEX, data->ref);
data->ref = LUA_NOREF;
luaL_unref(L, LUA_REGISTRYINDEX, data->callbacks[0]);
luaL_unref(L, LUA_REGISTRYINDEX, data->callbacks[1]);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/manual-test-exit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--come from https://github.com/luvit/luv/issues/599

-- run `lua manual-test-exit.lua || echo $?`
-- it shoud print `5`

local uv = require('luv')

local function setTimeout(callback, ms)
local timer = uv.new_timer()
timer:start(ms, 0, function()
timer:stop()
timer:close()
callback()
end)
return timer
end

setTimeout(function()
os.exit(5, true)
end, 1000)

uv.run()
4 changes: 4 additions & 0 deletions tests/manual-test-without-uv.run.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local uv = require('luv')

local test = uv.new_pipe(false)
uv.close(test)
57 changes: 57 additions & 0 deletions tests/test-loop.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
return require('lib/tap')(function (test)

local function get_cmd()
local i=1
repeat
i=i-1
until not arg[i]
return arg[i+1]
end

local cmd = get_cmd()
local cwd = require('luv').cwd()
print("cmd: ", cmd)
print("cwd: ", cwd)

test("uv.loop_mode", function (print, p, expect, uv)
assert(uv.loop_mode() == nil)
local timer = uv.new_timer()
Expand All @@ -10,4 +23,48 @@ return require('lib/tap')(function (test)
end))
end)

test("issue #437, crash without uv.run", function (print, p, expect, uv)
local handle
local stdout = uv.new_pipe(false)

handle = uv.spawn(cmd, {
args = { "tests/manual-test-without-uv.run.lua" },
cwd = cwd,
stdio = {nil, stdout},
},
expect(function(status, signal)
print('#437', status, signal)
assert(status==0)
assert(signal==0)
handle:close()
end))

uv.read_start(stdout, expect(function (err, chunk)
p("stdout", {err=err,chunk=chunk})
uv.close(stdout)
end))
end)

test("issue #599, crash during calling os.exit", function (print, p, expect, uv)
local handle
local stdout = uv.new_pipe(false)

handle = uv.spawn(cmd, {
args = { "tests/manual-test-exit.lua" },
cwd = cwd,
stdio = {nil, stdout},
},
expect(function(status, signal)
print('#599', status, signal)
assert(status==5)
assert(signal==0)
handle:close()
end))

uv.read_start(stdout, expect(function (err, chunk)
p("stdout", {err=err,chunk=chunk})
uv.close(stdout)
end))
end)

end)