From d8c2b3cf37b8588ec36c71ca5d305009fa979b7b Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 12 Sep 2026 14:44:02 -0700 Subject: [PATCH] Enhancement: allow regex timeout configuration --- docs/configuration.md | 8 ++++++++ src/documents/tests/test_regex.py | 7 +++++++ src/paperless/settings/__init__.py | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index e045af077..0068a325d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1209,6 +1209,14 @@ left unassigned, preventing low-confidence guesses from being applied. Defaults to 0.6. +#### [`PAPERLESS_MATCH_REGEX_TIMEOUT_SECONDS=`](#PAPERLESS_MATCH_REGEX_TIMEOUT_SECONDS) {#PAPERLESS_MATCH_REGEX_TIMEOUT_SECONDS} + +: Sets the timeout, in seconds, for regular expression matching. Increase this +value if date parsing or user-defined matching rules time out when processing +long documents, especially on slower hardware. + + Defaults to 0.1 seconds. + #### [`PAPERLESS_DATE_PARSER_LANGUAGES=`](#PAPERLESS_DATE_PARSER_LANGUAGES) {#PAPERLESS_DATE_PARSER_LANGUAGES} : Specifies which language Paperless should use when parsing dates from documents. diff --git a/src/documents/tests/test_regex.py b/src/documents/tests/test_regex.py index 7085199d1..d3aa04be5 100644 --- a/src/documents/tests/test_regex.py +++ b/src/documents/tests/test_regex.py @@ -1,5 +1,6 @@ import pytest import regex +from django.conf import settings from pytest_mock import MockerFixture from documents.regex import safe_regex_finditer @@ -9,6 +10,12 @@ from documents.regex import safe_regex_sub from documents.regex import validate_regex_pattern +def test_regex_timeout_uses_configured_setting() -> None: + from documents.regex import REGEX_TIMEOUT_SECONDS + + assert REGEX_TIMEOUT_SECONDS == settings.MATCH_REGEX_TIMEOUT_SECONDS + + class TestValidateRegexPattern: def test_valid_pattern(self) -> None: validate_regex_pattern(r"\d+") diff --git a/src/paperless/settings/__init__.py b/src/paperless/settings/__init__.py index c64dd811d..2fc845742 100644 --- a/src/paperless/settings/__init__.py +++ b/src/paperless/settings/__init__.py @@ -102,6 +102,10 @@ CLASSIFIER_MATCH_THRESHOLD: Final[float] = get_float_from_env( "PAPERLESS_CLASSIFIER_MATCH_THRESHOLD", 0.6, ) +MATCH_REGEX_TIMEOUT_SECONDS: Final[float] = get_float_from_env( + "PAPERLESS_MATCH_REGEX_TIMEOUT_SECONDS", + 0.1, +) LLM_INDEX_DIR = DATA_DIR / "llm_index" LLM_INDEX_LOCK = LLM_INDEX_DIR / "index.lock" # Cross-process read/write lock guarding the LLM index compaction/migration