From ee9885a6010c9384d276b031e9adb23d347280be Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sun, 4 Sep 2022 16:22:29 +0200 Subject: [PATCH] [core] fix concurrency issues * initialize first line of output earlier (before modules are initialized, so that module/thread output cannot interfere) * make sure that update and draw are protected against concurrent access --- bumblebee-status | 4 +++- bumblebee_status/core/output.py | 23 ++++++++++++++------- bumblebee_status/modules/core/pulseaudio.py | 1 - 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index 698bcf8..e7b3ea3 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -75,6 +75,7 @@ def handle_events(config, update_lock): def main(): + global started config = core.config.Config(sys.argv[1:]) level = logging.DEBUG if config.debug() else logging.ERROR if config.logfile(): @@ -97,6 +98,8 @@ def main(): core.input.register(None, core.input.WHEEL_UP, "i3-msg workspace prev_on_output") core.input.register(None, core.input.WHEEL_DOWN, "i3-msg workspace next_on_output") + core.event.trigger("start") + update_lock = threading.Lock() event_thread = threading.Thread(target=handle_events, args=(config, update_lock, )) event_thread.daemon = True @@ -127,7 +130,6 @@ def main(): if util.format.asbool(config.get("engine.collapsible", True)) == True: core.input.register(None, core.input.MIDDLE_MOUSE, output.toggle_minimize) - core.event.trigger("start") started = True signal.signal(10, sig_USR1_handler) while True: diff --git a/bumblebee_status/core/output.py b/bumblebee_status/core/output.py index cee579f..1bd5038 100644 --- a/bumblebee_status/core/output.py +++ b/bumblebee_status/core/output.py @@ -1,6 +1,7 @@ import sys import json import time +import threading import core.theme import core.event @@ -145,6 +146,7 @@ class i3(object): self.__content = {} self.__theme = theme self.__config = config + self.__lock = threading.Lock() core.event.register("update", self.update) core.event.register("start", self.draw, "start") core.event.register("draw", self.draw, "statusline") @@ -176,14 +178,15 @@ class i3(object): self.__content[widget_id]["minimized"] = not self.__content[widget_id]["minimized"] def draw(self, what, args=None): - cb = getattr(self, what) - data = cb(args) if args else cb() - if "blocks" in data: - sys.stdout.write(json.dumps(data["blocks"], default=dump_json)) - if "suffix" in data: - sys.stdout.write(data["suffix"]) - sys.stdout.write("\n") - sys.stdout.flush() + with self.__lock: + cb = getattr(self, what) + data = cb(args) if args else cb() + if "blocks" in data: + sys.stdout.write(json.dumps(data["blocks"], default=dump_json)) + if "suffix" in data: + sys.stdout.write(data["suffix"]) + sys.stdout.write("\n") + sys.stdout.flush() def start(self): return { @@ -244,6 +247,10 @@ class i3(object): return blocks def update(self, affected_modules=None, redraw_only=False, force=False): + with self.__lock: + self.update2(affected_modules, redraw_only, force) + + def update2(self, affected_modules=None, redraw_only=False, force=False): now = time.time() for module in self.__modules: if affected_modules and not module.id in affected_modules: diff --git a/bumblebee_status/modules/core/pulseaudio.py b/bumblebee_status/modules/core/pulseaudio.py index aa9eb3b..9e7150b 100644 --- a/bumblebee_status/modules/core/pulseaudio.py +++ b/bumblebee_status/modules/core/pulseaudio.py @@ -136,7 +136,6 @@ class Module(core.module.Module): core.event.trigger("update", [self.id], redraw_only=True) core.event.trigger("draw") os.set_blocking(proc.stdout.fileno(), False) - time.sleep(0.5) proc.stdout.read() os.set_blocking(proc.stdout.fileno(), True)