From 9c9bf68dca48393f127d24a9a6a80c2c1b1e32bf Mon Sep 17 00:00:00 2001 From: Shane Brown Date: Fri, 6 Jun 2025 12:02:55 -0600 Subject: [PATCH] Fix logging byte chunks --- buildrunner/docker/runner.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/buildrunner/docker/runner.py b/buildrunner/docker/runner.py index 798cef4..5bd25ca 100644 --- a/buildrunner/docker/runner.py +++ b/buildrunner/docker/runner.py @@ -615,9 +615,27 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None): self._run_log(log, output_buffer) elif hasattr(output_buffer, "next") or isinstance(output_buffer, GeneratorType): try: - for line in output_buffer: - self._run_log(console, line) - self._run_log(log, line) + # Buffer chunks into complete lines + line_buffer = b"" + for chunk in output_buffer: + if isinstance(chunk, str): + chunk = chunk.encode("utf-8") + line_buffer += chunk + + # Process complete lines + while b"\n" in line_buffer: + line, line_buffer = line_buffer.split(b"\n", 1) + if ( + line or line_buffer + ): # Don't log empty lines unless there's more content + line_with_newline = line + b"\n" + self._run_log(console, line_with_newline) + self._run_log(log, line_with_newline) + + # Process any remaining content in buffer + if line_buffer: + self._run_log(console, line_buffer) + self._run_log(log, line_buffer) except socket.timeout: # Ignore timeouts since we check for the exit code anyways at the end pass