lightning-thunder icon indicating copy to clipboard operation
lightning-thunder copied to clipboard

in generators (and async generators / coroutines, too), exception handling during yield should be done by the generator

Open t-vi opened this issue 1 year ago • 2 comments

Currently in generators, we catch exceptions during the yield and raise them. This is not correct, we should be setting the error and let the frame do the error handling. In other words, the current implementation ignores try blocks when the error happens during yield.

https://github.com/Lightning-AI/lightning-thunder/blob/d15b64c71aca487397c47452ac6872d853d9acda/thunder/core/interpreter.py#L5888-L5894

This is also the only occasion when Python 3.12 reaches the CLEANUP_THROW opcode.

cc @apaz-cli

t-vi avatar Jul 21 '24 13:07 t-vi

triage review:

  • we can ignore exceptions in some rare generator+exception use cases
  • not an urgent problem; no known use cases
  • erroring loudly would be too painful here; when we look at this let's just fix it
  • we don't have a test in our suite for this kind of thing

tfogal avatar Jul 22 '24 15:07 tfogal

So what needs to be done is in the above snippet to call into run_frame but instead of executing the next instruction to immediately jump to the exception handling. Probably just an if in _run_frame and calling _run_frame in the above snippet instead of raising.

t-vi avatar Jul 23 '24 21:07 t-vi

Not as urgent.

t-vi avatar Aug 11 '25 15:08 t-vi