luvit / luv

Bare libuv bindings for lua
Apache License 2.0
823 stars 185 forks source link

uv.spawn example code in docs is broken #606

Closed miversen33 closed 1 year ago

miversen33 commented 2 years ago

Hello! I am looking into trying to use luv's implementation of spawn but the provided sample code is unusable. When it is run (I've attached the example I am using), I receive the following error

Uncaught Error: bad argument #1 to '?' (Expected uv_handle userdata)
stack traceback:
        [C]: in ?
        [C]: in function '_G.tostring'
        [C]: in function 'print'
        /tmp/testing.lua:41: in function </tmp/testing.lua:40>
        [C]: in ?

Line 41

print("stdin shutdown", stdin)

Removing this line avoids this error but another is thrown

Uncaught Error: /tmp/testing.lua:41: bad argument #1 to 'close' (Expected uv_handle userdata)
stack traceback:
        [C]: in function 'close'
        /tmp/testing.lua:41: in function </tmp/testing.lua:40>
        [C]: in ?

New line 41

uv.close(handle, function()

I can't quite pin my finger on what the error is, hoping someone can get me pointed in the right direction.

Useful info

I am having other issues with spawn as well (such as not being able to get stdout from it, it leaving defunct processes, etc) but I think this is due to my misunderstanding of how I am supposed to be using it.

Thanks!

miversen33 commented 2 years ago

Additional notes, I also cannot get anything useful from the code snippet provided in uv.pipe

local uv = require("luv")
local fds = uv.pipe({nonblock=true}, {nonblock=true})

local read_pipe = uv.new_pipe()
read_pipe:open(fds.read)

local write_pipe = uv.new_pipe()
write_pipe:open(fds.write)

write_pipe:write("hello")
read_pipe:read_start(function(err, chunk)
  assert(not err, err)
  print(chunk)
end)

The above code does not fail, it just does nothing. I never see anything print from the read_pipe:read_start callback which leads me to believe this issue might be related to pipes in general, though I still don't know why they are failing here :/

I have verified that spawn does start the process (verified by doing, using it to modify my filesystem via mkdir). However, I cannot get output from the pipes

squeek502 commented 2 years ago

I believe the biggest problem you're running into is that you don't have a call to uv.run() at the end of your scripts so the Libuv event loop never runs. This would be enough fix the code in the second comment, but the uv.spawn example from docs.md does seem to be broken.

The most up-to-date examples would be from our tests. Here's a relevant uv.spawn test:

https://github.com/luvit/luv/blob/9f80386338af7d164ff1f47d480ee1ae775cb0ef/tests/test-process.lua#L96-L138

Here's a full adapted version that will work outside our test setup:

local uv = require('luv')

-- set this depending on your OS
local isWindows = false

local stdin = uv.new_pipe(false)
local stdout = uv.new_pipe(false)

local input = "Hello World"
local cmd, args, expectedOutput
if isWindows then
  cmd = "cmd.exe"
  args = {"/c", "set /p output=&call echo %output%"}
  expectedOutput = input .. "\r\n"
else
  cmd = "cat"
  args = {"-"}
  expectedOutput = input
end

local handle, pid
handle, pid = uv.spawn(cmd, {
  args = args,
  stdio = {stdin, stdout},
}, function (code, signal)
  print("exit", code, signal)
  uv.close(handle)
end)

print(handle, pid)

uv.read_start(stdout, function (err, chunk)
  print("stdout", chunk)
  assert(not err, err)
  assert(chunk == expectedOutput)
  uv.close(stdout)
end)

uv.write(stdin, input)
uv.shutdown(stdin, function ()
  uv.close(stdin)
end)

uv.run()
miversen33 commented 2 years ago

Ahh it was uv.run that was getting me. There's a blurb about it in the tcp example at the top but I ignored the example as I wasn't interested in socket/network communication. Helps if you fully read the docs I suppose lol.

Thank you!