luv icon indicating copy to clipboard operation
luv copied to clipboard

Error handling is not graceful in callback function

Open mythay opened this issue 10 years ago • 3 comments

After I have registered a callback to a luv handle, when the callback need to be called, we use luv_call_callback to call it. It's is OK when there is no exception when calling the callback function. But when error occured, it just print the stack traceback and call exit to end the process.

I think it is better to provide an alternative way just like "uncaughtException" in nodejs to allow user to do more control.

And I think luv_call_callback should to be exported just like "MakeCallback" in NAN, so it can provide a consistent error handling machanism when I write a extension for luv.

mythay avatar Nov 16 '15 02:11 mythay

The idea is that error handling will be handled at the lua level instead of at the C level. The fatal exit here is to prevent strange undefined behavior when errors go uncaught.

creationix avatar Nov 16 '15 16:11 creationix

After thinking about this for a while, I do think this needs to be handled at the luv level. How exactly does NAN make MakeCallback configurable? My idea is to allow customizing from the lua side a function that intercepts all call stacks:

function luv.makeCallback(fn)
  -- do anything custom here if desired
  return fn
end

And so a custom one would wrap in xpcall and route errors

function luv.makeCallback(fn)
  return function (...)
    local args = {...}
    local success, err = xpcall(function ()
      return fn(unpack(args))
    end, debug.traceback)
    -- TODO: handle success, and err
  end
end

Though I worry about the performance hit from adding all these closures to every event source.

creationix avatar Dec 16 '15 20:12 creationix

See #128 for more related ideas.

creationix avatar Dec 16 '15 20:12 creationix