Repository navigation
Description
If a keyword or a library import fails with an exception whose chain of __cause__ and __context__ contains a cycle, the whole execution crashes with RecursionError. Python does not prevent such chains, and its own traceback printing handles them. In Robot Framework, one such failure ends the execution instead of failing one keyword.
The following is a minimal example. raise error from error is not useful code, but it is the shortest way to get a cycle, because the error becomes its own cause.
Library Example.py:
def fail_with_cycle():
try:
raise ValueError("boom")
except ValueError as error:
raise error from errorTest example.robot:
*** Settings ***
Library Example.py
*** Test Cases ***
First Test
Fail With Cycle
Second Test
No OperationRunning robot example.robot stops at the first test:
First Test [ ERROR ] Unexpected error: RecursionError: maximum recursion depth exceeded
The exit code is 255, log.html and report.html are not created, and output.xml is not well-formed. Second Test and all teardowns are not run. Neither TRY/EXCEPT nor Run Keyword And Expect Error can catch the failure. A library import that fails like this crashes the execution the same way.
Full traceback
First Test [ ERROR ] Unexpected error: RecursionError: maximum recursion depth exceeded
Traceback (most recent call last):
File ".../Example.py", line 5, in fail_with_cycle
raise error from error
File ".../Example.py", line 3, in fail_with_cycle
raise ValueError("boom")
ValueError: boom
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../site-packages/robot/utils/application.py", line 97, in _execute
rc = self.main(arguments, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/run.py", line 496, in main
result = suite.run(settings)
^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/running/model.py", line 921, in run
self.visit(runner)
File ".../site-packages/robot/model/testsuite.py", line 470, in visit
visitor.visit_suite(self)
File ".../site-packages/robot/model/visitor.py", line 133, in visit_suite
suite.tests.visit(self)
File ".../site-packages/robot/model/itemlist.py", line 112, in visit
item.visit(visitor) # type: ignore
^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/model/testcase.py", line 214, in visit
visitor.visit_test(self)
File ".../site-packages/robot/running/suiterunner.py", line 205, in visit_test
runner.run(data, result)
File ".../site-packages/robot/running/bodyrunner.py", line 52, in run
item.run(result, self._context, self._run, self._templated)
File ".../site-packages/robot/running/model.py", line 173, in run
return KeywordRunner(context, run).run(self, result.body.create_keyword())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/running/bodyrunner.py", line 92, in run
return runner.run(data, result, context, self._run)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/running/librarykeywordrunner.py", line 52, in run
with StatusReporter(data, result, context, run, implementation=kw):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/running/statusreporter.py", line 67, in __exit__
failure = self._get_failure(exc_value, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/running/statusreporter.py", line 104, in _get_failure
if error.traceback:
^^^^^^^^^^^^^^^
File ".../site-packages/robot/utils/error.py", line 79, in traceback
self._traceback = self._format_traceback(self.error)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../site-packages/robot/utils/error.py", line 88, in _format_traceback
self._remove_robot_traces(error)
File ".../site-packages/robot/utils/error.py", line 100, in _remove_robot_traces
self._remove_robot_traces(error.__cause__)
File ".../site-packages/robot/utils/error.py", line 100, in _remove_robot_traces
self._remove_robot_traces(error.__cause__)
File ".../site-packages/robot/utils/error.py", line 100, in _remove_robot_traces
self._remove_robot_traces(error.__cause__)
[Previous line repeated 975 more times]
File ".../site-packages/robot/utils/error.py", line 94, in _remove_robot_traces
while tb and self._is_robot_traceback(tb):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded
Even this line exists in real code: a GitHub code search finds thousands of Python files with raise e from e. It has no visible effect in plain Python, so it easily goes unnoticed.
A more realistic example needs no odd line at all. I ran into this in robotframework-jsonrpcremote, when a connection was reset during a handshake. If a connection is reset, asyncio stores the same ConnectionResetError object in the stream reader and behind StreamWriter.wait_closed(). In this library, a helper wraps the error from the read, and its caller closes the connection the way the asyncio documentation recommends:
Library Client.py:
import asyncio
import contextlib
import socket
import struct
class HandshakeFailed(Exception):
pass
async def _handshake(reader, writer):
writer.write(b"HELLO\n")
await writer.drain()
try:
return await reader.readline()
except OSError as error:
raise HandshakeFailed("Handshake failed") from error
async def _connect(port):
reader, writer = await asyncio.open_connection("127.0.0.1", port)
try:
return await _handshake(reader, writer)
except HandshakeFailed:
writer.close()
with contextlib.suppress(OSError):
await writer.wait_closed() # Raises the same ConnectionResetError again.
raise
async def _reset_connection(reader, writer):
# The server side of this example: with a zero linger time, closing the socket sends a TCP reset.
await reader.readline()
linger = struct.pack("ii", 1, 0)
writer.get_extra_info("socket").setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger)
writer.transport.abort()
async def _main():
server = await asyncio.start_server(_reset_connection, "127.0.0.1", 0)
async with server:
await _connect(server.sockets[0].getsockname()[1])
def connect_to_server():
asyncio.run(_main())Test client.robot:
*** Settings ***
Library Client.py
*** Test Cases ***
First Test
Connect To Server
Second Test
No OperationThis crashes the same way. readline() raises the ConnectionResetError, which becomes the __cause__ of HandshakeFailed. While HandshakeFailed is being handled, wait_closed() raises the same ConnectionResetError object again, and Python sets its __context__ to HandshakeFailed. Python avoids cycles only through __context__, not across __cause__, so the two errors now refer to each other.
I have fixed my library by closing the connection outside the except block, but a problem like this in one library should not end the whole execution.
Python handles such chains. The interactive interpreter prints a normal traceback and goes on:
>>> try:
... raise ValueError("boom")
... except ValueError as error:
... raise error from error
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
raise error from error
File "<stdin>", line 2, in <module>
raise ValueError("boom")
ValueError: boom
>>> print("still running")
still runningtraceback.print_exception(), traceback.format_exception() and logging.exception() do the same. In CPython, TracebackException remembers the errors it has seen and uses a queue instead of recursion, and the C code that prints tracebacks has the same protection.
Other projects handle this as well: pytest stops at an error it has already seen, Django had an infinite loop and now warns with ExceptionCycleWarning, and tenacity hung on raise e from e and now guards against it.
The crash comes from ErrorDetails._remove_robot_traces in robot/utils/error.py, which follows the chain recursively without checking whether it has already visited an error:
def _remove_robot_traces(self, error):
tb = error.__traceback__
while tb and self._is_robot_traceback(tb):
tb = tb.tb_next
error.__traceback__ = tb
if error.__context__:
self._remove_robot_traces(error.__context__)
if error.__cause__:
self._remove_robot_traces(error.__cause__)With ROBOT_INTERNAL_TRACES=1, which skips this function, both examples work as expected: First Test fails with ValueError: boom or HandshakeFailed: Handshake failed, and Second Test passes. traceback.format_exception(), which Robot Framework calls next, has no problem with the cycle.
A long chain without any cycle crashes the same way (I tried 2000 errors), so the fix should not use recursion at all, for example:
def _remove_robot_traces(self, error):
seen = set()
pending = [error]
while pending:
current = pending.pop()
if current is None or id(current) in seen:
continue
seen.add(id(current))
tb = current.__traceback__
while tb and self._is_robot_traceback(tb):
tb = tb.tb_next
current.__traceback__ = tb
pending += [current.__context__, current.__cause__]With this version of the method, patched in at runtime on 7.5, both examples, a failing library import, Run Keyword And Expect Error and the long chain all behave as expected.
This is not a recent regression. _remove_robot_traces has followed the chain since Robot Framework 6.0 (#4476, a5659a4). The symptom is the same as in #5815 and #5819, but the cause is different: the RecursionError comes from handling the failure, not from argument conversion. Current master (d9fcd24) is affected as well, verified with Python 3.12, 3.14 and 3.15.
Robot Framework 7.5 (Python 3.12.16 on linux)
AI/tooling disclosure: This issue was prepared with assistance from Claude Code. I reviewed the result manually, understand the reported problem, and verified it locally.
I'd be happy to make a PR, but only after 7.5.1 is out.
Activity
pekkaklarck commentedon Nov 2, 2026
Crashes are never great, but luckily this requires either really stupid code (
raise e from e) or a special case where arguable the cycle should also be avoided. Definitely worth fixing, but based on the final comment in the description this isn't considered critical for RF 7.5.1.I'm not certain is handling overly long chain of exceptions really worth it. At least a chain of 2000 exceptions sounds like something that cannot happen in real life unless you are actually trying to create problems. Can you @d-biehl test what's the lower limit when problems appear? Also, do you have any even vaguely realistic examples where that could happen in practice?
pekkaklarck commentedon Nov 2, 2026
I added this issue to RF 7.6 scope because we hopefully don't need 7.5.2. If such a release is actually needed, we can add this one to it as well. Regardless the version, the fix can be added immediately.