Skip to content

Fix async state scoping for submodules (#2866)#2892

Open
raza-khan0108 wants to merge 1 commit into
Avaiga:developfrom
raza-khan0108:fix/issue-2866-async-state-scoping
Open

Fix async state scoping for submodules (#2866)#2892
raza-khan0108 wants to merge 1 commit into
Avaiga:developfrom
raza-khan0108:fix/issue-2866-async-state-scoping

Conversation

@raza-khan0108

Copy link
Copy Markdown

What type of PR is this? (Check all that apply)

  • 🛠 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • ⚡ Optimization
  • 📝 Documentation Update

Description

This PR resolves an issue where _AsyncState failed to correctly resolve the module context when accessing or modifying variables within a submodule. By capturing the module context using the caller's stack frame in __getattr__ and __setattr__ (via inspect.stack()[1]), state variables accessed during async callbacks are now correctly resolved in the proper submodule context instead of erroneously falling back to __main__.

Related Tickets & Documents

How to reproduce the issue

Set up an application with a submodule handling an async callback, where state variables are updated.

  1. Create a main file (main.py) routing to a home_page imported from a submodule (pages/home.py).
  2. Inside pages/home.py, define an async callback (e.g. async def count_to_10(state): state.counter = 10) hooked up to a button.
  3. Click the button. Previously, this would trigger Variable 'counter' is not available in the '__main__' module or AttributeError. It now successfully resolves the variable inside the pages.home context.

(See #2866 for the full reproduction code).

Backporting

This change should be backported to:

  • 3.0
  • 3.1
  • 4.0
  • develop

Checklist

We encourage keeping the code coverage percentage at 80% or above.

  • ✅ This solution meets the acceptance criteria of the related issue.
  • 📝 The related issue checklist is completed.
  • 🧪 This PR includes unit tests for the developed code.
    If not, explain why:
  • 🔄 End-to-End tests have been added or updated.
    If not, explain why: A unit test accurately captures and isolates the _AsyncState module scoping bug, rendering a full end-to-end test unnecessary for this fix.
  • 📚 The documentation has been updated, or a dedicated issue has been created.
    If not, explain why: This is an internal framework fix and introduces no public API or syntax changes for users.
  • 📌 The release notes have been updated.
    If not, explain why: Can be handled internally during the release preparation if necessary.

Copilot AI review requested due to automatic review settings July 9, 2026 18:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 BUG] Async callback submodule scoping issue

2 participants