[2/3] server/process: Avoid risk of exception deadlocks

Message ID 20220607141735.341158-2-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit bd12792f28efd2f03510653ec947ebf961315272
Headers show
Series [1/3] fetch/wget: Move files into place atomically | expand

Commit Message

Richard Purdie June 7, 2022, 2:17 p.m. UTC
The open coded lock acquire/release in the UI event handler doesn't
cover the case an exception occurs and if one did, it could deadlock
the code. Switch to use 'with' statements which would handle this
possibility.

We have seen deadlocks in the UI at exit this so this removes a
possible cause.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/server/process.py | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

Patch

diff --git a/lib/bb/server/process.py b/lib/bb/server/process.py
index 613956f30f..74b74dc39b 100644
--- a/lib/bb/server/process.py
+++ b/lib/bb/server/process.py
@@ -667,18 +667,14 @@  class BBUIEventQueue:
         self.t.start()
 
     def getEvent(self):
-        self.eventQueueLock.acquire()
-
-        if len(self.eventQueue) == 0:
-            self.eventQueueLock.release()
-            return None
-
-        item = self.eventQueue.pop(0)
+        with self.eventQueueLock:
+            if len(self.eventQueue) == 0:
+                return None
 
-        if len(self.eventQueue) == 0:
-            self.eventQueueNotify.clear()
+            item = self.eventQueue.pop(0)
+            if len(self.eventQueue) == 0:
+                self.eventQueueNotify.clear()
 
-        self.eventQueueLock.release()
         return item
 
     def waitEvent(self, delay):
@@ -686,10 +682,9 @@  class BBUIEventQueue:
         return self.getEvent()
 
     def queue_event(self, event):
-        self.eventQueueLock.acquire()
-        self.eventQueue.append(event)
-        self.eventQueueNotify.set()
-        self.eventQueueLock.release()
+        with self.eventQueueLock:
+            self.eventQueue.append(event)
+            self.eventQueueNotify.set()
 
     def send_event(self, event):
         self.queue_event(pickle.loads(event))