mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-05 05:01:49 +02:00
Report exceptions raised in document conversion
Exceptions raised during the document conversion process would be silently hidden. This was because ThreadPoolExecuter created various threads and hid any exceptions raised. Fixes #309
This commit is contained in:
parent
b9dc882663
commit
78fa35fb64
2 changed files with 24 additions and 4 deletions
|
@ -200,3 +200,6 @@ class Document:
|
||||||
if not isinstance(other, Document):
|
if not isinstance(other, Document):
|
||||||
return False
|
return False
|
||||||
return self.input_filename == other.input_filename
|
return self.input_filename == other.input_filename
|
||||||
|
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash(self.id)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import concurrent.futures
|
|
||||||
import gzip
|
import gzip
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
@ -7,7 +6,9 @@ import platform
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from typing import Callable, List, Optional
|
import traceback
|
||||||
|
from concurrent.futures import Future, ThreadPoolExecutor
|
||||||
|
from typing import Callable, Dict, List, Optional
|
||||||
|
|
||||||
import appdirs
|
import appdirs
|
||||||
import colorama
|
import colorama
|
||||||
|
@ -66,8 +67,24 @@ class DangerzoneCore(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
max_jobs = container.get_max_parallel_conversions()
|
max_jobs = container.get_max_parallel_conversions()
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor:
|
with ThreadPoolExecutor(max_workers=max_jobs) as executor:
|
||||||
executor.map(convert_doc, self.documents)
|
conversions: Dict[Document, Future] = {}
|
||||||
|
|
||||||
|
# Start all parallel conversions
|
||||||
|
for document in self.get_unconverted_documents():
|
||||||
|
conversion = executor.submit(convert_doc, document)
|
||||||
|
conversions[document] = conversion
|
||||||
|
|
||||||
|
# Check the results to raise any exceptions that may have happened
|
||||||
|
for document in conversions:
|
||||||
|
try:
|
||||||
|
conversion = conversions[document]
|
||||||
|
conversion.result()
|
||||||
|
except Exception as e:
|
||||||
|
log.error(
|
||||||
|
f"Something unexpected happened when converting document '{document.id}': {e}"
|
||||||
|
)
|
||||||
|
traceback.print_exception(type(e), e, e.__traceback__)
|
||||||
|
|
||||||
def get_unconverted_documents(self) -> List[Document]:
|
def get_unconverted_documents(self) -> List[Document]:
|
||||||
return [doc for doc in self.documents if doc.is_unconverted()]
|
return [doc for doc in self.documents if doc.is_unconverted()]
|
||||||
|
|
Loading…
Reference in a new issue