Skip to content

Commit 2bf6ec1

Browse files
committed
fixing thread instead of fork
1 parent 3d11194 commit 2bf6ec1

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "PDFSentinel"
3-
version = "3.0.0"
3+
version = "3.0.1"
44
authors = [
55
{ name="Not Empty Foundation", email="dev@not-empty.org" },
66
]

src/pdfsentinel/sentinel.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import multiprocessing as mp
3+
import threading
34
from queue import Empty
45
from pathlib import Path
56

@@ -245,16 +246,15 @@ def _run_with_timeout(self, worker, worker_args, timeout_seconds):
245246
if float(timeout_seconds) <= 0:
246247
raise ValueError("timeout_seconds must be greater than 0")
247248

248-
start_method = "fork" if "fork" in mp.get_all_start_methods() else "spawn"
249+
if threading.active_count() > 1:
250+
start_method = "spawn"
251+
else:
252+
start_method = "fork" if "fork" in mp.get_all_start_methods() else "spawn"
249253
ctx = mp.get_context(start_method)
250254
result_queue = ctx.Queue()
251255
process = ctx.Process(target=worker, args=(*worker_args, result_queue))
252-
process.start()
253-
process.join(float(timeout_seconds))
254256

255-
if process.is_alive():
256-
process.terminate()
257-
process.join()
257+
if not self._finish_process_with_timeout(process, timeout_seconds):
258258
return None
259259

260260
try:
@@ -267,6 +267,18 @@ def _run_with_timeout(self, worker, worker_args, timeout_seconds):
267267

268268
return payload["result"]
269269

270+
@staticmethod
271+
def _finish_process_with_timeout(process, timeout_seconds):
272+
process.start()
273+
process.join(float(timeout_seconds))
274+
275+
if not process.is_alive():
276+
return True
277+
278+
process.terminate()
279+
process.join()
280+
return False
281+
270282
def _file_analysis_sync(self, file_path, config=None):
271283
cfg = self._merge_config(self.base_config, config or {})
272284
doc = pymupdf.open_document(file_path)

0 commit comments

Comments
 (0)