From c6c7128099cb74ff4eb65d3957ff498e44cfb6ba Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <info@fabiomanganiello.com>
Date: Tue, 14 Jun 2022 16:47:52 +0200
Subject: [PATCH] Wrapped clipboard management logic in a try-except block to
 prevent the clipboard plugin from failing hard

---
 platypush/plugins/clipboard/__init__.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/platypush/plugins/clipboard/__init__.py b/platypush/plugins/clipboard/__init__.py
index a36db07f..c1364b05 100644
--- a/platypush/plugins/clipboard/__init__.py
+++ b/platypush/plugins/clipboard/__init__.py
@@ -1,3 +1,4 @@
+from time import time
 from typing import Optional
 
 from platypush.context import get_bus
@@ -47,13 +48,22 @@ class ClipboardPlugin(RunnablePlugin):
     def main(self):
         import pyperclip
 
-        while not self.should_stop():
-            text = pyperclip.paste()
-            if text and text != self._last_text:
-                get_bus().post(ClipboardEvent(text=text))
-                self._last_text = text
+        last_error_time = 0
 
-            self._should_stop.wait(0.1)
+        while not self.should_stop():
+            try:
+                text = pyperclip.paste()
+                if text and text != self._last_text:
+                    get_bus().post(ClipboardEvent(text=text))
+                    self._last_text = text
+            except Exception as e:
+                if time() - last_error_time > 60:
+                    last_error_time = time()
+                    self.logger.error(
+                        'Could not access the clipboard: %s: %s', type(e), e
+                    )
+            finally:
+                self._should_stop.wait(0.1)
 
         self.logger.info('Stopped clipboard monitor backend')