It is possible to create a loop by setting the __context__ attribute of the raised exception, either explicitly, or implicitly, using "raise ... from ...". Creating a loop can leads to the hanging in infinite loop or crash due to stack overflow. Several years ago we did have issues related to ExitStack and contextmanager() [1] [2], and recently we have found other issue related to asynccontextmanager() [3]. There may be other issues in the stdlib or third-party libraries.

To solve all such issues we can prevent creating the loop in the __context__ setter. Unfortunately there is no way to return an error in PyException_SetContext(), so it is always success. So we should break a loop at some point. There was proposed several options. If we have a chain

    A -> B -> C -> D -> E -> NULL

after raising C in the context of A ("raise C from A") or setting C.__context__ = A we will get a chain:

0. C -> A -> B -> C -> A -> B -> ...

A loop. This is the current behavior.

1. C -> D -> E -> NULL

The operation is no-op. We lose information about exceptions preceding C: A and B.

2. C -> NULL

Lose all context.

3. C -> A -> B -> D -> E -> NULL

C is moved at the front of the chain. No exception is lost, but their order is changed.

4. C -> A -> B -> NULL

Remove C from the chain and rewrite its context. We lose information about the old context of C: D and E.

5. C -> A -> B -> C' -> D -> E -> NULL

Create a copy of C (how?) and replace C with its copy in the chain. But what to do with other references to C?

There may be other options.

We need to solve this issue, otherwise we will continue to stick with other bugs related to the exceptions loop.

[1] https://bugs.python.org/issue25779
[2] https://bugs.python.org/issue25786
[3] https://bugs.python.org/issue40696
[4] https://bugs.python.org/issue25782
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/MHH2BOG2BIK7MLGKZP7Q5S6MQDCJPF3S/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to