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.

18.1 KB
Raw Blame
"""The Readability.js (Node) path must be bounded — see #6209.

readabilipy shells out to a Node subprocess and exposes no timeout, so a
slow or wedged node blocks its caller indefinitely: an unbounded stall for
a research run, and a killed xdist worker under pytest-timeout in CI.

The bound only makes sense when node is actually installed. Without it
readabilipy silently downgrades to its in-process html5lib parser, and
abandoning *that* on overrun just runs the same parse twice, so the
node-absent case must short-circuit instead.
"""

import sys
import threading
import types

import pytest

from local_deep_research.research_library.downloaders.extraction import (
    readability_extractor as re_mod,
)


@pytest.fixture(autouse=True)
def reset_breaker():
    """The breaker is process-global; never let it leak between tests."""
    re_mod._reset_node_breaker()
    yield
    re_mod._reset_node_breaker()


@pytest.fixture
def node_timeout(monkeypatch):
    """Set the Node budget for one test."""

    def _set(value):
        monkeypatch.setattr(re_mod, "NODE_TIMEOUT_SECONDS", value)

    return _set


def _extract(fake, html="<html></html>", node_available=True):
    return re_mod._extract_json(fake, html, node_available=node_available)


class _WedgedNode:
    """A readabilipy stand-in whose node path never returns on its own.

    Every handshake is an :class:`threading.Event`, so tests wait for the
    state they need instead of sleeping for a guessed interval.
    """

    def __init__(self):
        self.release = threading.Event()
        self.entered = threading.Event()
        self.finished = threading.Event()
        self.attempts = []
        self.workers = []
        self._lock = threading.Lock()

    def __call__(self, html, use_readability=False):
        with self._lock:
            self.attempts.append(use_readability)
            if use_readability:
                self.workers.append(threading.current_thread())
        if use_readability:
            self.entered.set()
            # Released by the test; the bound must fire long before this.
            self.release.wait(30)
            self.finished.set()
            return {"content": "<p>too late</p>"}
        return {"content": "<p>pure</p>"}

    @property
    def node_attempts(self):
        return sum(1 for a in self.attempts if a)

    def drain(self, timeout=10):
        """Let every started worker run to completion and join it."""
        self.release.set()
        for worker in list(self.workers):
            worker.join(timeout)
            assert not worker.is_alive(), "wedged worker never finished"


class TestNodeIsBounded:
    def test_zero_budget_never_invokes_node(self, node_timeout):
        """The suite-wide default: no subprocess, pure-Python only."""
        node_timeout(0)
        seen = []

        def fake(html, use_readability=False):
            seen.append(use_readability)
            return {"content": "<p>pure</p>"}

        assert _extract(fake) == {"content": "<p>pure</p>"}
        assert seen == [False], "node path must not be taken at budget 0"

    def test_node_used_when_budget_allows(self, node_timeout):
        node_timeout(30)
        seen = []

        def fake(html, use_readability=False):
            seen.append(use_readability)
            return {"content": "<p>node</p>"}

        assert _extract(fake) == {"content": "<p>node</p>"}
        assert seen == [True]

    def test_overrun_falls_back_instead_of_blocking(self, node_timeout):
        """A slow node must not hold the caller past the budget."""
        node_timeout(0.2)
        fake = _WedgedNode()

        try:
            result = _extract(fake)

            assert result == {"content": "<p>pure</p>"}
            assert fake.attempts == [True, False]
            assert fake.entered.is_set(), "the node path was never entered"
            assert not fake.finished.is_set(), (
                "the caller waited for the wedged node instead of falling "
                "back — this is the #6209 hang"
            )
        finally:
            fake.drain()

    def test_node_errors_still_surface(self, node_timeout):
        """Failures must propagate, not be swallowed by the wrapper."""
        node_timeout(30)

        def fake(html, use_readability=False):
            raise RuntimeError("node exploded")

        with pytest.raises(RuntimeError, match="node exploded"):
            _extract(fake)

    def test_thread_exhaustion_falls_back(self, node_timeout, monkeypatch):
        """``Thread.start`` can raise; that must not fail the extraction."""
        node_timeout(30)
        seen = []

        class Unstartable(threading.Thread):
            def start(self):
                raise RuntimeError("can't start new thread")

        # Shim the module's own ``threading`` reference rather than patching
        # ``threading.Thread`` process-wide, which would break every other
        # thread started while this test runs.
        monkeypatch.setattr(
            re_mod, "threading", types.SimpleNamespace(Thread=Unstartable)
        )

        def fake(html, use_readability=False):
            seen.append(use_readability)
            return {"content": "<p>pure</p>"}

        assert _extract(fake) == {"content": "<p>pure</p>"}
        assert seen == [False]
        # The reserved slot must be handed back, or the cap leaks.
        assert re_mod._node_calls_in_flight == 0

    def test_an_interrupted_join_hands_the_slot_over(
        self, node_timeout, monkeypatch
    ):
        """Ctrl-C while parked in ``join()`` must not eat the reservation.

        The interrupt lands on the caller, not on the worker, so the worker
        is still sitting on its node process: the call has to be recorded
        stranded rather than quietly released. Leaking the reservation
        instead is silent and permanent — enough interrupted joins and
        admission is refused for the rest of the process, with nothing
        logged.
        """
        node_timeout(5)
        fake = _WedgedNode()

        class InterruptedJoin(threading.Thread):
            # Only the join inside the extractor is interrupted; ``drain``
            # still needs a working one to reap the worker afterwards.
            interrupt = True

            def join(self, timeout=None):
                if InterruptedJoin.interrupt:
                    InterruptedJoin.interrupt = False
                    raise KeyboardInterrupt("ctrl-c while parked in join")
                return super().join(timeout)

        monkeypatch.setattr(
            re_mod, "threading", types.SimpleNamespace(Thread=InterruptedJoin)
        )

        try:
            with pytest.raises(KeyboardInterrupt):
                _extract(fake)

            assert fake.entered.wait(10), "the node path was never entered"
            assert re_mod._node_calls_in_flight == 0, (
                "the reserved slot leaked; enough of these and node is "
                "refused for the life of the process"
            )
            assert len(re_mod._stranded_calls) == 1, (
                "the worker is still on its node process, so the call must "
                "be handed to the stranded list, not released"
            )
        finally:
            fake.drain()


class TestNodeAbsentShortCircuits:
    """Without node, readabilipy's "node" path *is* the pure-Python parser.

    Bounding it would abandon an in-process html5lib parse and immediately
    redo it on the caller's thread: twice the CPU and twice the peak memory
    for no timeout benefit. The published runtime image has no node, so this
    is the shipped configuration, not a corner case.
    """

    def test_no_thread_is_spawned_when_node_is_absent(
        self, node_timeout, monkeypatch
    ):
        node_timeout(30)
        seen = []
        constructed = []

        class RecordingThread(threading.Thread):
            def __init__(self, *args, **kwargs):
                constructed.append(kwargs.get("name"))
                super().__init__(*args, **kwargs)

        # Shim the module's own ``threading`` reference rather than reading
        # ``threading.active_count()``, which is process-global: a daemon
        # left behind by an unrelated test can expire mid-assertion.
        monkeypatch.setattr(
            re_mod, "threading", types.SimpleNamespace(Thread=RecordingThread)
        )

        def fake(html, use_readability=False):
            seen.append((use_readability, threading.current_thread()))
            return {"content": "<p>pure</p>"}

        result = _extract(fake, node_available=False)

        assert result == {"content": "<p>pure</p>"}
        assert seen == [(False, threading.current_thread())], (
            "without node the pure-Python parse must run inline on the "
            "caller's thread, with use_readability=False"
        )
        assert constructed == [], (
            "a worker thread was constructed for a call that cannot use node"
        )

    def test_availability_is_probed_via_have_node_and_cached(self, monkeypatch):
        calls = []

        def have_node():
            calls.append(1)
            return False

        stub = types.ModuleType("readabilipy.simple_json")
        stub.have_node = have_node
        monkeypatch.setitem(sys.modules, "readabilipy.simple_json", stub)

        assert re_mod._node_is_available() is False
        assert re_mod._node_is_available() is False
        assert len(calls) == 1, "have_node() must be probed once, then cached"

    def test_a_failing_probe_keeps_the_bounded_path(self, monkeypatch):
        stub = types.ModuleType("readabilipy.simple_json")

        def have_node():
            raise OSError("no /proc")

        stub.have_node = have_node
        monkeypatch.setitem(sys.modules, "readabilipy.simple_json", stub)

        assert re_mod._node_is_available() is True, (
            "an unreadable probe must not silently drop the timeout"
        )


class TestExtractorStillWorks:
    def test_extract_returns_content_without_node(self, node_timeout):
        node_timeout(0)
        html = (
            "<html><body><article><p>"
            + "A" * 200
            + "</p></article></body></html>"
        )

        result = re_mod.ReadabilityExtractor().extract(html)

        assert result is not None
        assert "A" * 50 in result

    def test_extract_falls_back_when_node_wedges(
        self, node_timeout, monkeypatch
    ):
        """End-to-end through ``extract()``, not just ``_extract_json``.

        Reverting the bound makes this hang for 30s (the fake's own ceiling)
        instead of returning, so it pins the caller-visible behaviour.
        """
        node_timeout(0.2)
        fake = _WedgedNode()
        stub = types.ModuleType("readabilipy")
        stub.simple_json_from_html_string = fake
        monkeypatch.setitem(sys.modules, "readabilipy", stub)
        monkeypatch.setattr(re_mod, "_node_is_available", lambda: True)

        try:
            result = re_mod.ReadabilityExtractor().extract("<html>hi</html>")

            assert result == "<p>pure</p>"
            assert fake.attempts == [True, False]
            assert not fake.finished.is_set()
        finally:
            fake.drain()

    def test_empty_html_short_circuits(self, monkeypatch):
        """Blank input must not reach readabilipy at all.

        Characterisation of the pre-existing guard in ``extract()``: it is
        unchanged by this PR and holds on either side of it. It is kept so
        that a future rework of the node plumbing cannot quietly start
        shelling out for whitespace.
        """
        calls = []
        stub = types.ModuleType("readabilipy")

        def fake(html, use_readability=False):
            calls.append(html)
            return {"content": "<p>x</p>"}

        stub.simple_json_from_html_string = fake
        monkeypatch.setitem(sys.modules, "readabilipy", stub)

        assert re_mod.ReadabilityExtractor().extract("   ") is None
        assert calls == []


class TestBreakerBoundsAccumulation:
    """Each timeout strands a node process AND the thread blocked on it, so
    enough stuck calls must stop us reaching for node at all."""

    def test_wedged_calls_stop_spawning_more(self, node_timeout):
        node_timeout(0.1)
        fake = _WedgedNode()

        try:
            for _ in range(re_mod.NODE_MAX_STRANDED_CALLS + 4):
                assert _extract(fake) == {"content": "<p>pure</p>"}

            assert fake.node_attempts == re_mod.NODE_MAX_STRANDED_CALLS, (
                f"node was attempted {fake.node_attempts}x; should stop after "
                f"{re_mod.NODE_MAX_STRANDED_CALLS} calls are stuck"
            )
        finally:
            fake.drain()

    def test_concurrent_callers_cannot_exceed_the_cap(self, node_timeout):
        """Admission must be atomic, not check-then-act.

        Reading the counts and then starting a thread as two steps lets
        every caller through the gate at once: sixteen concurrent callers
        would strand sixteen threads no matter what the bound says.
        """
        node_timeout(0.2)
        width = re_mod.NODE_MAX_CONCURRENT_CALLS * 2
        fake = _WedgedNode()
        barrier = threading.Barrier(width)
        results = []
        results_lock = threading.Lock()

        def caller():
            barrier.wait(10)
            value = _extract(fake)
            with results_lock:
                results.append(value)

        callers = [
            threading.Thread(target=caller, name=f"caller-{i}")
            for i in range(width)
        ]
        try:
            for thread in callers:
                thread.start()
            for thread in callers:
                thread.join(30)
                assert not thread.is_alive(), "a caller blocked on wedged node"

            assert len(results) == width
            assert results == [{"content": "<p>pure</p>"}] * width
            assert fake.node_attempts <= re_mod.NODE_MAX_CONCURRENT_CALLS, (
                f"{width} concurrent callers started {fake.node_attempts} "
                f"node workers; the cap is "
                f"{re_mod.NODE_MAX_CONCURRENT_CALLS}"
            )
        finally:
            fake.drain()
            for thread in callers:
                thread.join(10)

    def test_a_slow_but_finishing_node_does_not_latch_it_off(
        self, node_timeout
    ):
        """Under concurrency every call can blow a wall-clock budget while
        node is merely busy. Those threads finish, so they must not count as
        stranded."""
        node_timeout(0.05)
        rounds = re_mod.NODE_MAX_STRANDED_CALLS + 3
        node_attempts = 0

        for _ in range(rounds):
            # One fake per round: it overruns the budget, then completes
            # before the next admission — a busy node, not a wedged one.
            fake = _WedgedNode()
            try:
                assert _extract(fake) == {"content": "<p>pure</p>"}
                assert fake.entered.wait(10), "node path was never entered"
                node_attempts += fake.node_attempts
            finally:
                fake.drain()

        assert node_attempts == rounds, (
            f"node was attempted only {node_attempts}x; a busy-but-working "
            "node must not disable the path"
        )

    def test_healthy_concurrency_is_not_capped_by_the_stranded_budget(
        self, node_timeout
    ):
        """A healthy node must serve the whole extraction fan-out.

        The stranded budget is a health signal, not a concurrency limit.
        Charging live calls against it throttled healthy concurrency down to
        three, so most of a wide run was silently downgraded to the
        lower-quality pure-Python parser while node was perfectly fine.
        """
        node_timeout(30)
        width = re_mod.NODE_MAX_CONCURRENT_CALLS
        assert width > re_mod.NODE_MAX_STRANDED_CALLS, (
            "the two bounds must differ, or this test proves nothing"
        )

        # Every admitted caller has to be inside the node path at the same
        # time, otherwise a cap of one would satisfy the assertion below.
        together = threading.Barrier(width)
        attempts = []
        results = []
        failures = []
        lock = threading.Lock()

        def fake(html, use_readability=False):
            with lock:
                attempts.append(use_readability)
            if use_readability:
                together.wait(10)
                return {"content": "<p>node</p>"}
            return {"content": "<p>pure</p>"}

        def caller():
            try:
                value = re_mod._extract_json(
                    fake, "<html></html>", node_available=True
                )
            except BaseException as exc:  # reported, not swallowed
                with lock:
                    failures.append(exc)
                return
            with lock:
                results.append(value)

        callers = [
            threading.Thread(target=caller, name=f"healthy-{i}")
            for i in range(width)
        ]
        try:
            for thread in callers:
                thread.start()
            for thread in callers:
                thread.join(30)
                assert not thread.is_alive(), "a caller never returned"
        finally:
            together.abort()

        node_attempts = sum(1 for a in attempts if a)
        assert not failures, f"callers failed: {failures}"
        assert node_attempts == width, (
            f"only {node_attempts} of {width} concurrent callers reached "
            "Readability.js; a healthy node must not be rationed by the "
            "stranded budget"
        )
        assert results == [{"content": "<p>node</p>"}] * width
        assert re_mod._node_calls_in_flight == 0

    def test_it_recovers_once_stuck_calls_clear(self, node_timeout):
        """A latch that never lifts is the wrong shape for a long-lived
        process that hit a busy patch."""
        node_timeout(0.05)
        fake = _WedgedNode()

        try:
            for _ in range(re_mod.NODE_MAX_STRANDED_CALLS):
                _extract(fake)
            assert (
                re_mod._stranded_call_count() >= re_mod.NODE_MAX_STRANDED_CALLS
            )
        finally:
            fake.drain()

        assert re_mod._stranded_call_count() < re_mod.NODE_MAX_STRANDED_CALLS, (
            "stranded calls finished but the breaker stayed latched"
        )