Skip to content

Commit

Permalink
Prevent KeyError in third party hook registries
Browse files Browse the repository at this point in the history
Reloading the config can cause third party hook registries to raise a
KeyError when firing hooks. This is because the `subscriptions` dict is
cleared but the regitry key is only created when a hook is subscribed
(as the hook module is not reloaded). Therefore, if a user does not
subcribe to any hooks from the third party library, this will cause the
hook to fail.

Fixes: elParaguayo/qtile-extras#301
  • Loading branch information
elParaguayo committed Dec 15, 2023
1 parent 20739f8 commit fbf7fd3
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libqtile/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def register_hook(self, hook: Hook) -> None:
def fire(self, event, *args, **kwargs):
if event not in self.subscribe.hooks:
raise utils.QtileError("Unknown event: %s" % event)
# We should check if the registry name is in the subscriptions dict
# A name can disappear if the config is reloaded (which clears subscriptions)
# but there are no hook subscriptions. This is not an issue for qtile core but
# third party libraries will need this to prevent KeyErrors when firing hooks
if self.name not in subscriptions:
subscriptions[self.name] = dict()
for i in subscriptions[self.name].get(event, []):
try:
if asyncio.iscoroutinefunction(i):
Expand Down

0 comments on commit fbf7fd3

Please sign in to comment.