Quick revision
The short answer
When the app starts, several parallel API calls all find the access token expired and each one sends a refresh request with the same refresh token. With rotation, the first refresh succeeds and invalidates that token, so the others arrive with an already-used token and get invalid_grant; with reuse detection, the server may also revoke the whole session, which logs the user out. The fix is in the client: allow one refresh at a time and make other calls wait for its result, and refresh a little before expiry.
What to remember
In this answer, we traced random mobile logouts to parallel refreshes at startup. Each call refreshed with the same token; rotation made all but the first fail with invalid_grant, and reuse detection then revoked the session. The fix is a single-flight refresh in the client, with other calls waiting for its result and a check after the lock, plus refreshing slightly before expiry. A server grace period can soften the race, but the client must still be fixed and tested.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
When the app starts, several parallel API calls all find the access token expired and each one sends a refresh request with the same refresh token. With rotation, the first refresh succeeds and invalidates that token, so the others arrive with an already-used token and get invalid_grant; with reuse detection, the server may also revoke the whole session, which logs the user out. The fix is in the client: allow one refresh at a time and make other calls wait for its result, and refresh a little before expiry.
Why this question is asked
Random logouts are one of the most frustrating bugs to chase, because they depend on timing and almost never happen on a developer's phone with a fresh token. The clue in this question is the pairing of a successful refresh with failures a moment later, and the fact that the app fires several requests at startup. Interviewers use it to see whether we connect a security feature, refresh token rotation, with a concurrency bug in the client. In this answer, we will recap rotation and reuse detection, reproduce the race, see why it sometimes logs the user out entirely, and fix it with a single-flight refresh, then look at grace periods and early refresh.
1A Quick Recap of Rotation and Reuse Detection
The app holds a short-lived access token and a longer-lived refresh token. When the access token expires, the app sends the refresh token to the authorization server's token endpoint and receives a new access token. With refresh token rotation, the response also contains a new refresh token, and the old one becomes invalid. Each refresh token is meant to be used exactly once.
Reuse detection is the safety net on top. If the server sees an already-used refresh token again, it assumes that someone stole it, and it revokes the whole family of tokens that came from that sign-in. That protects against theft, but it cannot tell a thief from a buggy client that simply sent the same token twice.
2Reproducing the Race at Startup
The app was in the background overnight, so its access token expired hours ago. On launch, the home screen loads the profile, the feed and the notifications at the same moment. Each call has its own interceptor that checks the token, sees it expired, and refreshes on its own.
8 messages in order. Play them, or step through them one at a time.
Notice that the first refresh is perfectly valid. The failures come from the second and third requests, which still carry R1. Whether the user ends up logged out depends on the server: without reuse detection, the extra calls just fail and the app may recover on retry; with reuse detection, the server revokes R2 as well, and the next refresh fails no matter what the app does. Because it depends on how many calls were in flight and how fast the network was, it looks random.
3Confirming the Diagnosis in the Logs
Before changing code, we confirm the story. In the authorization server's logs, we look for the same refresh token (or its hashed id) used two or more times within a second or two from the same device, with the first attempt successful and the rest invalid_grant. Most identity providers log a specific reason for reuse, and some log the family revocation as a separate event. On the client side, the analytics around the logout should show several requests started at the same time with expired tokens.
If the reuse is from different IP addresses or devices minutes apart, it is a different story, possibly real token theft, and the server is doing exactly its job. The narrow timing and the startup pattern are what point to the client race.
4The Fix: One Refresh at a Time
The client must make sure only one refresh is ever in flight. The first call that sees an expired token starts the refresh; every other call waits for that same result and then uses the new access token. This is often called a single-flight refresh.
What changed is that the refresh is shared state with a lock around it. The check inside the lock matters: a call that waited must not refresh again with the token it read before waiting, because by now that token is used. The same applies to the 401 path: if an API returns 401 because the token expired in flight, the retry must go through the same single-flight refresh. The new refresh token should also be stored durably before it is used, so that an app killed mid-refresh does not wake up with an old one.
5Grace Periods and Refreshing Early
Some authorization servers offer a short reuse grace period: for a few seconds after a refresh token is used, a repeated use returns the same new tokens or is at least not treated as theft. That makes the race harmless, but it is a server feature with different names and defaults across providers, so check your identity provider. It is a cushion, not a substitute for fixing the client, because other clients and other providers will not have it.
Refreshing a little before expiry also helps. If the client refreshes when the access token has 30 to 60 seconds left, or right when the app comes to the foreground before the screens load, the burst of startup calls finds a valid token and never needs to refresh. Combined with the single-flight lock, the token endpoint sees one refresh per expiry instead of one per screen.
6Testing the Whole Flow
The bug only shows with an expired token and parallel calls, so the test must create that situation on purpose. A useful instrumented test sets the stored access token as expired, launches the home screen, and checks with a fake authorization server that exactly one refresh request arrived. A second test makes the refresh slow, so the other calls definitely wait, and a third simulates a 401 in the middle of a request. Running them in CI keeps the race from coming back when someone adds a new interceptor.
7Follow-up questions
Should we just turn off reuse detection to stop the logouts?
No. Reuse detection is what limits the damage of a stolen refresh token on a public client. Turning it off hides the client bug and removes a real protection. Fix the client, and use the server's grace period if it has one.
Can the same race happen in a single-page web app?
Yes, and also across browser tabs, which share storage but not memory. Within one tab a shared promise works like the mutex. Across tabs we need a lock such as the Web Locks API, or better, a backend-for-frontend that holds the tokens on the server and refreshes once per session.
Why do the logs show the first refresh succeeding?
Because it did. The first request carried a valid refresh token and got new tokens. The failures are the duplicates that followed, and with reuse detection those duplicates also revoke the tokens the first request just received.
8Conclusion
In this answer, we traced random mobile logouts to parallel refreshes at startup. Each call refreshed with the same token; rotation made all but the first fail with invalid_grant, and reuse detection then revoked the session. The fix is a single-flight refresh in the client, with other calls waiting for its result and a check after the lock, plus refreshing slightly before expiry. A server grace period can soften the race, but the client must still be fixed and tested.
Open with
What is happening is a race in the client. When the app starts, the access token has usually expired overnight, and the app fires several API calls at once.
Then, if they want more
Each call sees the expired token and sends its own refresh request with the same refresh token. Our server uses rotation, so the first refresh succeeds, returns a new refresh token, and invalidates the old one. The other requests arrive with that already-used token and get invalid_grant.
That alone causes errors, but with reuse detection it is worse: the server sees the old token used again, assumes it was stolen, and revokes the whole token family, including the new token the first call just got. So the next refresh fails and the app shows the login screen.
It looks random because it depends on how many calls were in flight. I would confirm it in the auth server logs by finding the same refresh token used several times within a second from one device.
The fix is in the client: a single-flight refresh, where the first call takes a lock and refreshes, and the others wait and then reuse the new access token, with a check inside the lock so nobody refreshes twice.
I would also refresh a little before expiry or when the app comes to the foreground, and check whether our identity provider has a short reuse grace period.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RFC 7519, JSON Web Token
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Users of a mobile app are logged out randomly. Logs show refresh requests failing with...": https://scrivio-interview-library.netlify.app/spring-boot/jwt/3f9956abaf/