Skip to content
Snippets Groups Projects
Commit c9986d93 authored by Georgiy Lebedev's avatar Georgiy Lebedev Committed by Vladimir Davydov
Browse files

lua: fix `print` wrapper to handle errors thrown from `print`

Both of the callbacks in the `print` wrapper are expected to be called, but
`print` may throw errors, e.g.,
`print(setmetatable({}, {__tostring = error})`, so we need to call it in a
protected environment and execute the 'after' callback even if `print`
throws.

Closes #8136

NO_CHANGELOG=<gh-7186 was not released yet>
NO_DOC=bugfix
parent ef85bc8b
No related branches found
No related tags found
No related merge requests found
......@@ -8,10 +8,13 @@ _G.print = function(...)
if M.before_cb ~= nil then
M.before_cb()
end
M.raw_print(...)
local ok, err = pcall(M.raw_print, ...)
if M.after_cb ~= nil then
M.after_cb()
end
if not ok then
error(err)
end
end
return M
local it = require('test.interactive_tarantool')
local t = require('luatest')
local g = t.group()
-- Checks that `print` throwing an error is handled correctly.
g.test_basic_print_with_exception = function()
local child = it.new({args = {'-l', 'fiber'}})
child:execute_command([[
_ = fiber.create(function()
fiber.name('print_flood', {truncate = true})
while true do
pcall(function()
print(setmetatable({}, {__tostring = error}))
end)
print('flood')
fiber.sleep(0.01)
end
end)
]])
child:assert_empty_response()
local exp_line = it.PROMPT .. it.CR .. it.ERASE_IN_LINE ..
it.PROMPT .. it.CR .. it.ERASE_IN_LINE ..
'flood'
for _ = 1, 10 do
child:assert_line(exp_line)
end
child:close()
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment