From 2149a6fe3be334784236937ca7b714a98c6672c5 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Thu, 5 Feb 2026 10:07:36 +0100 Subject: [PATCH] Memory improvement - Use builtin markupsafe instead of creating a jinja2 template env each time for small strings (#3836) --- changedetectionio/jinja2_custom/safe_jinja.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/changedetectionio/jinja2_custom/safe_jinja.py b/changedetectionio/jinja2_custom/safe_jinja.py index fd5491d41..67f4e57af 100644 --- a/changedetectionio/jinja2_custom/safe_jinja.py +++ b/changedetectionio/jinja2_custom/safe_jinja.py @@ -52,7 +52,13 @@ def render(template_str, **args: t.Any) -> str: return output[:JINJA2_MAX_RETURN_PAYLOAD_SIZE] def render_fully_escaped(content): - env = jinja2.sandbox.ImmutableSandboxedEnvironment(autoescape=True) - template = env.from_string("{{ some_html|e }}") - return template.render(some_html=content) + """ + Escape HTML content safely. + + MEMORY LEAK FIX: Use markupsafe.escape() directly instead of creating + Jinja2 environments (was causing 1M+ compilations per page load). + Simpler, faster, and no concerns about environment state. + """ + from markupsafe import escape + return str(escape(content))