Skip to content

Commit

Permalink
luvit: uv: protect spawn
Browse files Browse the repository at this point in the history
I don't have a test case for this but I think we need this safety.
  • Loading branch information
Brandon Philips committed Aug 11, 2012
1 parent 9ea91c5 commit bbdeedf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/luvit/uv.lua
Expand Up @@ -354,7 +354,16 @@ function Process:initialize(command, args, options)
args = args or {}
options = options or {}

self.userdata = native.spawn(self.stdin, self.stdout, self.stderr, command, args, options)
local success, result = pcall(native.spawn, self.stdin, self.stdout, self.stderr, command, args, options)

if success then
self.userdata = result
else
self.stdin:close()
self.stderr:close()
self.stdout:close()
error(result)
end

self.stdout:readStart()
self.stderr:readStart()
Expand Down
13 changes: 11 additions & 2 deletions tests/test-process.lua
Expand Up @@ -4,7 +4,7 @@ local os = require('os')

local environmentTestResult = false

function test()
function test_process_env()
local options = {
env = { TEST1 = 1 }
}
Expand All @@ -22,7 +22,16 @@ function test()
end)
end

test()
function test_process_fail()
child = spawn('this_executable_will_never_exist_i_hope', {})
p(child)
child:on("exit", function(stuff)
p(stuff)
end)
end

test_process_env()
test_process_fail()

assert(process.pid ~= nil)

Expand Down

0 comments on commit bbdeedf

Please sign in to comment.