Skip to content
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

Make gears.object:emit_signal fail proof (Fix #3667) #3669

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/gears/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local pairs = pairs
local type = type
local error = error
local properties = require("gears.object.properties")
local protected_call = require("gears.protected_call")

local object = { properties = properties, mt = {} }

Expand Down Expand Up @@ -132,13 +133,13 @@ end
function object:emit_signal(name, ...)
local sig = find_signal(self, name)
for func in pairs(sig.strong) do
func(self, ...)
protected_call(func, self, ...)
end
for func in pairs(sig.weak) do
func(self, ...)
protected_call(func, self, ...)
end
for _, func in ipairs(self._global_receivers) do
func(name, self, ...)
protected_call(func, name, self, ...)
end
end

Expand All @@ -158,7 +159,7 @@ function object._setup_class_signals(t, args)
assert(name)
for _, func in pairs(conns[name] or {}) do
if condition(...) then return end
func(...)
protected_call(func, ...)
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/gears/object_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
---------------------------------------------------------------------------

local object = require("gears.object")
local gdebug = require("gears.debug")

describe("gears.object", function()
local obj
Expand Down Expand Up @@ -185,6 +186,29 @@ describe("gears.object", function()
object{enable_auto_signals=true, enable_properties=false}
end)
end)

it(
"signal callback with exception shouldn't break the emit_signal function",
function()
-- Stop the error reporting during tests
local orig_print_error = gdebug.print_error
gdebug.print_error = function() end

local cb = spy.new(function() end)
obj:connect_signal("error", function()
error "error"
end)
obj:connect_signal("error", function()
cb()
end)

obj:emit_signal "error"
assert.spy(cb).was_called()

-- Restore gdebug
gdebug.print_error = orig_print_error
end
)
end)

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80