From 9414aec4b9895889224e4bc1f83410a831dbcf4c Mon Sep 17 00:00:00 2001 From: Yihuang Yu Date: Thu, 1 Aug 2024 14:57:22 +0800 Subject: [PATCH] fix: Raise error when aexpect_helper doesn't work properly When aexpect_helper does not work properly, the process will hang without any log information. This fix adds a timeout and checks the process status and raises an error if the process terminates prematurely. Signed-off-by: Yihuang Yu --- aexpect/client.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/aexpect/client.py b/aexpect/client.py index d686f59..967310f 100644 --- a/aexpect/client.py +++ b/aexpect/client.py @@ -193,9 +193,19 @@ def __init__(self, command=None, a_id=None, auto_close=False, echo=False, sub.stdin.write(f"{command}\n".encode(self.encoding)) sub.stdin.flush() # Wait for the server to complete its initialization - while (f"Server {self.a_id} ready" not in - sub.stdout.readline().decode(self.encoding, "ignore")): - pass + full_output = "" + pattern = f"Server {self.a_id} ready" + end_time = time.time() + 60 + while time.time() < end_time: + output = sub.stdout.readline().decode(self.encoding, "ignore") + if pattern in output: + break + full_output += output + sub_status = sub.poll() + if sub_status is not None: + raise ExpectProcessTerminatedError(pattern, sub_status, full_output) + else: + raise ExpectTimeoutError(pattern, full_output) # Open the reading pipes if is_file_locked(self.lock_server_running_filename):