process: Clean up connection retry logic
PATCH 1/2 process: Clean up connection retry logic
Its possible for a connection to connect to the server as its shutting down but before its removed the socket file. This patch:
a) Removes the socket file earlier to avoid connections.
b) Handles EOFError in initial connections gracefully. These occur if the socket is closed during the server shutdown.
c) Ensure duplicate events aren't shown on the console. This makes debugging these issues very very confusing.
With these changes the backtrace that was concerning users is hidden and the server works as expected with a reconnect when it catches it in a bad state.
--- lib/bb/main.py | 13 ++++++++++--- lib/bb/server/process.py | 8 ++++---- 2 files changed, 14 insertions(+), 7 deletions(-)
lib/bb/main.py +10 -3
@@ -438,9 +438,10 @@ def setup_bitbake(configParams, configuration, extrafeatures=None): return None, None # we start a server with a given configuration logger.info("Starting bitbake server...") - server = bb.server.process.BitBakeServer(lock, sockname, configuration, featureset) - # The server will handle any events already in the queue + # Clear the event queue since we already displayed messages bb.event.ui_queue = [] + server = bb.server.process.BitBakeServer(lock, sockname, configuration, featureset) + else: logger.info("Reconnecting to bitbake server...") if not os.path.exists(sockname):
@@ -448,7 +449,13 @@ def setup_bitbake(configParams, configuration, extrafeatures=None): time.sleep(5) raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?") if not configParams.server_only: - server_connection = bb.server.process.connectProcessServer(sockname, featureset) + try: + server_connection = bb.server.process.connectProcessServer(sockname, featureset) + except EOFError: + # The server may have been shutting down but not closed the socket yet. If that happened, + # ignore it. + pass + if server_connection or configParams.server_only: break except (Exception, bb.server.process.ProcessTimeout) as e:
lib/bb/server/process.py +4 -4
@@ -215,6 +215,10 @@ class ProcessServer(multiprocessing.Process): ready = self.idle_commands(.1, fds) print("Exiting") + # Remove the socket file so we don't get any more connections to avoid races + os.unlink(self.sockname) + self.sock.close() + try: self.cooker.shutdown(True) except:
@@ -222,10 +226,6 @@ class ProcessServer(multiprocessing.Process): self.cooker.post_serve() - # Remove the socket file so we don't get any more connections to avoid races - os.unlink(self.sockname) - self.sock.close() - # Finally release the lockfile but warn about other processes holding it open lock = self.bitbake_lock lockfile = lock.name
Signature · 1 line
2.7.4
PATCH 2/2 main: Attempt to gain bitbake.lock rather than just waiting
Rather than just waiting for 5s, try and get the lockfile. If we gain the lock, we know we're ready to retry and can skip any remaining timeout.
--- lib/bb/main.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
lib/bb/main.py +9 -1
@@ -446,7 +446,15 @@ def setup_bitbake(configParams, configuration, extrafeatures=None): logger.info("Reconnecting to bitbake server...") if not os.path.exists(sockname): print("Previous bitbake instance shutting down?, waiting to retry...") - time.sleep(5) + i = 0 + lock = None + # Wait for 5s or until we can get the lock + while not lock and i < 50: + time.sleep(0.1) + _, lock = lockBitbake() + i += 1 + if lock: + bb.utils.unlockfile(lock) raise bb.server.process.ProcessTimeout("Bitbake still shutting down as socket exists but no lock?") if not configParams.server_only: try:
Signature · 1 line
2.7.4