diff mbox series

[2/4] server/process: Move heartbeat to idle thread

Message ID 20230111175058.1526619-2-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit 0f9a0c7853b181817bf01863a26da21412376294
Headers show
Series [1/4] tinfoil: Don't wait for events indefinitely | expand

Commit Message

Richard Purdie Jan. 11, 2023, 5:50 p.m. UTC
Rather than risk the heartbeat event code locking up the server control
socket, handle it in the 'idle' thread with the other work. The aim
is to remove it as a possible issue with some ongoing hangs.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/server/process.py | 44 ++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/server/process.py b/lib/bb/server/process.py
index a44ec36139..588a3ae04d 100644
--- a/lib/bb/server/process.py
+++ b/lib/bb/server/process.py
@@ -441,6 +441,28 @@  class ProcessServer():
                     serverlog("Exception %s broke the idle_thread, exiting" % traceback.format_exc())
                     self.quit = True
 
+            # Create new heartbeat event?
+            now = time.time()
+            if bb.event._heartbeat_enabled and now >= self.next_heartbeat:
+                # We might have missed heartbeats. Just trigger once in
+                # that case and continue after the usual delay.
+                self.next_heartbeat += self.heartbeat_seconds
+                if self.next_heartbeat <= now:
+                    self.next_heartbeat = now + self.heartbeat_seconds
+                if hasattr(self.cooker, "data"):
+                    heartbeat = bb.event.HeartbeatEvent(now)
+                    try:
+                        bb.event.fire(heartbeat, self.cooker.data)
+                    except Exception as exc:
+                        if not isinstance(exc, bb.BBHandledException):
+                            logger.exception('Running heartbeat function')
+                        serverlog("Exception %s broke in idle_thread, exiting" % traceback.format_exc())
+                        self.quit = True
+            if nextsleep and bb.event._heartbeat_enabled and now + nextsleep > self.next_heartbeat:
+                # Shorten timeout so that we we wake up in time for
+                # the heartbeat.
+                nextsleep = self.next_heartbeat - now
+
             if time.time() > (lastdebug + 60):
                 lastdebug = time.time()
                 with bb.utils.lock_timeout(self._idlefuncsLock):
@@ -459,28 +481,6 @@  class ProcessServer():
             self.idle = threading.Thread(target=self.idle_thread)
             self.idle.start()
 
-        # Create new heartbeat event?
-        now = time.time()
-        if bb.event._heartbeat_enabled and now >= self.next_heartbeat:
-            # We might have missed heartbeats. Just trigger once in
-            # that case and continue after the usual delay.
-            self.next_heartbeat += self.heartbeat_seconds
-            if self.next_heartbeat <= now:
-                self.next_heartbeat = now + self.heartbeat_seconds
-            if hasattr(self.cooker, "data"):
-                heartbeat = bb.event.HeartbeatEvent(now)
-                try:
-                    bb.event.fire(heartbeat, self.cooker.data)
-                except Exception as exc:
-                    if not isinstance(exc, bb.BBHandledException):
-                        logger.exception('Running heartbeat function')
-                    serverlog("Exception %s broke in idle_commands, exiting" % traceback.format_exc())
-                    self.quit = True
-        if nextsleep and bb.event._heartbeat_enabled and now + nextsleep > self.next_heartbeat:
-            # Shorten timeout so that we we wake up in time for
-            # the heartbeat.
-            nextsleep = self.next_heartbeat - now
-
         if nextsleep is not None:
             if self.xmlrpc:
                 nextsleep = self.xmlrpc.get_timeout(nextsleep)