Backtestspytestrun-20261106-234554#10745

Issue #10745 Merged fix PR #14903 Run 6 Nov 2026 · run-20261106-234554 pytest-dev/pytest

Test with infinite recursion error slows down pytest even if the exception is not shown to the user (because it’s marked as xfail).

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

The issue identifies the root cause explicitly: `TestReport.from_item_and_call()` runs `repr_failure()` for an xfail test whose traceback is never shown. Patch A fixes exactly that. A new `pytest_runtest_makereport` implementation in the skipping plugin detects that a failure will be reported as an expected failure (honouring `--runxfail`, `--xfail-tb`, `raises=` matching, imperative `pytest.xfail`, and the cheap no-pytrace `fail.Exception` case) and builds the report with a cheap `(path, lineno, message)` longrepr instead of walking the traceback. That tuple form mirrors what pytest already uses for skipped reports, so it is consistent with existing conventions. The xfail-matching logic is refactored into `_xfail_raises_matches` and reused by the existing wrapper, avoiding duplicated semantics. Its tests are strong: a conftest that makes any failure formatting raise, so every scenario (RecursionError, mark with/without raises, imperative xfail in setup/call/teardown, unittest expectedFailure, subtests) would fail before and pass after, plus a test that unexpected failures, `--xfail-tb` and `--runxfail` still format. Independent tests confirm: all three pass with A, with timing equal to the short-traceback case.

Patch B never addresses the root cause. It still formats the traceback for xfail tests; it just makes formatting cheaper by memoizing statement line numbers on the AST node and by truncating undetectable recursions to the first and last 10 frames. These are reasonable general optimizations, and the second one changes user-visible output for ordinary (non-xfail) RecursionError failures, which is beyond the issue's ask and carries some risk of hiding relevant frames. The independent tests show B cuts the time from roughly 0.21s to 0.04s but still leaves the xfail RecursionError report about 25x slower than a short-traceback xfail, so all three tests still fail. B's tests are fine for what they cover but verify the optimizations, not the reported xfail slowdown.

A resolves the issue at its root with thorough tests and minimal scope; B masks part of the symptom and adds an unrelated output change. A wins clearly.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

16m 22s

Wall-clock time in the container

Full test suite

No new failures

873 passed · 15 skipped · 888 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

253vs 103 merged

Changed lines, ours vs merged

In the following code, calling f() fails with a RecursionError. Because test_recursion is marked as xfail, the test suite still passes.

import pytest

def f(x):
    f(x + 1)

@pytest.mark.xfail
def test_recursion():
    f(0)

Running with --durations 10 -vv gives the following output:

test_recursion.py::test_recursion XFAIL                                 [100%]

============================ slowest 10 durations =============================
0.00s call     test_recursion.py::test_recursion
0.00s teardown test_recursion.py::test_recursion
0.00s setup    test_recursion.py::test_recursion
============================= 1 xfailed in 0.41s ==============================

When commenting out the body of test_recursion():

test_recursion.py::test_recursion XPASS                                 [100%]

============================ slowest 10 durations =============================
0.00s setup    test_recursion.py::test_recursion
0.00s call     test_recursion.py::test_recursion
0.00s teardown test_recursion.py::test_recursion
============================= 1 xpassed in 0.01s ==============================

When putting the body of test_recursion() in an exception handler that suppresses the exception:

test_recursion.py::test_recursion XPASS                                 [100%]

============================ slowest 10 durations =============================
0.00s call     test_recursion.py::test_recursion
0.00s setup    test_recursion.py::test_recursion
0.00s teardown test_recursion.py::test_recursion
============================= 1 xpassed in 0.01s ==============================

Pytest takes much longer when the test function raises the exception, even though the test function itself completes quickly in all cases and the exception is not shown to the user if xfailing.

One issue is that TestReport.from_item_and_call() calls Function.repr_failure() (which is slow because of the long traceback) although the error is never shown to the user.

Another issue is that the durations don’t include the part of pytest where the majority of the time is spent. When trying to find why my test suite was slow, --durations didn’t help. It’s probably meant to help finding slow tests instead of performance problems in pytest. Still, I think it could be worthwhile to include timings for some of the code outside of the actual test (like for example generating the test report), as it’s not reasonable to expect users to profile pytest when running into a slowdown like this.

Output of pip list:

attrs          22.2.0
exceptiongroup 1.1.0
iniconfig      2.0.0
packaging      23.0
pip            22.3.1
pluggy         1.0.0
pytest         7.2.1
setuptools     66.0.0
tomli          2.0.1
wheel          0.38.4

The two patches

Our patch+241 −12
changelog/10671.improvement.rstnew file mode 100644index 000000000..333be813d@@ -0,0 +1,2 @@+The traceback of a test failing as expected (``xfail``) is no longer formatted unless it is shown with ``--xfail-tb``, which could make pytest very slow, for example for a ``RecursionError`` with a deep traceback.+For such tests, ``report.longrepr`` is now a ``(path, lineno, message)`` tuple describing where the exception was raised.src/_pytest/reports.pyindex 72fe10e96..c97f3dfe0 100644@@ -269,6 +269,19 @@ def _format_failed_longrepr(     return longrepr  +def _format_crash_location_longrepr(+    excinfo: ExceptionInfo[BaseException],+) -> tuple[str, int, str] | str:+    """Cheap alternative to ``_format_failed_longrepr``, which only describes+    the location and message of the exception, without walking (and rendering)+    the whole traceback."""+    reprcrash = excinfo._getreprcrash()+    if reprcrash is None:+        # All traceback entries are hidden.+        return excinfo.exconly(tryshort=True)+    return (reprcrash.path, reprcrash.lineno, reprcrash.message)++ def _format_exception_group_all_skipped_longrepr(     item: Item,     excinfo: ExceptionInfo[BaseExceptionGroup[BaseException | BaseExceptionGroup]],@@ -387,6 +400,20 @@ class TestReport(BaseReport):         :param item: The item.         :param call: The call info.         """+        return cls._from_item_and_call(item, call, format_failure=True)++    @classmethod+    def _from_item_and_call(+        cls, item: Item, call: CallInfo[None], *, format_failure: bool+    ) -> TestReport:+        """Like :meth:`from_item_and_call`.++        If ``format_failure`` is False, a failure is not formatted with the+        (potentially very slow) ``item.repr_failure()``; instead the longrepr+        is the ``(path, lineno, message)`` location of the crash. This is used+        for failures which are expected (xfail) and whose traceback is+        therefore not shown to the user.+        """         when = call.when         # Remove "collect" from the Literal type -- only for collection calls.         assert when != "collect"@@ -435,7 +462,10 @@ class TestReport(BaseReport):                 longrepr = _format_exception_group_all_skipped_longrepr(item, excinfo)             else:                 outcome = "failed"-                longrepr = _format_failed_longrepr(item, call, excinfo)+                if format_failure:+                    longrepr = _format_failed_longrepr(item, call, excinfo)+                else:+                    longrepr = _format_crash_location_longrepr(excinfo)         for rwhen, key, content in item._report_sections:             sections.append((f"Captured {key} {rwhen}", content))         return cls(src/_pytest/skipping.pyindex aa97e3b7d..25610c267 100644@@ -11,6 +11,7 @@ import platform import sys import traceback +from _pytest._code import ExceptionInfo from _pytest.config import Config from _pytest.config import hookimpl from _pytest.config.argparsing import Parser@@ -273,6 +274,59 @@ def pytest_runtest_call(item: Item) -> Generator[None]:             item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)  +def _xfail_raises_matches(xfailed: Xfail, exc: BaseException) -> bool:+    """Whether the exception raised by an xfail-marked test is an expected one."""+    raises = xfailed.raises+    return (+        raises is None+        or (isinstance(raises, type | tuple) and isinstance(exc, raises))+        or (isinstance(raises, AbstractRaises) and raises.matches(exc))+    )+++def _is_expected_xfail_exception(item: Item, call: CallInfo[None]) -> bool:+    """Whether a failure caused by the exception raised by ``call`` (if any)+    will be reported as an expected failure (xfailed) by+    ``pytest_runtest_makereport`` below.++    Skip exceptions are not considered here: they are reported as skipped+    before ever being treated as failures.+    """+    if item.config.option.runxfail:+        return False+    excinfo = call.excinfo+    if not isinstance(excinfo, ExceptionInfo):+        return False+    exc = excinfo.value+    if isinstance(exc, xfail.Exception):+        return True+    xfailed = item.stash.get(xfailed_key, None)+    return xfailed is not None and _xfail_raises_matches(xfailed, exc)+++@hookimpl(specname="pytest_runtest_makereport")+def pytest_runtest_makereport_for_expected_xfail(+    item: Item, call: CallInfo[None]+) -> TestReport | None:+    """Create the report of a test which failed as expected (xfail) without+    formatting the traceback of the failure, which can be very slow (e.g. for+    a ``RecursionError``) and is not shown to the user anyway (#10671).++    The traceback is still formatted when requested with ``--xfail-tb``.+    """+    if item.config.getoption("xfail_tb", False):+        return None+    if not _is_expected_xfail_exception(item, call):+        return None+    assert call.excinfo is not None+    exc = call.excinfo.value+    if isinstance(exc, fail.Exception) and not exc.pytrace:+        # Formatted cheaply anyway (only the message, see ``_repr_failure_py``),+        # e.g. ``xfail(run=False)``.+        return None+    return TestReport._from_item_and_call(item, call, format_failure=False)++ @hookimpl(wrapper=True) def pytest_runtest_makereport(     item: Item, call: CallInfo[None]@@ -287,17 +341,7 @@ def pytest_runtest_makereport(         rep.outcome = "skipped"     elif not rep.skipped and xfailed:         if call.excinfo:-            raises = xfailed.raises-            if raises is None or (-                (-                    isinstance(raises, type | tuple)-                    and isinstance(call.excinfo.value, raises)-                )-                or (-                    isinstance(raises, AbstractRaises)-                    and raises.matches(call.excinfo.value)-                )-            ):+            if _xfail_raises_matches(xfailed, call.excinfo.value):                 rep.outcome = "skipped"                 rep.wasxfail = xfailed.reason             else:testing/test_skipping.pyindex fbe196915..a451c09f6 100644@@ -1567,3 +1567,156 @@ def test_exit_with_reason_works_ok(pytester: Pytester) -> None:     )     result = pytester.runpytest(p)     result.stdout.fnmatch_lines("*_pytest.outcomes.Exit: foo*")+++class TestXFailFailureNotFormatted:+    """The traceback of an expected failure (xfail) is not shown to the user, so+    it must not be formatted, which can be very slow, e.g. for a RecursionError+    (#10671)."""++    # Formatting a failure raises, so any test whose failure gets formatted+    # results in an INTERNALERROR.+    CONFTEST = """+        import pytest+        from _pytest.nodes import Node++        def _repr_failure_py(self, excinfo, style=None):+            raise RuntimeError("failure of {} was formatted".format(self.nodeid))++        @pytest.fixture(autouse=True)+        def _no_formatting(monkeypatch):+            monkeypatch.setattr(Node, "_repr_failure_py", _repr_failure_py)+    """++    def test_recursion_error(self, pytester: Pytester) -> None:+        pytester.makepyfile(+            """+            import pytest++            def f(x):+                f(x + 1)++            @pytest.mark.xfail+            def test_recursion():+                f(0)+            """+        )+        pytester.makeconftest(self.CONFTEST)+        reprec = pytester.inline_run()+        reprec.assertoutcome(skipped=1)+        (rep,) = (r for r in reprec.getreports() if r.when == "call")+        assert rep.wasxfail == ""+        path, lineno, message = rep.longrepr+        assert path == str(pytester.path / "test_recursion_error.py")+        assert lineno == 4+        assert message.startswith("RecursionError: maximum recursion depth exceeded")++    @pytest.mark.parametrize(+        "mark", ["@pytest.mark.xfail", "@pytest.mark.xfail(raises=ValueError)"]+    )+    def test_xfail_mark(self, pytester: Pytester, mark: str) -> None:+        pytester.makepyfile(+            f"""+            import pytest++            {mark}+            def test_func():+                raise ValueError("oops")+            """+        )+        pytester.makeconftest(self.CONFTEST)+        result = pytester.runpytest("-rx")+        result.stdout.fnmatch_lines(["XFAIL test_xfail_mark.py::test_func"])+        result.stdout.no_fnmatch_line("*INTERNALERROR*")+        result.assert_outcomes(xfailed=1)++    @pytest.mark.parametrize("when", ["setup", "call", "teardown"])+    def test_imperative_xfail(self, pytester: Pytester, when: str) -> None:+        pytester.makepyfile(+            f"""+            import pytest++            @pytest.fixture+            def fix():+                if {when!r} == "setup":+                    pytest.xfail("xfail in setup")+                yield+                if {when!r} == "teardown":+                    pytest.xfail("xfail in teardown")++            def test_func(fix):+                if {when!r} == "call":+                    pytest.xfail("xfail in call")+            """+        )+        pytester.makeconftest(self.CONFTEST)+        result = pytester.runpytest("-rx")+        result.stdout.fnmatch_lines([f"XFAIL *test_func* - xfail in {when}"])+        result.stdout.no_fnmatch_line("*INTERNALERROR*")++    def test_unittest_expected_failure(self, pytester: Pytester) -> None:+        pytester.makepyfile(+            """+            import unittest++            class MyTestCase(unittest.TestCase):+                @unittest.expectedFailure+                def test_func(self):+                    assert False+            """+        )+        pytester.makeconftest(self.CONFTEST)+        result = pytester.runpytest()+        result.stdout.no_fnmatch_line("*INTERNALERROR*")+        result.assert_outcomes(xfailed=1)++    def test_subtest_xfail(self, pytester: Pytester) -> None:+        pytester.makepyfile(+            """+            import pytest++            def test_func(subtests):+                with subtests.test("sub"):+                    pytest.xfail("xfail sub")+            """+        )+        pytester.makeconftest(self.CONFTEST)+        result = pytester.runpytest("-v")+        result.stdout.fnmatch_lines(["*::test_func SUBXFAIL[[]sub[]] (xfail sub)*"])+        result.stdout.no_fnmatch_line("*INTERNALERROR*")++    def test_failure_is_formatted_when_shown(self, pytester: Pytester) -> None:+        """Failures which are not expected, or whose traceback is requested,+        are still formatted."""+        pytester.makepyfile(+            """+            import pytest++            @pytest.mark.xfail(raises=TypeError)+            def test_unexpected():+                raise ValueError("unexpected")++            @pytest.mark.xfail+            def test_expected():+                raise ValueError("expected")+            """+        )+        result = pytester.runpytest()+        result.stdout.fnmatch_lines(+            ["*ValueError: unexpected", "*1 failed, 1 xfailed*"]+        )++        result = pytester.runpytest("--xfail-tb")+        result.stdout.fnmatch_lines(+            [+                "*= XFAILURES =*",+                "*_ test_expected _*",+                '*raise ValueError("expected")',+                "E*ValueError: expected",+            ]+        )++        result = pytester.runpytest("--runxfail")+        result.stdout.fnmatch_lines(+            ["*ValueError: unexpected", "*ValueError: expected", "*2 failed*"]+        ) 
Merged fix · PR #14903+100 −3
changelog/10745.improvement.1.rstnew file mode 100644index 000000000..0732ce842@@ -0,0 +1,3 @@+The line numbers of a module's statements are now computed once per traceback+instead of once per traceback entry, which noticeably speeds up rendering+failures raised from large test modules.changelog/10745.improvement.2.rstnew file mode 100644index 000000000..070043470@@ -0,0 +1,5 @@+A ``RecursionError`` whose recursion origin cannot be located now shows only the+first and last 10 stack frames, matching what pytest already did when locating+the origin failed outright. Previously the full traceback -- often around a+thousand near-identical frames -- was rendered, which was both slow and+unreadable. Pass ``--full-trace`` to see every frame.src/_pytest/_code/code.pyindex c2de27bbb..3da398477 100644@@ -1167,10 +1167,10 @@ class ExceptionInfoFormatter:         TypeError), in which case we do our best to warn the user of the         error and show a limited traceback.         """+        max_frames = 10         try:             recursionindex = traceback.recursionindex()         except Exception as e:-            max_frames = 10             extraline: str | None = (                 "!!! Recursion error detected, but an error occurred locating the origin of recursion.\n"                 "  The following exception happened when comparing locals in the stack frame:\n"@@ -1184,6 +1184,18 @@ class ExceptionInfoFormatter:             if recursionindex is not None:                 extraline = "!!! Recursion detected (same locals & position)"                 traceback = traceback[: recursionindex + 1]+            elif self.tbfilter is not False and len(traceback) > 2 * max_frames:+                # The origin of the recursion could not be pinned down (the frames+                # differ), but rendering hundreds of near-identical entries is both+                # slow and unreadable -- show the ends only, as above.+                extraline = (+                    "!!! Recursion error detected, but the origin of the recursion "+                    "could not be located.\n"+                    f"  Displaying first and last {max_frames} stack frames out of {len(traceback)}.\n"+                    "  Pass `--full-trace` to see all frames."+                )+                # Type ignored for the same reason as above.+                traceback = traceback[:max_frames] + traceback[-max_frames:]  # type: ignore             else:                 extraline = None src/_pytest/_code/source.pyindex b7cae2662..0a97962fb 100644@@ -166,10 +166,19 @@ def deindent(lines: Iterable[str]) -> list[str]:     return textwrap.dedent("\n".join(lines)).splitlines()  -def get_statement_startend2(lineno: int, node: ast.AST) -> tuple[int, int | None]:+def _statement_linenos(node: ast.AST) -> list[int]:+    """Sorted 0-based line numbers of all statements below ``node``.++    Walking the tree is proportional to the size of the whole module, while a+    traceback asks for many line numbers of the same module -- so the result is+    memoized on the node, which shares the lifetime of the caller's ast cache.+    """+    values: list[int] | None = getattr(node, "_pytest_statement_linenos", None)+    if values is not None:+        return values     # Flatten all statements and except handlers into one lineno-list.     # AST's line numbers start indexing at 1.-    values: list[int] = []+    values = []     for x in ast.walk(node):         if isinstance(x, ast.stmt | ast.ExceptHandler):             # The lineno points to the class/def, so need to include the decorators.@@ -183,6 +192,15 @@ def get_statement_startend2(lineno: int, node: ast.AST) -> tuple[int, int | None                     # Treat the finally/orelse part as its own statement.                     values.append(val[0].lineno - 1 - 1)     values.sort()+    try:+        node._pytest_statement_linenos = values  # type: ignore[attr-defined]+    except AttributeError:  # pragma: no cover - defensive+        pass+    return values+++def get_statement_startend2(lineno: int, node: ast.AST) -> tuple[int, int | None]:+    values = _statement_linenos(node)     insert_index = bisect_right(values, lineno)     if insert_index == 0:         return 0, Nonetesting/code/test_excinfo.pyindex af32f3506..f4cc6d7e7 100644@@ -1876,6 +1876,47 @@ def test_exception_repr_extraction_error_on_recursion():     )  +@pytest.mark.usefixtures("limited_recursion_depth")+def test_undetectable_recursion_is_truncated() -> None:+    """A recursion whose origin cannot be located is shown ends-only (#10745)."""++    def f(x):+        # The locals differ in every frame, so recursionindex() finds nothing.+        f(x + 1)++    with pytest.raises(RecursionError) as excinfo:+        f(0)++    p = ExceptionInfoFormatter(style="long", tbfilter=True)+    traceback, extraline = p._truncate_recursive_traceback(excinfo.traceback)+    assert len(traceback) == 20+    assert extraline is not None+    matcher = LineMatcher(extraline.splitlines())+    matcher.fnmatch_lines(+        [+            "!!! Recursion error detected, but the origin of the recursion could not be located.",+            "*Displaying first and last 10 stack frames out of *",+            "*--full-trace*",+        ]+    )+++@pytest.mark.usefixtures("limited_recursion_depth")+def test_undetectable_recursion_kept_with_full_trace() -> None:+    """``--full-trace`` (tbfilter=False) still renders every frame (#10745)."""++    def f(x):+        f(x + 1)++    with pytest.raises(RecursionError) as excinfo:+        f(0)++    p = ExceptionInfoFormatter(style="long", tbfilter=False)+    traceback, extraline = p._truncate_recursive_traceback(excinfo.traceback)+    assert len(traceback) == len(excinfo.traceback)+    assert extraline is None++ @pytest.mark.usefixtures("limited_recursion_depth") def test_no_recursion_index_on_recursion_error():     """testing/code/test_source.pyindex 5d073af2a..b2f86024f 100644@@ -1,6 +1,7 @@ # mypy: allow-untyped-defs from __future__ import annotations +import ast import inspect import linecache from pathlib import Path@@ -706,3 +707,20 @@ def test_patched_compile() -> None:      with patch("builtins.compile", new=patched_compile2):         Source(patched_compile2).getstatement(1)+++def test_statement_linenos_are_memoized_per_node() -> None:+    """The statement index of a module is reused across traceback entries (#10745)."""+    from _pytest._code.source import _statement_linenos+    from _pytest._code.source import get_statement_startend2++    node = ast.parse("x = 1\nif x:\n    y = 2\n")+    values = _statement_linenos(node)+    assert values == [0, 1, 2]+    # A second call reuses the very same list instead of walking the tree again.+    assert _statement_linenos(node) is values+    assert get_statement_startend2(1, node) == (1, 2)++    other = ast.parse("z = 3\n")+    assert _statement_linenos(other) == [0]+    assert _statement_linenos(node) is values