Skip to contentSkip to conversation

server/process/daemonize: Use sys.exit(), not os._exit() for server

PATCH New Richard Purdie 1 participant 3 messages Aug 22, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 server/process/daemonize: Use sys.exit(), not os._exit() for server

By using os._exit() in the server, we confuse python programmers no end since atexit calls, along with threading cleanup calls are skipped. We're careful about the environment the server is created within so we should be able to use sys.exit() here and have things behave as the python devs intend.

---
 lib/bb/daemonize.py      | 4 +---
 lib/bb/server/process.py | 1 +
 2 files changed, 2 insertions(+), 3 deletions(-)
lib/bb/daemonize.py +1 -3
index f01e6ec7cc..0ec5ef3986 100644
@@ -89,9 +89,7 @@ def createDaemon(function, logfile):
         traceback.print_exc()
     finally:
         bb.event.print_ui_queue()
-        # os._exit() doesn't flush open files like os.exit() does. Manually flush
-        # stdout and stderr so that any logging output will be seen, particularly
+        # Manually flush stdout and stderr so that any logging output will be seen, particularly
         # exception tracebacks.
         sys.stdout.flush()
         sys.stderr.flush()
-        os._exit(0)
lib/bb/server/process.py +1 -0
index 65e1eab527..b03d64391b 100644
@@ -492,6 +492,7 @@ class BitBakeServer(object):
             # Flush any ,essages/errors to the logfile before exit
             sys.stdout.flush()
             sys.stderr.flush()
+        sys.exit(0)
 
 def connectProcessServer(sockname, featureset):
     # Connect to socket
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/3 cooker: Ensure parse_quit thread is closed down

Each run through the parser would leak a thread from the queue created to shut the parser down. Close this down correctly and clean up the code flow slightly whilst in the area, making sure this thread does shut down correctly (we don't care if it loses data).

---
 lib/bb/cooker.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
lib/bb/cooker.py +8 -6
index 9123605461..05be9bef67 100644
@@ -2103,12 +2103,11 @@ class CookerParser(object):
                                             self.total)
 
             bb.event.fire(event, self.cfgdata)
-            for process in self.processes:
-                self.parser_quit.put(None)
-        else:
-            self.parser_quit.cancel_join_thread()
-            for process in self.processes:
-                self.parser_quit.put(None)
+
+        # Allow data left in the cancel queue to be discarded
+        self.parser_quit.cancel_join_thread()
+        for process in self.processes:
+            self.parser_quit.put(None)
 
         # Cleanup the queue before call process.join(), otherwise there might be
         # deadlocks.
@@ -2125,6 +2124,9 @@ class CookerParser(object):
             else:
                 process.join()
 
+        self.parser_quit.close()
+        self.parser_quit.join_thread()
+
         def sync_caches():
             for c in self.bb_caches.values():
                 c.sync()
Signature · 1 line
2.25.1
#2 lore raw

PATCH 3/3 cooker: Explictly shut down the sync thread

In reply to Richard Purdie

Hongxu Jia reported a problem where the bb_cache files were not always being written out correctly. This was due to the sync thread being terminated prematurely.

Whilst the preceeding changes mean the exit handler for this thread is now correctly called since we switch to using sys.exit() instead of os._exit(), this write can happen after we drop the bitbake lock, leading to potential races. Avoid that headache by adding in explicit thread join() calls before we drop the lock (which atexit or Finalize can't do).

---
 lib/bb/cooker.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/bb/cooker.py +7 -1
index 05be9bef67..33452e7b93 100644
@@ -1674,6 +1674,7 @@ class BBCooker:
 
         if self.parser:
             self.parser.shutdown(clean=not force, force=force)
+            self.parser.final_cleanup()
 
     def finishcommand(self):
         self.state = state.initial
@@ -2063,6 +2064,7 @@ class CookerParser(object):
 
         self.start()
         self.haveshutdown = False
+        self.syncthread = None
 
     def start(self):
         self.results = self.load_cached()
@@ -2132,8 +2134,8 @@ class CookerParser(object):
                 c.sync()
 
         sync = threading.Thread(target=sync_caches)
+        self.syncthread = sync
         sync.start()
-        multiprocessing.util.Finalize(None, sync.join, exitpriority=-100)
         bb.codeparser.parser_cache_savemerge()
         bb.fetch.fetcher_parse_done()
         if self.cooker.configuration.profile:
@@ -2147,6 +2149,10 @@ class CookerParser(object):
             bb.utils.process_profilelog(profiles, pout = pout)
             print("Processed parsing statistics saved to %s" % (pout))
 
+    def final_cleanup(self):
+        if self.syncthread:
+            self.syncthread.join()
+
     def load_cached(self):
         for mc, cache, filename, appends in self.fromcache:
             cached, infos = cache.load(filename, appends)
Signature · 1 line
2.25.1