Skip to contentSkip to conversation

server/process: Ensure we don't keep looping if some other server is started

PATCH New Richard Purdie 1 participant 2 messages Sep 5, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 server/process: Ensure we don't keep looping if some other server is started

Showing "leftover process" messages when a new server has started and is being used by some UI is horrible. Compare the PID data from the lockfile to avoid this (and the ton of confusing log data it generates).

Also, move the time.sleep() call to be after the first lock attempt, which reduces noise in the logs significantly.

---
 lib/bb/server/process.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
lib/bb/server/process.py +20 -1
index f794505fd4..83c3e6b444 100644
@@ -266,6 +266,17 @@ class ProcessServer():
         # Finally release the lockfile but warn about other processes holding it open
         lock = self.bitbake_lock
         lockfile = self.bitbake_lock_name
+
+        def get_lock_contents(lockfile):
+            try:
+                with open(lockfile, "r") as f:
+                    return f.readlines()
+            except FileNotFoundError:
+                return None
+
+        lockcontents = get_lock_contents(lockfile)
+        serverlog("Original lockfile contents: " + str(lockcontents))
+
         lock.close()
         lock = None
 
@@ -273,14 +284,22 @@ class ProcessServer():
             i = 0
             lock = None
             while not lock and i < 30:
-                time.sleep(0.1)
                 lock = bb.utils.lockfile(lockfile, shared=False, retry=False, block=False)
+                if not lock:
+                    newlockcontents = get_lock_contents(lockfile)
+                    if newlockcontents != lockcontents:
+                        # A new server was started, the lockfile contents changed, we can exit
+                        serverlog("Lockfile now contains different contents, exiting: " + str(newlockcontents))
+                        return
+                    time.sleep(0.1)
                 i += 1
             if lock:
                 # We hold the lock so we can remove the file (hide stale pid data)
                 # via unlockfile.
                 bb.utils.unlockfile(lock)
+                serverlog("Exiting as we could obtain the lock")
                 return
+
             if not lock:
                 # Some systems may not have lsof available
                 procs = None
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 server/process: Prefix the log data with pid/time information

Knowing which process printed which messages and the timestamp of the message is useful for debugging, so add this. Ensure the log parsing isn't affected by using search() instead of match().

---
 lib/bb/server/process.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/bb/server/process.py +2 -2
index 83c3e6b444..21f95cb61e 100644
@@ -36,7 +36,7 @@ class ProcessTimeout(SystemExit):
     pass
 
 def serverlog(msg):
-    print(msg)
+    print(str(os.getpid()) + " " +  datetime.datetime.now().strftime('%H:%M:%S.%f') + " " + msg)
     sys.stdout.flush()
 
 class ProcessServer():
@@ -480,7 +480,7 @@ class BitBakeServer(object):
                             lines.append(line)
                         else:
                             lastlines.append(line)
-                            res = logstart_re.match(line.rstrip())
+                            res = logstart_re.search(line.rstrip())
                             if res:
                                 ldatetime = datetime.datetime.strptime(res.group(2), start_log_datetime_format)
                                 if ldatetime >= startdatetime:
Signature · 1 line
2.25.1