🎉 New Pull Request to ag2ai/faststream by @Vitaly312
✨ Fix: exit already entered middlewares if one fails to enter (#3252)
📊 +42/-11
🌿 Vitaly312:fix/middleware-enter-leak → main
DESCRIPTION
Fixes #3251
Middlewares were entered by hand, and their __aexit__ was pushed to the exit stack only after the message scope had been entered — so a failing on_receive left every already entered middleware without after_processed.
They cannot just be entered with stack.enter_async_context, because then they would be exited after the message/log_context scope is released. Instead, the scope now gets its own stack, reserved before the middlewares are entered, which keeps the exit order and removes both push_async_exit loops.
Side effect: an error raised in on_receive now reaches CriticalLogMiddleware and is logged. Previously it disappeared entirely.
Type of change
☑️ Bug fix (a non-breaking change that resolves an issue)
Checklist
☑️ My code adheres to the style guidelines of this project (just lint shows no errors)
☑️ I have conducted a self-review of my own code
☑️ My changes do not generate any new warnings
☑️ I have added tests to validate the effectiveness of my fix or the functionality of my new feature
☑️ Both new and existing unit tests pass successfully on my local environment by running just test-coverage
☑️ I have ensured that static analysis tests are passing by running just static-analysis
sent via relator
Сообщение🎉 New Pull Request to ag2ai/faststream by @Vitaly312
1 октября 2026Рядом в ленте
это сообщение
Б