diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index 653be05..04cc7e1 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -94,8 +94,6 @@ class IsolationProvider(ABC): self.proc_stderr = subprocess.PIPE else: self.proc_stderr = subprocess.DEVNULL - # Store the proc stderr in memory - self.stderr = BytesIO() def should_capture_stderr(self) -> bool: return self.debug or getattr(sys, "dangerzone_dev", False) @@ -335,8 +333,10 @@ class IsolationProvider(ABC): timeout_force: int = TIMEOUT_FORCE, ) -> Iterator[subprocess.Popen]: """Start a conversion process, pass it to the caller, and then clean it up.""" + # Store the proc stderr in memory + stderr = BytesIO() p = self.start_doc_to_pixels_proc(document) - stderr_thread = self.start_stderr_thread(p) + stderr_thread = self.start_stderr_thread(p, stderr) if platform.system() != "Windows": assert os.getpgid(p.pid) != os.getpgid( @@ -357,7 +357,7 @@ class IsolationProvider(ABC): # Wait for the thread to complete. If it's still alive, mention it in the debug log. stderr_thread.join(timeout=1) - debug_bytes = self.stderr.getvalue() + debug_bytes = stderr.getvalue() debug_log = read_debug_text(debug_bytes)[:MAX_CONVERSION_LOG_CHARS] incomplete = "(incomplete)\n" if stderr_thread.is_alive() else "" @@ -371,14 +371,14 @@ class IsolationProvider(ABC): ) def start_stderr_thread( - self, process: subprocess.Popen + self, process: subprocess.Popen, stderr: IO[bytes] ) -> Optional[threading.Thread]: """Start a thread to read stderr from the process""" def _stream_stderr(process_stderr: IO[bytes]) -> None: try: for line in process_stderr: - self.stderr.write(line) + stderr.write(line) except (ValueError, IOError) as e: log.debug(f"Stderr stream closed: {e}") diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 7200a39..0c3eb59 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -125,7 +125,7 @@ class Container(IsolationProvider): with subprocess.Popen( [container_runtime, "image", "ls"], stdout=subprocess.DEVNULL, - stderr=subprocess.PIPE, + stderr=self.proc_stderr, startupinfo=get_subprocess_startupinfo(), ) as p: _, stderr = p.communicate() diff --git a/dangerzone/logic.py b/dangerzone/logic.py index 0ee17f6..eb588b9 100644 --- a/dangerzone/logic.py +++ b/dangerzone/logic.py @@ -1,7 +1,6 @@ import concurrent.futures import json import logging -from io import StringIO from typing import Callable, List, Optional import colorama