Skip to content

Commit

Permalink
lua: fix print wrapper to handle errors thrown from print
Browse files Browse the repository at this point in the history
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 tarantool#8136

NO_DOC=bugfix
  • Loading branch information
CuriousGeorgiy committed Jan 11, 2023
1 parent 182034d commit 9ac24e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## bugfix/lua

* Fixed `print` function wrapper not handling errors thrown from `print`: this
could render the hide/show prompt feature (gh-7169) to be useless and leak
memory (gh-8136).
5 changes: 4 additions & 1 deletion src/lua/print.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 comments on commit 9ac24e1

Please sign in to comment.