Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for corrupted hook state #1254

Merged

Conversation

Archmonger
Copy link
Contributor

@Archmonger Archmonger commented Jan 22, 2025

Description

Due to some backends re-using threads across multiple requests, the hook stack could potentially become corrupted resulting in the following error: RuntimeError("Hook stack is in an invalid state")

Instead of using a ThreadLocal for the hook stack, we now use a ContextVar when ReactPy is run with a webserver. We have to use a ThreadLocal when rendering via pyodide due to lack of ContextVar support.

This PR was originally based on the work of #1204 but has deviated significantly.

As a note, this PR had to create a new pytest fixture to initialize ReactPy's hook stack. This is typically done automatically within our rendering engine, but unfortunately ContextVar values are not retained when using pytest-asyncio async fixtures.

Checklist

Please update this checklist as you complete each item:

  • Tests have been developed for bug fixes or new functionality.
  • The changelog has been updated, if necessary.
  • Documentation has been updated, if necessary.
  • GitHub Issues closed by this PR have been linked.

By submitting this pull request I agree that all contributions comply with this project's open source license(s).

@Archmonger
Copy link
Contributor Author

Archmonger commented Jan 22, 2025

@JamesHutchison I split your ContextVar changes into this PR but it doesn't appear to work within the test suite. Running the test suite results in the following error: LookupError: <ContextVar name='_hook_state' at 0x000001AC3906CF90>

These changes do seem to work fine outside the test runner though - Are there any other changes I'm supposed to migrate from #1204 to get this working?

@JamesHutchison
Copy link
Contributor

JamesHutchison commented Jan 27, 2025

The issue is likely that the tests are missing a call to create_hook_state that occurs normally during the rendering loop. I did not run the reactpy tests against my code in the marathon coding week that I wrote most of this. You could probably just add an automatic test fixture.

I do not test reactpy code automatically so this doesn't show up in Heavy Resume.

@Archmonger Archmonger changed the base branch from main to develop January 31, 2025 23:27
@Archmonger Archmonger added the flag-help-wanted Requires presently unavailable expertise or resources. label Feb 2, 2025
@Archmonger Archmonger removed the flag-help-wanted Requires presently unavailable expertise or resources. label Feb 11, 2025
@Archmonger Archmonger marked this pull request as ready for review February 11, 2025 22:44
@Archmonger Archmonger merged commit e5e2661 into reactive-python:develop Feb 11, 2025
17 checks passed
@Archmonger Archmonger deleted the fix-corrupted-hook-state branch February 11, 2025 22:47
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.

2 participants