awesomeWM / awesome

awesome window manager
https://awesomewm.org/
GNU General Public License v2.0
6.37k stars 598 forks source link

single_shot in gears.timer not working with a lua error() in the callback #3667

Open FluffyDango opened 2 years ago

FluffyDango commented 2 years ago

Output of awesome --version:

awesome v4.3 (Too long) • Compiled against Lua 5.3.6 (running with Lua 5.3) • D-Bus support: ✔ • execinfo support: ✔ • xcb-randr version: 1.6 • LGI version: 0.9.2

How to reproduce the issue:

  1. Create a timer with

    gears.timer {
    timeout = 2,
    autostart = true,
    callback = function()
        test()
    end,
    single_shot = true
    }
  2. In the test() callback add error() Example:

    local function test()
    naughty.notify({ text = "One" })
    naughty.notify({ text = "Two" })
    error("give error")
    end

Actual result:

The callback keeps going without stopping and no error is given.

Expected result:

It should only run once, give an error and stop.

Aire-One commented 2 years ago

Hello,

Throwing an error in the callback results in breaking the execution of the emit_signal method. I have proposed a fix that mitigates the issue.

There is also something you can do on the user side : catching the error in the callback.

gears.timer {
    timeout = 2,
    autostart = true,
    callback = function()
        local status, result = pcall(test)
        if not status then
            naughty.notify({ text = result })
        end
    end,
    single_shot = true
}

Obviously, this "user level fix" wouldn't work if this is the callback itself that throw the error. So the proposed PR is still required to correctly mitigate the issue.