Skip to content

Commit

Permalink
isolation_provider: Get exit code without timing out
Browse files Browse the repository at this point in the history
Get the exit code of the spawned process for the doc-to-pixels phase,
without timing out. More specifically, if the spawned process has not
finished within a generous amount of time (hardcode to 15 seconds),
return UnexpectedConversionError, with a custom message.

This way, the happy path is not affected, and we still make our best to
learn the underlying cause of the I/O error.
  • Loading branch information
apyrgio committed Apr 24, 2024
1 parent 171a7ec commit cd4cbdb
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions dangerzone/isolation_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PIXELS_TO_PDF_LOG_START = "----- PIXELS TO PDF LOG START -----"
PIXELS_TO_PDF_LOG_END = "----- PIXELS TO PDF LOG END -----"

TIMEOUT_EXCEPTION = 15

def read_bytes(f: IO[bytes], size: int, exact: bool = True) -> bytes:
"""Read bytes from a file-like object."""
Expand Down Expand Up @@ -174,9 +175,23 @@ def print_progress(
if self.progress_callback:
self.progress_callback(error, text, percentage)

def get_proc_exception(self, p: subprocess.Popen) -> Exception:
def get_proc_exception(
self, p: subprocess.Popen, timeout: int = TIMEOUT_EXCEPTION
) -> Exception:
"""Returns an exception associated with a process exit code"""
error_code = p.wait(3)
try:
error_code = p.wait(timeout)
except subprocess.TimeoutExpired:
return errors.UnexpectedConversionError(
"Encountered an I/O error during document to pixels conversion,"
f" but the conversion process is still running after {timeout} seconds"
f" (PID: {p.pid})"
)
except Exception:
return errors.UnexpectedConversionError(
"Encountered an I/O error during document to pixels conversion,"
f" but the status of the conversion process is unknown (PID: {p.pid})"
)
return errors.exception_from_error_code(error_code)

@abstractmethod
Expand Down

0 comments on commit cd4cbdb

Please sign in to comment.