Skip to content

Commit

Permalink
Fix: Deal with non-utf8 encoded bytes in comm
Browse files Browse the repository at this point in the history
Closes #697
  • Loading branch information
klieret committed Aug 20, 2024
1 parent 8884cc1 commit d0f3f63
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions sweagent/environment/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def ready_to_read(fd) -> bool:
return True
return bool(select.select([fd], [], [], 0.01)[0])

n_decode_failures = 0
while time.time() < end_time:
if ready_to_read(fd):
try:
Expand All @@ -227,17 +226,9 @@ def ready_to_read(fd) -> bool:
break
if data:
buffer += data
try:
decoded = buffer.decode()
except UnicodeDecodeError:
# Should we reset the buffer to skip the byte that causes the error?
n_decode_failures += 1
if n_decode_failures > 30:
msg = "Too many decode failures while reading from subprocess."
raise RuntimeError(msg)
else:
if PROCESS_DONE_MARKER_START in decoded:
break
decoded = buffer.decode("utf-8", errors="backslashreplace")
if PROCESS_DONE_MARKER_START in decoded:
break
time.sleep(0.01) # Prevents CPU hogging

if container.poll() is not None:
Expand All @@ -246,7 +237,8 @@ def ready_to_read(fd) -> bool:
if time.time() >= end_time:
msg = f"Timeout reached while reading from subprocess.\nCurrent buffer: {buffer.decode()}"
raise TimeoutError(msg)
decoded = buffer.decode()

decoded = buffer.decode("utf-8", errors="backslashreplace")
body = "\n".join(line for line in decoded.splitlines() if not line.startswith(PROCESS_DONE_MARKER_START))
_results = PROCESS_DONE_REGEX.search(decoded)
if _results is None:
Expand Down

0 comments on commit d0f3f63

Please sign in to comment.