Skip to content

Test hangs forever with a specific exception chain #3804

@jcugat

Description

@jcugat

When running a test that raises an Exception with a loop in the __causes__ variable (by using raise A from B), pytest hangs forever but a normal execution succeeds.

This is a minimal example I have created to reproduce this issue:

class SomethingWentWrong(Exception):
    pass


def run_safely(callable):
    """
    Runs the callable and returns it,
    or raises SomethingWentWrong if there is any exception
    """
    try:
        return callable()
    except Exception as e:
        raise SomethingWentWrong() from e


def do_error():
    return 0 / 0


def do_something():
    try:
        return run_safely(lambda: do_error())
    except SomethingWentWrong as exc:
        raise exc.__cause__


if __name__ == "__main__":
    do_something()


def test_bug():
    do_something()

Note inside the except clause in do_something() it is reraising the original clause of the exception, and my guess is that implicitly it is creating a loop there.

A normal execution runs fine and shows the chained exception:

$ python test_bug.py
Traceback (most recent call last):
  File "test_bug.py", line 22, in do_something
    return run_safely(lambda: do_error())
  File "test_bug.py", line 13, in run_safely
    raise SomethingWentWrong() from e
__main__.SomethingWentWrong

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_bug.py", line 30, in <module>
    do_something()
  File "test_bug.py", line 26, in do_something
    raise exc.__cause__
  File "test_bug.py", line 11, in run_safely
    return callable()
  File "test_bug.py", line 22, in <lambda>
    return run_safely(lambda: do_error())
  File "test_bug.py", line 17, in do_error
    return 0 / 0
ZeroDivisionError: division by zero

But when running with pytest it just hangs forever:

$ pytest test_bug.py
=============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.6.6, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: /Users/nova/workspace/sherlock, inifile:
collected 1 item

test_bug.py

Version information:

$ python -V
Python 3.6.6
$ pip list
Package        Version
-------------- -------
atomicwrites   1.1.5
attrs          18.1.0
more-itertools 4.3.0
pip            18.0
pluggy         0.7.1
py             1.5.4
pytest         3.7.1
setuptools     40.0.0
six            1.11.0
$ uname -a
Darwin 10.0.0.10 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: tracebacksrelated to displaying and handling of tracebackstype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions