You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Session docs should be clearer on potential race condition pitfalls when committing session in nested routesΒ #5647
Are all your remix dependencies & dev-dependencies using the same version?
Yes
Steps to Reproduce
Take this example scenario.
You have this route setup:
root
- index
- api/login
In root's loader, you consume a flash message from the session and commit it.
In index you consume an unrelated flash message from the session and commit it.
In api/login's action you write a flash message saying the user has been logged in.
What you end up with now is a race condition where, after logging in, both index and root are reloaded, causing two session commits to happen simultaneously, with unpredictable behaviour (in my case the flash message was never deleted).
I feel like the documentation should mention this potential issue, and maybe describe how to deal with it (I'm still not clear how you're supposed to navigate this).
Typically if you're using flash, you'll want to have a single loader read it, if another loader wants a flash message, use a different key for that loader.
This seems misleading, using different keys will not stop commits from stomping over each other if the routes get loaded simultaneously
That means if you session.flash in an action, and then session.get in another, you must commit it for that flashed message to go away. With other session storage strategies you only have to commit it when it's created (the browser cookie doesn't need to change because it doesn't store the session data, just the key to find it elsewhere).
This also seems incorrect. With other types of session storage you still need to commit when making changes, you just don't have to update the cookie if the session wasn't newly created. But in most situations the session may be getting created for the first time so you just always set the cookie to be safe.. right?
Expected Behavior
N/A
Actual Behavior
N/A
Reacted by Magnus Bergmark, Jennifer Zouak, Christoph Wanja, Sam Lindstrom, Edgar and candidia
Reacted by Thomas Verleye, Robin Van Cauter, Aaron Adams, Diky Arga and Edgar
changed the title [-]"Session Gotchas" in the docs should mention potential pitfalls of committing session in nested routes[/-][+]Session docs should clearer on potential race condition pitfalls when committing session in nested routes[/+]on Mar 3, 2023
changed the title [-]Session docs should clearer on potential race condition pitfalls when committing session in nested routes[/-][+]Session docs should be clearer on potential race condition pitfalls when committing session in nested routes[/+]on Mar 4, 2023
@johtso interesting insight! I think I might have struggled with the same issue!
What we ended up doing was creating a separate cookie storage specifically for some settings: https://remix.run/docs/en/1.14.0/utils/cookies
I know this makes it more complex because you need to commit more in the header, but this does create separations of concerns.
Your root route fetches user information if the user is authenticated and the info hasn't been previously fetched.
The route from which the user logged in displays form errors from session flash.
After logging in the current route is reloaded, along with the root route. They both commit session storage π₯
Couldn't Remix at least warn the developer if more than one route is committing a session storage at the same time?
I think there's a potential for more race conditions in saving sessions from different windows/tabs simultaneously. I think there should be an optimistic lock mechanism, perhaps.
I have started a discussion about this here: #9169
I just wanted to put a message here as I was investigating a related issue for the whole evening. In my case, I was using remix-toast to display a notification. What I observed was that at times I had double notifications. I've discovered that it is related to a loader race condition. When a flashed cookie was read and committed, the request which was committing it did not have time to complete and hence clear its value from the browser; therefore, when data for a different route was requested, browser provided it again to a loader. That's when the app route saw it again and scheduled a duplicated toast message.
Reacted by candidia, Nikita Burkov, Jikyu Choi and the-dream-machine
Thank you for opening this issue, and our apologies we haven't gotten around to it yet!
With the release of React Router v7 we are sun-setting continued development/maintenance on Remix v2. If you have not already upgraded to React Router v7, we recommend you do so. We've tried to make the upgrade process as smooth as possible with our Future Flags. We are now in the process of cleaning up outdated issues and pull requests to improve the overall hygiene of our repositories.
We plan to continue to address 2 types of issues in Remix v2:
Bugs that pose security concerns
Bugs that prevent upgrading to React Router v7
If you believe this issue meets one of those criteria, please respond or create a new issue.
For all other issues, ongoing maintenance will be happening in React Router v7, so please reopen this issue in that repo with a new minimal reproduction against v7.
If you have any questions you can always reach out on Discord. Thanks again for providing feedback and helping us make our framework even better!
Activity
[-]"Session Gotchas" in the docs should mention potential pitfalls of committing session in nested routes[/-][+]Session docs should clearer on potential race condition pitfalls when committing session in nested routes[/+][-]Session docs should clearer on potential race condition pitfalls when committing session in nested routes[/-][+]Session docs should be clearer on potential race condition pitfalls when committing session in nested routes[/+]thomasverleye commentedon Mar 4, 2023
@johtso interesting insight! I think I might have struggled with the same issue!
What we ended up doing was creating a separate cookie storage specifically for some settings:
https://remix.run/docs/en/1.14.0/utils/cookies
I know this makes it more complex because you need to commit more in the header, but this does create separations of concerns.
johtso commentedon Mar 4, 2023
@thomasverleye creating a separate session storage for ephemeral session data seems like a good suggestion!
johtso commentedon Mar 4, 2023
Another example situation:
Your root route fetches user information if the user is authenticated and the info hasn't been previously fetched.
The route from which the user logged in displays form errors from session flash.
After logging in the current route is reloaded, along with the root route. They both commit session storage π₯
Couldn't Remix at least warn the developer if more than one route is committing a session storage at the same time?
johtso commentedon Mar 5, 2023
The solution I've ended up with now, is to consume all my flash data in the
rootroute, and expose it using a Context provider.Seems to work great and avoids a lot of these issues!
moltar commentedon Mar 31, 2024
I think there's a potential for more race conditions in saving sessions from different windows/tabs simultaneously. I think there should be an optimistic lock mechanism, perhaps.
I have started a discussion about this here: #9169
grundmanise commentedon Jul 28, 2024
I just wanted to put a message here as I was investigating a related issue for the whole evening. In my case, I was using remix-toast to display a notification. What I observed was that at times I had double notifications. I've discovered that it is related to a loader race condition. When a flashed cookie was read and committed, the request which was committing it did not have time to complete and hence clear its value from the browser; therefore, when data for a different route was requested, browser provided it again to a loader. That's when the app route saw it again and scheduled a duplicated toast message.
brookslybrand commentedon Jan 22, 2025
Thank you for opening this issue, and our apologies we haven't gotten around to it yet!
With the release of React Router v7 we are sun-setting continued development/maintenance on Remix v2. If you have not already upgraded to React Router v7, we recommend you do so. We've tried to make the upgrade process as smooth as possible with our Future Flags. We are now in the process of cleaning up outdated issues and pull requests to improve the overall hygiene of our repositories.
We plan to continue to address 2 types of issues in Remix v2:
If you believe this issue meets one of those criteria, please respond or create a new issue.
For all other issues, ongoing maintenance will be happening in React Router v7, so please reopen this issue in that repo with a new minimal reproduction against v7.
If you have any questions you can always reach out on Discord. Thanks again for providing feedback and helping us make our framework even better!