FIXUP: Do not attach the stderr to the base object

This commit is contained in:
Alexis Métaireau 2025-01-13 16:08:16 +01:00
parent 3a56f51e94
commit 3b961e6b90
No known key found for this signature in database
GPG key ID: C65C7A89A8FFC56E
3 changed files with 7 additions and 8 deletions

View file

@ -94,8 +94,6 @@ class IsolationProvider(ABC):
self.proc_stderr = subprocess.PIPE self.proc_stderr = subprocess.PIPE
else: else:
self.proc_stderr = subprocess.DEVNULL self.proc_stderr = subprocess.DEVNULL
# Store the proc stderr in memory
self.stderr = BytesIO()
def should_capture_stderr(self) -> bool: def should_capture_stderr(self) -> bool:
return self.debug or getattr(sys, "dangerzone_dev", False) return self.debug or getattr(sys, "dangerzone_dev", False)
@ -335,8 +333,10 @@ class IsolationProvider(ABC):
timeout_force: int = TIMEOUT_FORCE, timeout_force: int = TIMEOUT_FORCE,
) -> Iterator[subprocess.Popen]: ) -> Iterator[subprocess.Popen]:
"""Start a conversion process, pass it to the caller, and then clean it up.""" """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) 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": if platform.system() != "Windows":
assert os.getpgid(p.pid) != os.getpgid( 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. # Wait for the thread to complete. If it's still alive, mention it in the debug log.
stderr_thread.join(timeout=1) 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] debug_log = read_debug_text(debug_bytes)[:MAX_CONVERSION_LOG_CHARS]
incomplete = "(incomplete)\n" if stderr_thread.is_alive() else "" incomplete = "(incomplete)\n" if stderr_thread.is_alive() else ""
@ -371,14 +371,14 @@ class IsolationProvider(ABC):
) )
def start_stderr_thread( def start_stderr_thread(
self, process: subprocess.Popen self, process: subprocess.Popen, stderr: IO[bytes]
) -> Optional[threading.Thread]: ) -> Optional[threading.Thread]:
"""Start a thread to read stderr from the process""" """Start a thread to read stderr from the process"""
def _stream_stderr(process_stderr: IO[bytes]) -> None: def _stream_stderr(process_stderr: IO[bytes]) -> None:
try: try:
for line in process_stderr: for line in process_stderr:
self.stderr.write(line) stderr.write(line)
except (ValueError, IOError) as e: except (ValueError, IOError) as e:
log.debug(f"Stderr stream closed: {e}") log.debug(f"Stderr stream closed: {e}")

View file

@ -125,7 +125,7 @@ class Container(IsolationProvider):
with subprocess.Popen( with subprocess.Popen(
[container_runtime, "image", "ls"], [container_runtime, "image", "ls"],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE, stderr=self.proc_stderr,
startupinfo=get_subprocess_startupinfo(), startupinfo=get_subprocess_startupinfo(),
) as p: ) as p:
_, stderr = p.communicate() _, stderr = p.communicate()

View file

@ -1,7 +1,6 @@
import concurrent.futures import concurrent.futures
import json import json
import logging import logging
from io import StringIO
from typing import Callable, List, Optional from typing import Callable, List, Optional
import colorama import colorama