Release dispatch state references before running a background handler
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing functionality to not work as expected)
- This change requires a documentation update
New Feature Submission:
- Does your submission pass the tests?
- Have you linted your code locally prior to submission?
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
After these steps, you're ready to open a pull request.
Refs #7444 (the StateProxy.wrapped retention is left open)
In _execute_event, the locals state, substate and root_state stay alive across await process_event(...) for a background handler, pinning the dispatch-time state tree for as long as the handler runs. I added del state, substate, root_state right after proxy = StateProxy(substate), as the issue proposed. None of those locals are used afterwards.
This addresses only the first reference described in the issue (the frame locals). It does not touch the second one, StateProxy.__wrapped__ holding the last async with self tree.
I added a test that checks the dispatch-time state tree is collected while a background handler is still running (it fails on the base commit and passes with the change), plus a news fragment. The whole test file passes locally (31 passed) and ruff is clean.
This change was agent-assisted, and I reviewed and checked it myself.