Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
luvit: uv: Handle: avoid double close on handles
libuv will assert if you close a handle twice. This is an easy mistake
to make but telling the user about it is tricky. Throw an error to avoid
the libuv assert.
  • Loading branch information
Brandon Philips committed Aug 8, 2012
1 parent 574d995 commit 53e5ad5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/luvit/uv.lua
Expand Up @@ -23,7 +23,14 @@ uv.Handle = Handle

-- Wrapper around `uv_close`. Closes the underlying file descriptor of a handle.
-- Handle:close()
Handle.close = native.close
Handle.close = function(self)
if self._closed then
error("close called on closed handle")
return
end
native.close(self)
self._closed = true
end

--[[
This is used by Emitters to register with native events when the first listener
Expand Down Expand Up @@ -253,7 +260,6 @@ uv.Timer = Timer
function Timer:initialize()
self.userdata = native.newTimer()
self._active = false
self._closed = false
end

function Timer:_update()
Expand All @@ -267,13 +273,6 @@ function Timer:start(timeout, interval, callback)
self:_update()
end

function Timer:close()
if not self._closed then
Handle.close(self)
end
self._closed = true
end

-- Timer:stop()
function Timer:stop()
native.timerStop(self)
Expand Down
3 changes: 3 additions & 0 deletions tests/test-net.lua
Expand Up @@ -46,6 +46,9 @@ server:listen(PORT, HOST, function(err)
assert(data == 'hello')
client:destroy()
server:close()
-- Ensure double close doesn't assert and returns an error
local success, err = pcall(server:close())
assert(err)
end)
client:write('hello')
Expand Down

0 comments on commit 53e5ad5

Please sign in to comment.