Fix asyncio compile by youknowone · Pull Request #6739 · RustPython/RustPython
📝 Walkthrough
Walkthrough
The patch modifies exception re-raising behavior within try-finally unwinding paths in the compiler's bytecode generation. It ensures that before emitting a RERAISE instruction, the FinallyEnd fblock is properly popped, the previous exception is explicitly copied from the stack, and the exception context is restored before re-raising to route exceptions to outer handlers correctly.
Changes
| Cohort / File(s) | Summary |
|---|---|
Exception Re-raising in Try-Finally Unwinding crates/codegen/src/compile.rs |
Modified RERAISE logic to pop FinallyEnd fblock prior to re-raising; added explicit stack manipulation (COPY index 2) to restore previous exception state; inserted POP_EXCEPT to restore exception context, ensuring exceptions route to outer exception handlers rather than intermediate cleanup blocks. Changes applied symmetrically across both normal and exception-path handling branches. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~22 minutes
Possibly related PRs
#6545: Modifies exception re-raise and unwind handling in compile.rs with adjustments to reraise paths, fblocks, and stack state management around finally blocks.
Poem
🐰 In finally blocks where exceptions dwell,
We pop the cleanup, then reraise bell,
Stack state aligned, the handlers now see,
Exceptions escape where they ought to be!
No more loops spinning, just control flow clean, ✨
🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. | |
| Title check | ❓ Inconclusive | The title is vague and generic, using non-descriptive language that doesn't clarify what specific asyncio compilation issue is being fixed. | Revise the title to be more specific, such as 'Fix exception routing in try-finally with async/await' or 'Fix CancelledError re-raising in asyncio Condition.wait', to better convey the nature of the fix. |
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Linked Issues check | ✅ Passed | The code changes directly address the core issue: fixing exception routing so that re-raised exceptions in finally blocks proceed to outer handlers rather than re-entering cleanup loops, matching CPython behavior. |
| Out of Scope Changes check | ✅ Passed | All changes in compile.rs are focused on fixing the exception routing logic in try-finally-await control flow, directly addressing the linked issues without introducing unrelated modifications. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
📜 Recent review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/codegen/src/compile.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style usingcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/codegen/src/compile.rs
🔇 Additional comments (2)
crates/codegen/src/compile.rs (2)
2550-2566: Good: popFinallyEndbeforeRERAISEto avoid cleanup re-entry.
This ordering plus theCopy/PopExceptrestore keeps the rethrow routed to the outer handler as intended.
2850-2865: Consistent exception-path rethrow handling in finally.
PoppingFinallyEndbefore restoringprev_exckeeps the rethrow targeted at the outer handler, not cleanup.
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.