Skip to content

Commit

Permalink
fix: Raise error when aexpect_helper doesn't work properly
Browse files Browse the repository at this point in the history
When aexpect_helper does not work properly, the process will hang
without any log information. This fix checks the process status and
raises an error if the process terminates prematurely.

Signed-off-by: Yihuang Yu <[email protected]>
  • Loading branch information
PaulYuuu committed Aug 1, 2024
1 parent 4e5ac96 commit 2126738
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions aexpect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,16 @@ 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_ouput = ""
pattern = f"Server {self.a_id} ready"
while True:
output = sub.stdout.readline().decode(self.encoding, "ignore")
if pattern in output:
break
full_ouput += output
sub_status = sub.poll()
if sub_status is not None:
raise ExpectProcessTerminatedError(pattern, sub_status, full_ouput)

# Open the reading pipes
if is_file_locked(self.lock_server_running_filename):
Expand Down Expand Up @@ -522,6 +529,7 @@ def __init__(self, command=None, a_id=None, auto_close=False, echo=False,
:param encoding: Override text encoding (by default: autodetect by
locale.getpreferredencoding())
"""
self.tail_thread = None
# Add a reader and a close hook
self._add_reader("tail")
self._add_close_hook(Tail._join_thread)
Expand All @@ -542,7 +550,6 @@ def __init__(self, command=None, a_id=None, auto_close=False, echo=False,
self.output_prefix = output_prefix

# Start the thread in the background
self.tail_thread = None
if self.is_alive():
if termination_func or output_func:
self._start_thread()
Expand Down

0 comments on commit 2126738

Please sign in to comment.