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

fix: Login - fix /api/v2/user cache race condition #47

Merged
merged 3 commits into from
Jan 22, 2022

Conversation

bryphe-coder
Copy link
Contributor

@bryphe-coder bryphe-coder commented Jan 22, 2022

While I was testing the Sign-out functionality in #46 , I found a bug on login.

Sometimes, intermittently, the login wouldn't 'take' even though the login POST was successful and the subsequent GET to /api/v2/user was successful - the user would still be brought to the /login screen a second time:

2022-01-21 19 52 31

^ I log-in, and then the login screen just resets, and logging in a second time works 🤔

I tracked this down and found it is a new race condition that I inadvertently introduced when I migrated to SWR in #46. Because SWR caches responses based on the API path - we have to invalidate the cache for /api/v2/user when the user logins, otherwise we'll continue to serve the user-not-found error until SWR decides to retry.

However, we weren't waiting for that cache-invalidation to go through - so there was the chance that an /api/v2/user request was still in-flight while we were redirecting after a successful login. If the request made it back prior to the redirect - login would work on the first try. If the request took longer - SWR would serve the stale, erroneous user from the cache.

Luckily the fix is simple - mutate in the SWR API returns a promise, so we can just wait for that to go through before redirecting. Unfortunately, though, this is tricky to exercise in a unit test because with the SWR model, the login logic is spread between _app.tsx, UserContext.tsx, and SignInForm.tsx, and the race condition involves an integration of all of those places.

I found that there is a lint rule that can help protect us from making this mistake - typescript-eslint/no-floating-promises (it's a lint rule we wanted to turn on in coder/m, too, actually - just a lot of work to clean up the code for it!), so I turned it on as part of this PR, and it caught a couple extra places to check.

@bryphe-coder bryphe-coder self-assigned this Jan 22, 2022
@codecov
Copy link

codecov bot commented Jan 22, 2022

Codecov Report

Merging #47 (61af313) into main (4183a4e) will increase coverage by 3.35%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #47      +/-   ##
==========================================
+ Coverage   67.74%   71.10%   +3.35%     
==========================================
  Files          55       59       +4     
  Lines        2158     2308     +150     
  Branches       30       30              
==========================================
+ Hits         1462     1641     +179     
+ Misses        578      521      -57     
- Partials      118      146      +28     
Flag Coverage Δ
unittest-go-macos-latest 66.41% <ø> (-0.22%) ⬇️
unittest-go-ubuntu-latest 70.30% <ø> (?)
unittest-go-windows-latest 66.14% <ø> (?)
unittest-js 74.91% <100.00%> (ø)
Impacted Files Coverage Δ
site/components/SignIn/SignInForm.tsx 100.00% <100.00%> (ø)
site/contexts/UserContext.tsx 93.33% <100.00%> (ø)
peerbroker/listen.go 80.46% <0.00%> (-2.35%) ⬇️
provisioner/terraform/serve.go 66.66% <0.00%> (ø)
provisioner/terraform/provision.go 63.26% <0.00%> (ø)
provisioner/terraform/parse.go 70.73% <0.00%> (ø)
database/postgres/postgres.go 66.66% <0.00%> (ø)
peer/channel.go 86.95% <0.00%> (+0.62%) ⬆️
peer/conn.go 76.89% <0.00%> (+0.91%) ⬆️
database/migrate.go 28.57% <0.00%> (+28.57%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4183a4e...61af313. Read the comment docs.

@bryphe-coder bryphe-coder changed the title fix: Wait for /api/v2/user cache to expire prior to redirecting fix: Login - wait for /api/v2/user cache to expire prior to redirecting Jan 22, 2022
@bryphe-coder bryphe-coder changed the title fix: Login - wait for /api/v2/user cache to expire prior to redirecting fix: Login - fix /api/v2/user cache race condition Jan 22, 2022
@bryphe-coder
Copy link
Contributor Author

Thanks for reviewing @kylecarbs 🙏

@bryphe-coder bryphe-coder merged commit e82e7b3 into main Jan 22, 2022
@bryphe-coder bryphe-coder deleted the bryphe/fix/double-login branch January 22, 2022 20:48
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.

None yet

2 participants