luvit / luv

Bare libuv bindings for lua
Apache License 2.0
832 stars 187 forks source link

process creation segfault #671

Closed XeroOl closed 1 year ago

XeroOl commented 1 year ago

Hello, I'm trying to work with processes in lua.

When I run the following code, (taken verbatim from https://github.com/luvit/luv/blob/master/docs.md), I get a segfault.

local uv = require("luv")

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

print("stdin", stdin)
print("stdout", stdout)
print("stderr", stderr)

local handle, pid = uv.spawn("cat", {
  stdio = {stdin, stdout, stderr}
}, function(code, signal) -- on exit
  print("exit code", code)
  print("exit signal", signal)
end)

print("process opened", handle, pid)

uv.read_start(stdout, function(err, data)
  assert(not err, err)
  if data then
    print("stdout chunk", stdout, data)
  else
    print("stdout end", stdout)
  end
end)

uv.read_start(stderr, function(err, data)
  assert(not err, err)
  if data then
    print("stderr chunk", stderr, data)
  else
    print("stderr end", stderr)
  end
end)

uv.write(stdin, "Hello World")

uv.shutdown(stdin, function()
  print("stdin shutdown", stdin)
  uv.close(handle, function()
    print("process closed", handle, pid)
  end)
end)

The output from my shell is:

stdin   uv_pipe_t: 0x55a26b7bbda0
stdout  uv_pipe_t: 0x55a26b7bd200
stderr  uv_pipe_t: 0x55a26b7bd310
process opened  uv_process_t: 0x55a26b7bd830    88131
zsh: segmentation fault (core dumped)  lua test.lua

I expected to the output of various print statements, with no segfault.

I'm using Lua 5.4.6 on Arch Linux. Let me know if I've done anything wrong or if any more details are needed.

Bilal2453 commented 1 year ago

The examples assume an implied uv.run call at the end of the code (In Luvit, this call is done for you). Though when using Luv directly, you should add this call explicitly

XeroOl commented 1 year ago

thank you for the quick reply! adding uv.run() to the end of the file makes the problem go away.