Skip to content

[#1672] Fix scene handlers not receiving context from observer middlewares#1673

Closed
Vadim-Khristenko wants to merge 7 commits intoaiogram:dev-3.xfrom
Vadim-Khristenko:fix-scene-bug
Closed

[#1672] Fix scene handlers not receiving context from observer middlewares#1673
Vadim-Khristenko wants to merge 7 commits intoaiogram:dev-3.xfrom
Vadim-Khristenko:fix-scene-bug

Conversation

@Vadim-Khristenko
Copy link
Contributor

Description

This PR fixes the issue where scene action handlers do not receive context and data injected by observer (outer_middleware) middlewares, leading to missing argument errors. Now, scene handlers (such as enter, leave, etc.) properly receive context/data from observer middlewares, matching the behavior of regular handlers.

Fixes #1672

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

No new automated tests have been added yet, but I can provide them if required.
The fix was manually tested using the MRE from #1672, which now works as expected after this change.

Test Configuration:

  • Operating System: Windows 11 (also reproducible on Linux)
  • Python version: 3.12.10

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Note:
No new tests are included in this PR, but I am ready to add them if necessary. The fix was verified with the MRE provided in #1672.

@github-actions github-actions bot added the 3.x Issue or PR for stable 3.x version label Apr 14, 2025
@github-actions
Copy link

✔️ Changelog found.

Thank you for adding a description of the changes

@Vadim-Khristenko Vadim-Khristenko changed the title [#1672] [#1672] Fix scene handlers not receiving context from observer middlewares Apr 14, 2025
@JrooTJunior JrooTJunior requested a review from Copilot April 15, 2025 00:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • CHANGES/1673.bugfix.rst: Language not supported
Comments suppressed due to low confidence (2)

aiogram/fsm/scene.py:589

  • [nitpick] The function name '_actual_handler' is generic; consider renaming it to better reflect its role in invoking the action configuration with middleware context (for example, '_invoke_action_with_middleware').
async def _actual_handler(event, **data):

aiogram/fsm/scene.py:592

  • Automated tests are missing for the new observer middleware branch. Consider adding tests to ensure this behavior remains reliable.
await observer.wrap_outer_middleware(

@Vadim-Khristenko
Copy link
Contributor Author

Fix middleware chain in scene handling

Found and fixed an issue where standard middlewares caused a TypeError due to handler signature mismatch (extra context like dispatcher not accepted as kwargs).
Now, all middleware are wrapped in a single chain, and the handler receives **data as expected by aiogram.
Sorry for missing this before—now tested and working!

@Vadim-Khristenko
Copy link
Contributor Author

@JrooTJunior I’ve pushed an updated version that fully fixes middleware handling in scenes (both outer and standard middlewares).
Sorry again for the earlier oversight and thanks for your feedback!
Could you please take another look when you have the time?

@JrooTJunior
Copy link
Member

JrooTJunior commented Apr 15, 2025

Thanks for you contribution.

I checked the issue and purposed solution and my conclusion is that the proposed solution is overcomplicated and is not correct.

Explanation:

The handler that is the entry point into the scene at the time of the call has already passed the middleware chain, so in no case should it be reprocessed again, also by design the handler should not know how exactly it was called, it should expect that it will be called correctly and this principle is violated in the proposed solution.

Also, leave, exit triggers are not handlers, and are always called explicitly inside the handlers, so they should also not have a duplicate call of all middleware steps, but all parameters must be passed explicitly.

The correct solution is very simple and consists in replacing only two lines of code that should accept and pass arguments to the callback in the handler.

Conclusion

I will make this correction in the near future and I reject this proposed solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Issue or PR for stable 3.x version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scene action handlers do not pass through observer middleware, causing missing context and argument errors

3 participants