Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.6 KB
Raw Blame
"""Concurrent HTML extraction must not share lxml parsers between threads.

Regression guard for a SIGABRT that killed the whole server process: three
worker threads extracting different pages at once corrupted libxml2's shared
``xmlDict`` and glibc aborted with ``double free or corruption (out)`` inside
``htmlParseDocument``. Every user's session and all in-flight research died
with it.

Sharing an lxml *parser* across threads is supported (lxml serializes it with a
per-parser lock). The hazard is the ``xmlDict``: on entry to a parse lxml
rebinds the parser context's dict to the calling thread's dict, and a thread
with no dict yet adopts whatever dict the parser is carrying. Threads driving a
module-level parser therefore end up interning into one dict concurrently. The
fix gives each thread its own parser so no adoption happens.

The crash itself cannot be asserted in-process — it is a SIGABRT, not an
exception, and it is rare (5,000+ attempts failed to reproduce it). So these
tests assert the *invariant* that removes the hazard rather than the absence of
the crash.
"""

import threading

import lxml.html

from local_deep_research.utilities import lxml_thread_safety as lts


def test_pipeline_import_installs_per_thread_parsers():
    """Importing the extraction pipeline must arm the wrapper.

    Installation has to happen before any worker thread parses, so it is wired
    to module import rather than left to a caller to remember.
    """
    import local_deep_research.research_library.downloaders.extraction.pipeline  # noqa: F401,E501

    assert lts.is_installed(), (
        "lxml.html.fromstring is not wrapped — parser-less calls from "
        "newspaper4k and readabilipy would fall back to the process-global "
        "lxml.html.html_parser that caused the SIGABRT."
    )


def test_each_thread_gets_its_own_parsers():
    # Keep references to the parser OBJECTS, not their id()s. A thread's
    # thread-local dies with the thread, and CPython promptly reuses the freed
    # address — so comparing id()s collected from exited threads reports every
    # thread sharing one parser even when each had its own.
    parsers = {}
    lock = threading.Lock()

    def collect():
        name = threading.current_thread().name
        general = lts.get_thread_parser()
        trafilatura = lts.get_thread_trafilatura_parser()
        with lock:
            parsers[name] = (general, trafilatura)

    threads = [threading.Thread(target=collect, name=f"t{i}") for i in range(6)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

    general = [p[0] for p in parsers.values()]
    trafilatura = [p[1] for p in parsers.values()]

    assert len({id(p) for p in general}) == len(general), (
        f"{len(general)} threads shared "
        f"{len({id(p) for p in general})} general parsers; each thread must "
        "have its own or they share an xmlDict."
    )
    assert len({id(p) for p in trafilatura}) == len(trafilatura), (
        f"{len(trafilatura)} threads shared "
        f"{len({id(p) for p in trafilatura})} trafilatura parsers; each "
        "thread must have its own."
    )


def test_same_thread_reuses_its_parser():
    """Per-thread must not degrade into per-call — parsers would churn."""
    assert lts.get_thread_parser() is lts.get_thread_parser()
    assert (
        lts.get_thread_trafilatura_parser()
        is lts.get_thread_trafilatura_parser()
    )


def test_explicit_parser_argument_is_respected():
    """The wrapper supplies a default; it must never override the caller.

    Asserted behaviourally: a parser configured to strip comments must still
    strip them when passed explicitly, which it would not if the wrapper
    substituted this thread's default parser.
    """
    html = "<html><body><!-- secret --><p>x</p></body></html>"

    stripping = lxml.html.HTMLParser(remove_comments=True)
    keeping = lxml.html.HTMLParser(remove_comments=False)

    stripped = lxml.html.tostring(
        lxml.html.fromstring(html, parser=stripping)
    ).decode()
    kept = lxml.html.tostring(
        lxml.html.fromstring(html, parser=keeping)
    ).decode()

    assert "secret" not in stripped, (
        "explicit remove_comments=True parser was ignored — the wrapper is "
        "overriding the caller's parser instead of only defaulting it"
    )
    assert "secret" in kept, (
        "explicit remove_comments=False parser was ignored — the wrapper is "
        "overriding the caller's parser instead of only defaulting it"
    )


def test_install_is_idempotent():
    """A second install must not wrap the wrapper (unbounded nesting)."""
    before = lxml.html.fromstring
    assert lts.install_per_thread_html_parsers() is False
    assert lxml.html.fromstring is before


def test_trafilatura_parser_uses_html_element_class():
    """Must be lxml.html's parser, not lxml.etree's.

    lxml.etree.HTMLParser yields plain ``_Element`` objects, which fail
    trafilatura's ``isinstance(..., HtmlElement)`` check in ``load_html``. It
    then returns None and extraction silently produces empty text — no
    exception, so this regresses invisibly.
    """
    tree = lts.parse_for_trafilatura(
        "<html><body><article><p>hello</p></article></body></html>"
    )
    assert isinstance(tree, lxml.html.HtmlElement), (
        f"parse_for_trafilatura returned {type(tree)!r}; trafilatura's "
        "load_html only accepts HtmlElement and would silently extract nothing."
    )


def test_parse_for_trafilatura_returns_none_on_empty_input():
    """Callers fall back to the raw string on None, so empty must not raise."""
    assert lts.parse_for_trafilatura("") is None
    assert lts.parse_for_trafilatura("   ") is None