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

Race condition when mutate is called multiple times in short succession with the same key #566

Closed
derindutz opened this issue Jul 29, 2020 · 6 comments · Fixed by #735
Closed
Labels
bug Something isn't working

Comments

@derindutz
Copy link

derindutz commented Jul 29, 2020

Bug report

First off, thank you for your awesome work on this library 😊 I really enjoy using it and it's tremendously helpful.

Description / Observed Behavior

Calling mutate multiple times with the same key within a very short amount of time leads to a race condition. When I pass a callback function to mutate, if I update the value on call #1 of mutate, call #2 of mutate doesn't get the updated value.

This leads to weird unexpected bugs since it's not clear that the library will behave this way.

It seems like the bug is here: https://github.com/vercel/swr/blob/master/src/use-swr.ts#L131. If mutate is called multiple times in short succession, cache.get(key) will not be up to date.

Expected Behavior

I expect no race condition. When I update the value on call #1 of mutate, I expect the value to be updated on call #2 of mutate.

Repro Steps / Code Example

https://codesandbox.io/s/swr-mutate-race-condition-gnrs3

Additional Context

SWR version: 0.2.3
Add any other context about the problem here.

@shuding shuding added the bug Something isn't working label Jul 31, 2020
@kweiberth
Copy link

I think I'm experiencing something very similar to this. I'm building an optimistic UI, and if there are two quick edits made, the two local invocations of mutate(key, updateFn, false) don't each update the local cache; instead, only the first one updates it.

Any updates on a fix? Is it indeed due to cache.get(key) not getting updated.

PS, I'd like to echo @derindutz sentiment that this an awesome library it's been a pleasure to work with 🙏

@ranisalt
Copy link

ranisalt commented Sep 4, 2020

Can confirm I'm having the same issue, using mutate returned from a call to useSWR, where the second call to mutate overrides the first if the second fires before the first finishes.

@blujedis
Copy link

Confirmed, so I'm not crazy....ya'll have me debugging like a mad man, this does seem like an issue with the key.

In my case an auth strategy that I am using seems to exacerbate the issue. This is because it calls server on render of component verifying roles before rendering the inner or content component.

Initially that it was the culprit but after inspection and some debugging it is in fact SWR it appears. If I disable SWR and just use a simple fetch there are no issues with timing or race conditions.

That all said just love this lib!!!

@shuding
Copy link
Member

shuding commented Oct 30, 2020

Hi @ranisalt I'd love to know if #735 fixes the issue you had.

@ranisalt
Copy link

ranisalt commented Nov 5, 2020

@shuding I can't test it, I haven't had this issue anymore even before the merge.

@shuding
Copy link
Member

shuding commented Nov 5, 2020

@ranisalt No worries! Let me know if you see it again. 👍 Also thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants