hynek/stamina

Prevent exponential retry explosion

hynek opened this issue · 0 comments

hynek commented

Originally, I wanted to get this into 23.1.0, but between work, having a life, and EP preparation I had make a decision.

I think it should be possible with contextvars, but it's a bit more involved, than one would expect.

This is a test that needs to pass:

def test_retry_block_no_recursive_by_default():
    """
    When retrying context managers, don't retry recursively by default.
    """
    inner = outer = 0

    with pytest.raises(ValueError):
        for o_a in stamina.retry_context(
            on=ValueError, attempts=2, wait_max=0
        ):
            with o_a:
                outer += 1
                for i_a in stamina.retry_context(
                    on=ValueError, attempts=2, wait_max=0
                ):
                    with i_a:
                        inner += 1
                        raise ValueError

    assert 2 == inner
    assert 2 == outer