From 9fa01d39afece5faae6338e7413cfeacc4d2bf44 Mon Sep 17 00:00:00 2001 From: elig0n <31196036+elig0n@users.noreply.github.com> Date: Wed, 31 Mar 2021 12:36:01 +0300 Subject: [PATCH 1/2] Re-instantiate after a broken pipe when restarting i3 the socket connection is lost (broken pipe), requiring manual restart of the script and optionally killing the amassing previous one(s) this invents a stop mechanism for the threads and also re-instantiate FocusWatcher when a broken pipe exception is detected history is lost but registry continues on current session --- examples/focus-last.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/focus-last.py b/examples/focus-last.py index ccc884b..b719e95 100755 --- a/examples/focus-last.py +++ b/examples/focus-last.py @@ -15,7 +15,9 @@ class FocusWatcher: + global focus_watcher def __init__(self): + self.alive = True self.i3 = i3ipc.Connection() self.i3.on('window::focus', self.on_window_focus) # Make a directory with permissions that restrict access to @@ -52,24 +54,33 @@ def read(conn): data = conn.recv(1024) if data == b'switch': with self.window_list_lock: - tree = self.i3.get_tree() - windows = set(w.id for w in tree.leaves()) - for window_id in self.window_list[1:]: - if window_id not in windows: - self.window_list.remove(window_id) - else: - self.i3.command('[con_id=%s] focus' % window_id) - break + try: + tree = self.i3.get_tree() + windows = set(w.id for w in tree.leaves()) + for window_id in self.window_list[1:]: + if window_id not in windows: + self.window_list.remove(window_id) + else: + self.i3.command('[con_id=%s] focus' % window_id) + break + except BrokenPipeError: + print("Caught broken pipe, restarting...") + self.alive = False elif not data: selector.unregister(conn) conn.close() selector.register(self.listening_socket, selectors.EVENT_READ, accept) - while True: + while self.alive: for key, event in selector.select(): callback = key.data callback(key.fileobj) + + self.i3.main_quit() + focus_watcher = FocusWatcher() + focus_watcher.run() + del(self) def run(self): t_i3 = threading.Thread(target=self.launch_i3) From 864a0e0451d7a7f228c57fee8938529e367542bb Mon Sep 17 00:00:00 2001 From: elig0n <31196036+elig0n@users.noreply.github.com> Date: Wed, 31 Mar 2021 12:55:51 +0300 Subject: [PATCH 2/2] removed global declaration and EOL spaces --- examples/focus-last.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/focus-last.py b/examples/focus-last.py index b719e95..814d933 100755 --- a/examples/focus-last.py +++ b/examples/focus-last.py @@ -15,7 +15,6 @@ class FocusWatcher: - global focus_watcher def __init__(self): self.alive = True self.i3 = i3ipc.Connection() @@ -77,10 +76,10 @@ def read(conn): callback = key.data callback(key.fileobj) - self.i3.main_quit() + self.i3.main_quit() focus_watcher = FocusWatcher() focus_watcher.run() - del(self) + del(self) def run(self): t_i3 = threading.Thread(target=self.launch_i3)