Commit 7ea925d
authored
[mypyc] Fix exception swallowing in async try/finally blocks with await (#19353)
When a try/finally block in an async function contains an await statement
in the finally block, exceptions raised in the try block are silently
swallowed if a context switch occurs. This happens because mypyc stores
exception information in registers that don't survive across await points.
The Problem:
- mypyc's transform_try_finally_stmt uses error_catch_op to save exceptions
- to a register, then reraise_exception_op to restore from that register
- When await causes a context switch, register values are lost
- The exception information is gone, causing silent exception swallowing
The Solution:
- Add new transform_try_finally_stmt_async for async-aware exception handling
- Use sys.exc_info() to preserve exceptions across context switches instead
- of registers
- Check error indicator first to handle new exceptions raised in finally
- Route to async version when finally block contains await expressions
Implementation Details:
- transform_try_finally_stmt_async uses get_exc_info_op/restore_exc_info_op
- which work with sys.exc_info() that survives context switches
- Proper exception priority: new exceptions in finally replace originals
- Added has_await_in_block helper to detect await expressions
Test Coverage:
Added comprehensive async exception handling tests:
- testAsyncTryExceptFinallyAwait: 8 test cases covering various scenarios
- Simple try/finally with exception and await
- Exception caught but not re-raised
- Exception caught and re-raised
- Different exception raised in except
- Try/except inside finally block
- Try/finally inside finally block
- Control case without await
- Normal flow without exceptions
- testAsyncContextManagerExceptionHandling: Verifies async with still works
- Basic exception propagation
- Exception in **aexit** replacing original
See mypyc/mypyc#1114.1 parent 02c9766 commit 7ea925d
2 files changed
Lines changed: 346 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| 108 | + | |
107 | 109 | | |
108 | 110 | | |
109 | 111 | | |
| |||
683 | 685 | | |
684 | 686 | | |
685 | 687 | | |
686 | | - | |
| 688 | + | |
687 | 689 | | |
688 | 690 | | |
689 | 691 | | |
| |||
719 | 721 | | |
720 | 722 | | |
721 | 723 | | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
722 | 836 | | |
723 | 837 | | |
724 | 838 | | |
| |||
727 | 841 | | |
728 | 842 | | |
729 | 843 | | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
730 | 855 | | |
731 | 856 | | |
732 | 857 | | |
| |||
737 | 862 | | |
738 | 863 | | |
739 | 864 | | |
740 | | - | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
741 | 873 | | |
742 | 874 | | |
743 | 875 | | |
| |||
828 | 960 | | |
829 | 961 | | |
830 | 962 | | |
| 963 | + | |
831 | 964 | | |
832 | 965 | | |
833 | 966 | | |
| |||
0 commit comments