Skip to content

Commit

Permalink
Fix leak when an async fs_scandir request has an error
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Mar 2, 2024
1 parent ec6ecf5 commit 4b80697
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ static int push_fs_result(lua_State* L, uv_fs_t* req) {
}

if (req->result < 0) {
if (req->fs_type == UV_FS_SCANDIR) {
// We need to unref the luv_fs_scandir_t userdata to allow it to be garbage collected.
// The scandir callback can only be called once, so we now know that the
// req can be safely garbage collected.
luaL_unref(L, LUA_REGISTRYINDEX, data->data_ref);
data->data_ref = LUA_NOREF;
}
lua_pushnil(L);
if (fs_req_has_dest_path(req)) {
lua_rawgeti(L, LUA_REGISTRYINDEX, data->data_ref);
Expand Down
9 changes: 9 additions & 0 deletions tests/manual-test-leaks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ return require('lib/tap')(function (test)
end)
end)

test("fs-scandir-async error", function (print, p, expect, uv)
bench(uv, p, 0x10000, function ()
local outer_req = assert(uv.fs_scandir('intentionally missing folder this should not exist!', function(err, req)
assert(err)
assert(not req)
end))
end)
end)

test("lots-o-timers", function (print, p, expect, uv)
bench(uv, p, 0x100000, function ()
local timer = uv.new_timer()
Expand Down

0 comments on commit 4b80697

Please sign in to comment.