~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Per-thread lxml parsers, so concurrent HTML extraction cannot corrupt the heap.
Why this exists
---------------
A research run reads several pages at once — one worker thread per page — and
each thread runs the extraction pipeline, which parses HTML through
trafilatura, newspaper4k, readabilipy and justext. All four reach lxml, and
therefore libxml2.
lxml keeps a *per-thread* string dictionary (libxml2's ``xmlDict``). On entry
to a parse it rebinds the parser context's dict to the calling thread's dict
(``_ParserDictionaryContext.initThreadDictRef``, ``lxml/parser.pxi``), and a
thread that has no dict yet **adopts whatever dict the parser is currently
carrying**. Because trafilatura parses through a module-level parser
(``trafilatura.utils.HTML_PARSER``) and newspaper4k/readabilipy parse through
lxml's own module-level ``lxml.html.html_parser``, worker threads end up
adopting *each other's* dict and then interning strings into one ``xmlDict``
concurrently. ``xmlDict`` is not thread-safe, so its hash table gets corrupted
and the interpreter dies with SIGABRT — ``double free or corruption (out)``
raised from inside ``htmlParseDocument``, taking every user's session and all
in-flight research down with it.
lxml's own per-parser lock does not prevent this: the racing parses run on
*different* parser objects that merely share a dict, so the lock is never
contended by them.
What this does
--------------
lxml's FAQ prescribes "create a parser for each thread yourself". Each library
needs a different route to get there:
* ``lxml.html.fromstring`` is wrapped so that a call passing no explicit
``parser=`` receives *this thread's* parser rather than the process-global
one. newspaper4k (``newspaper/parsers.py``) and readabilipy
(``readabilipy/extractors/extract_element.py``) both call it as a module
attribute, so the wrapper reaches them. Anything else in the process that
calls ``lxml.html.fromstring`` is covered for free.
* trafilatura cannot be reached that way — it ``from``-imports ``fromstring``
and always passes its own ``HTML_PARSER`` explicitly. Instead callers hand
it an already-parsed tree via :func:`parse_for_trafilatura`; its
``load_html`` accepts an ``HtmlElement`` directly.
* justext already builds a fresh parser per call and needs nothing.
Extraction output was verified byte-identical before and after this change on
the three real pages from the crash (3237 / 21578 / 26882 characters).
Note the parsers below are built from ``lxml.html.HTMLParser``, never
``lxml.etree.HTMLParser``. The etree variant yields plain ``_Element`` objects,
which fail trafilatura's ``isinstance(..., HtmlElement)`` check in
``load_html`` — it then returns None and extraction silently yields empty text
rather than raising.
"""
import threading
from typing import Any, Optional
import lxml.html
from loguru import logger
# One parser per thread, per configuration. Parsers are cheap; a worker thread
# builds its two parsers once and reuses them for its lifetime. The
# short-lived per-call threads used to bound the Readability.js subprocess
# (see downloaders/extraction/readability_extractor) do not add to that: they
# run only readabilipy's ``use_readability=True`` branch, which shells out to
# node and never reaches ``lxml.html.fromstring`` — that call lives in the
# pure-Python branch, which runs on the caller's own thread.
_local = threading.local()
_install_lock = threading.Lock()
_installed = False
def get_thread_parser() -> lxml.html.HTMLParser:
"""This thread's general-purpose HTML parser.
Deliberately constructed with lxml's defaults so that replacing the
process-global ``lxml.html.html_parser`` changes *which* parser object is
used and nothing else about how the document is parsed.
"""
parser = getattr(_local, "html_parser", None)
if parser is None:
parser = lxml.html.HTMLParser()
_local.html_parser = parser
return parser
def get_thread_trafilatura_parser() -> lxml.html.HTMLParser:
"""This thread's parser configured exactly as trafilatura configures its own.
Mirrors ``trafilatura.utils.HTML_PARSER``. The options must match, or the
tree we hand trafilatura would not be the tree it would have built for
itself, and extraction output could drift.
"""
parser = getattr(_local, "trafilatura_parser", None)
if parser is None:
parser = lxml.html.HTMLParser(
collect_ids=False,
default_doctype=False,
encoding="utf-8",
remove_comments=True,
remove_pis=True,
)
_local.trafilatura_parser = parser
return parser
def parse_for_trafilatura(html: str) -> Optional[Any]:
"""Parse ``html`` with this thread's parser, for handing to trafilatura.
Returns an ``HtmlElement``, or None if the document could not be parsed —
in which case the caller should fall back to passing the raw string, since
trafilatura's own error handling is more forgiving than ours needs to be.
"""
if not html or not html.strip():
return None
try:
return lxml.html.fromstring(
html, parser=get_thread_trafilatura_parser()
)
except Exception:
logger.debug(
"Per-thread parse failed; caller should fall back to the raw string"
)
return None
def install_per_thread_html_parsers() -> bool:
"""Route parser-less ``lxml.html.fromstring`` calls onto per-thread parsers.
Idempotent and safe to call from any thread. Returns True if this call
installed the wrapper, False if it was already in place.
Must run before worker threads start parsing — importing it at extraction
module import time is early enough, since nothing parses before that.
"""
global _installed
with _install_lock:
if _installed:
return False
original = lxml.html.fromstring
def fromstring(html, base_url=None, parser=None, **kwargs):
if parser is None:
parser = get_thread_parser()
return original(html, base_url=base_url, parser=parser, **kwargs)
# Keep a handle on the original so the wrapper is inspectable and a
# second install() can detect it rather than double-wrapping.
fromstring.__wrapped__ = original
fromstring.__doc__ = (
"lxml.html.fromstring, defaulting to a per-thread parser. "
"See local_deep_research.utilities.lxml_thread_safety."
)
lxml.html.fromstring = fromstring
_installed = True
logger.debug(
"Installed per-thread lxml.html parsers "
"(guards against concurrent xmlDict corruption)"
)
return True
def is_installed() -> bool:
"""Whether the per-thread parser wrapper is currently active."""
return getattr(lxml.html.fromstring, "__wrapped__", None) is not None