Skip to content

async with self lock invalidates when yielding #3869

Closed

Description

@sebastianjanisch

Describe the bug
In a background task, when locking the state using async with self the lock invalidates when yielding, causing a ImmutableStateError on subsequent state changes.

To Reproduce
Works:

import asyncio
import reflex as rx


class BugState(rx.State):

    test_var: str = "hello"

    @rx.background
    async def on_click(self):
        async with self:
            self.test_var = "world"

            await asyncio.sleep(2)

            self.test_var = "i don't know"


@rx.page(route="/test")
def bug() -> rx.Component:
    return rx.vstack(
        rx.button("Click me", on_click=BugState.on_click),
        rx.text(BugState.test_var),
    )

Fails:

import asyncio
import reflex as rx


class BugState(rx.State):

    test_var: str = "hello"

    @rx.background
    async def on_click(self):
        async with self:
            self.test_var = "world"

            yield

            await asyncio.sleep(2)

            self.test_var = "i don't know"


@rx.page(route="/test")
def bug() -> rx.Component:
    return rx.vstack(
        rx.button("Click me", on_click=BugState.on_click),
        rx.text(BugState.test_var),
    )

As a workaround, the lock must be reentered:

import asyncio
import reflex as rx


class BugState(rx.State):

    test_var: str = "hello"

    @rx.background
    async def on_click(self):
        async with self:
            self.test_var = "world"

        yield
        await asyncio.sleep(2)

        async with self:
            self.test_var = "i don't know"


@rx.page(route="/test")
def bug() -> rx.Component:
    return rx.vstack(
        rx.button("Click me", on_click=BugState.on_click),
        rx.text(BugState.test_var),
    )

Expected behavior
Lock does not invalidate when yielding, or lock is reacquired as the function re-enters.

Screenshots
If applicable, add screenshots to help explain your problem.

Specifics (please complete the following information):

  • Python Version: 3.11
  • Reflex Version: 0.5.10
  • OS:
  • Browser (Optional):

Additional context
Add any other context about the problem here.

Activity

  1. added
    bug
    on Sep 2, 2024
  2. DanielFrantes commented on Sep 4, 2024

    @DanielFrantes
    Contributor

    Hello @sebastianjanisch Can you try to reproduce a bug in newest version of reflex?

    thanks
    danik292

  3. sebastianjanisch commented on Sep 4, 2024

    @sebastianjanisch
    Author

    I did, 0.5.10 right?

  4. added 2 commits that reference this issue on Sep 4, 2024
    d9ee459
    cbb92fd
  5. DanielFrantes commented on Sep 5, 2024

    @DanielFrantes
    Contributor

    Sorry I looked into bad field.

  6. added a commit that references this issue on Sep 6, 2024
    6308eb6
  7. added a commit that references this issue on Sep 6, 2024
    eca32d0
  8. added 2 commits that reference this issue on Sep 9, 2024
    d29e2c1
    a5c73ad
  9. added a commit that references this issue on Sep 11, 2024
    95631ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions