-
Notifications
You must be signed in to change notification settings - Fork 185
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
How callback errors are handled has changed (maybe unintentionally) #433
Comments
My original meaning is to avoid the exit of the program in the callback function, and I don't realize the existence of this problem ( Simply to say, I tend to restore the previous exit to avoid I'll make a PR to try. |
I think the problem is that there's no realistic way to recover from execution errors in callbacks, since Lua doesn't even have a way to know they happened. I'm away from my computer at the moment so I can't come up with an example, but I will tomorrow. We can solve this one of two ways:
|
1. To avoid undefine…d behavior in libuv and luajit follow luvit@7526c1d. 2. Replay luvit#433. 3. LUVF_CALLBACK_ flags introduced by luvit@5b68007#diff-4221738e9478598ca8f06be66d888559
@squeek502 A kind and responsible person. 👍 |
Here's a somewhat contrived example, but I think it shows the problem. local uv = require('luv')
local contents = nil
-- infinite timer that ends when contents is not nil
local timer = uv.new_timer()
timer:start(0, 500, function()
print("timer tick")
if contents ~= nil then
print(contents)
timer:stop()
timer:close()
end
end)
-- read a file async and set the contents var to the contents of the file
local fd = assert(uv.fs_open("something.txt", "r", tonumber('644', 8)))
assert(uv.fs_read(fd, 32, 0, function(err, chunk)
assert(not err, err)
-- uncomment the next line to force a simulated execution error
--error("some execution error")
contents = chunk
assert(uv.fs_close(fd))
end))
uv.run() This code works fine as long as the
The program ends because the timer is closed and the uv loop is done. However, if there is an execution error in the When forcing an execution error with Luv 1.9.1 (which still had the hard exit), this is the output:
When forcing an execution error with current master (hard exit removed), this is the output:
So, the easy route is just to exit the program with an error status code whenever there's an execution error within a callback, since we can't predict if the error is recoverable or not. The harder route is allowing Lua to handle errors within callbacks (#127, #128, #192) |
I guess this explains why my program will sometimes not exit after an error. I never thought much of it, only that maybe it was normal. Why is lua_error not used? Because of potential pcalls? |
Good question, I'm not sure. |
Do you really want to exit after an error, we should try to avoid that but need more work to achive.
We can try lua_error. and it's time to make a clear logic to handle errors. |
hope @bfredl to know this, this related with #345, #350. |
Currently, any runtime errors within a callback don't affect the exit code of Lua (they are just printed to stderr and then execution continues). This seems to have changed in 89f7baf#diff-49ea526d495b77fdd140adde02ced70eL109 (#254). Previously, any errors within a callback would lead to
exit(-1)
(the exit was added in 7526c1d).Simple test case:
Before, running this would cause Lua to exit with status code
-1
, but right now it exits with status code0
.This is a major change that might have been unintentional. If it wasn't, then we'll need to figure out the implications (since not exiting on error could lead to weird problems as mentioned in 7526c1d).
Related to #127, #128, #192.
@zhaozg
The text was updated successfully, but these errors were encountered: