Use a different base temporary directory for each page count job

This commit is contained in:
Kovid Goyal 2026-01-04 22:34:07 +05:30
parent 3aaa938481
commit 2f3ec7ea25
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 11 deletions

View File

@ -20,7 +20,7 @@ from calibre.ebooks.oeb.polish.container import Container as ContainerBase
from calibre.ebooks.oeb.polish.parsing import decode_xml, parse
from calibre.ebooks.oeb.polish.pretty import NON_NAMESPACED_BLOCK_TAGS
from calibre.ebooks.oeb.polish.toc import get_toc
from calibre.ptempfile import TemporaryDirectory
from calibre.ptempfile import TemporaryDirectory, override_base_dir
from calibre.utils.cleantext import clean_xml_chars
from calibre.utils.ipc import eintr_retry_call
from calibre.utils.logging import DevNull
@ -246,17 +246,18 @@ class Server:
def serve_requests(pipe: Connection) -> None:
executor = ThreadPoolExecutor()
for line in sys.stdin:
for i, line in enumerate(sys.stdin):
path = bytes.fromhex(line.rstrip()).decode()
try:
result = count_pages(path, executor)
except Exception as e:
import traceback
result = str(e), traceback.format_exc()
try:
eintr_retry_call(pipe.send, result)
except EOFError:
break
with TemporaryDirectory(suffix=f'.pc{i}') as base_tdir, override_base_dir(base_tdir):
try:
result = count_pages(path, executor)
except Exception as e:
import traceback
result = str(e), traceback.format_exc()
try:
eintr_retry_call(pipe.send, result)
except EOFError:
break
def worker_main(pipe_fd: int) -> None:

View File

@ -6,6 +6,7 @@ being closed.
'''
import os
import tempfile
from contextlib import contextmanager
from calibre.constants import __appname__, filesystem_encoding, get_windows_temp_path, ismacos, iswindows
from calibre.utils.safe_atexit import remove_dir, remove_file_atexit, remove_folder_atexit, unlink
@ -105,6 +106,16 @@ def reset_base_dir():
_base_dir = None
@contextmanager
def override_base_dir(newval: str) -> None:
global _base_dir
before, _base_dir = _base_dir, newval
try:
yield
finally:
_base_dir = before
def force_unicode(x):
# Cannot use the implementation in calibre.__init__ as it causes a circular
# dependency