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

uv.spawn example code in docs is broken #606

Closed
miversen33 opened this issue Jun 5, 2022 · 3 comments
Closed

uv.spawn example code in docs is broken #606

miversen33 opened this issue Jun 5, 2022 · 3 comments

Comments

@miversen33
Copy link

miversen33 commented Jun 5, 2022

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

  • os: Ubuntu 20.04
  • Lua version: 5.2.4
  • luv version: 1.43.0 (I have tried both installing from the apt repository as well as with luarocks)
  • testing.lua

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
Copy link
Author

miversen33 commented Jun 5, 2022

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
Copy link
Member

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:

luv/tests/test-process.lua

Lines 96 to 138 in 9f80386

test("process stdio", function (print, p, expect, uv)
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},
}, expect(function (code, signal)
p("exit", {code=code, signal=signal})
uv.close(handle)
end))
p{
handle=handle,
pid=pid
}
uv.read_start(stdout, expect(function (err, chunk)
p("stdout", {err=err,chunk=chunk})
assert(not err, err)
assert(chunk == expectedOutput)
uv.close(stdout)
end))
uv.write(stdin, input)
uv.shutdown(stdin, expect(function ()
uv.close(stdin)
end))
end)

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()

@squeek502 squeek502 changed the title uv.spawn example code fails to run on lua 5.2.4 uv.spawn example code in docs is broken Jun 5, 2022
@miversen33
Copy link
Author

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!

@zhaozg zhaozg closed this as completed May 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants