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

Understanding routing and race conditions #852

Open
JovanVeljanoski opened this issue Nov 6, 2024 · 3 comments
Open

Understanding routing and race conditions #852

JovanVeljanoski opened this issue Nov 6, 2024 · 3 comments

Comments

@JovanVeljanoski
Copy link
Collaborator

Hello again,

Another.. tricky to explain question but I hope I will get the message across..

Say there is a button that does something like this

@solara.component
def SomePage():
   router = solara.use_router()
   solara.Button(label="logout", on_click=lambda: router.push("logout"))

The "logout" is another page (component) we can navigate to, that essentially does this

def logout(router):
    # do some logic
    # logic gives some URL that we need to redirect to (external possibly)
    router.push(logout_url)

@solara.component
def Logout():
    router = solara.use_router()
    # Some other solara visual components to be loaded.. like a progress bar or so
    solara.use_effect(lambda: logout(router), [])

In the above example, some kind of race condition happens, or whatever is called when the order of the routes/redirects/pushes is mixed up, and the process does not complete as it should. Which is strange to me as the use_effect should wait before everything is done/rendered before it executes the function right?

However, If I do something like this, everything works fine!
The example that works as expected:

def logout():
    # some logic that does stuff and generates an logout url
    return logout_url

@solara.component
def Logout():
    router = solara.use_router()
    # Some other solara visual components to be loaded.. like a progress bar or so

    def perform_logout():
        logout_url = logout()
        router.push(logout_url)
    solara.use_effect(perform_logout, [])

There is something fundamental I do not understand here.. may someone shed a bit of light here.. i hope the example is clear enough.. would be very challening to make a working example of this.
Many thanks!

@maartenbreddels
Copy link
Contributor

I've stared at it for a few minutes, but I think this is totally equivalent code right?

@JovanVeljanoski
Copy link
Collaborator Author

Yes! :)
I thought so too.. but option one..i think essentially gets to some race conditions and one rerouting is interrupted by another.
In option 2 it does not happen somehow.. i wanted to understand it.. and know if I am doing anything wrong..

Perhaps something with the rendering etc.. but i thought use_effect takes care of that (waits until the render is complete). But maybe with routing is different?

@maartenbreddels
Copy link
Contributor

What I mean is that the code is fully equivalent, and does not behave different.

Maybe you can have a better reproducing example that shows the issue, then I can take another look.

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

No branches or pull requests

2 participants