Everything worked until we shipped live updates
We added live updates to our app with Server-Sent Events (SSE). When someone approves a request, every other user sees it right away, with no page reload. It worked well in testing.
Then a bug report came in: "I had the app open in two windows. I didn't click anything, and suddenly both windows showed the login page."
It only happened with two or more windows open, and only after the live-updates release. Nothing in the login code had changed. So why did a feature that only reads data log people out?
The setup
A few facts about our app matter here:
- The access token and refresh token are stored in cookies. All tabs of the same site share the same cookies.
- When a request gets
401, the frontend callsPOST /users/refresh. The backend asks Keycloak for new tokens and sets new cookies. - Keycloak has refresh token rotation on: every refresh returns a new refresh token, and the old one becomes invalid. With "Refresh Token Max Reuse = 0", sending an old refresh token a second time is treated as an attack. Keycloak then ends the whole session.
- Inside one tab, we already shared one refresh call between all requests that got
401at the same time. But nothing coordinated between tabs.
Why SSE made the tabs collide
The easiest way to picture it: every tab has its own phone line to the server (the SSE stream), and all tabs share one key (the cookies).
For security, our server ends each SSE stream exactly when the access token expires. A session that was ended somewhere else can't keep listening forever. The browser then reconnects, and because the token has expired, the reconnect gets 401.
Before SSE, each tab refreshed only when the user happened to click something, at random moments, so two tabs almost never refreshed together. With SSE, every tab's stream ends at the same millisecond, because they all share the same token. So every tab reconnects, gets 401 and calls /refresh at the same time.
What happens next
With two windows, A and B:
- The token expires. The streams in A and B both end.
- A and B both call
/refreshwith the same refresh token, 12 ms apart. - A's call wins: Keycloak returns new tokens and A gets new cookies.
- B's call sends the old refresh token again. Keycloak logs this:
REFRESH_TOKEN_ERROR ... reason="Maximum allowed refresh token reuse exceeded" REFRESH_TOKEN_ERROR ... reason="Session doesn't have required client" - Keycloak drops the session. Our backend answers B with
401and clears the auth cookies. - The cookies are shared, so A loses its brand-new tokens too. Both windows end on the login page.
The fix: let the tabs take turns
The problem isn't that tabs refresh. It's that they refresh at the same time with the same token. If only one tab refreshes and the others wait, then reuse the new cookie, Keycloak never sees a token twice.
Browsers have a built-in tool for exactly this: the Web Locks API. If you know Java, think of a synchronized block that is shared by every tab of the same site. Only one tab at a time runs the code inside it, and the others wait in line:
await navigator.locks.request("my-lock", async () => { // only one tab at a time runs this }); The lock is released when the callback finishes, or automatically if the tab is closed or crashes. So a tab that dies mid-refresh can't block the others forever.
Skipping the extra refresh
A lock alone turns "two refreshes at once" into "two refreshes one after the other". That already stops the logout: by the time the second tab sends its request, the shared cookies hold the new refresh token from the first tab, so nothing is reused. But the second call is wasted.
There is a cleaner way. A tab that waited for the lock should ask: "Did another tab refresh while I was waiting?" If yes, there is nothing to do: the shared cookies already hold fresh tokens.
Tabs don't share JavaScript memory, so they need a shared place to leave a note. localStorage works for that: it is shared by all tabs of the same site.
The code
async function refreshAcrossTabs(): Promise<boolean> { const requestedAt = Date.now(); // "when did I ask?" return navigator.locks.request("auth-refresh", async () => { const lastRefresh = Number(localStorage.getItem("auth-last-refresh") ?? 0); if (lastRefresh >= requestedAt) return true; // another tab refreshed after I asked const refreshed = await refreshToken(); // POST /users/refresh if (refreshed) localStorage.setItem("auth-last-refresh", String(Date.now())); return refreshed; }); } That's the whole idea: take the lock, check the note, refresh only if nobody else just did.
The same timeline, with the fix
Say the access token expires at 10:00, with tabs A and B open:
- 10:00:00.000. Both streams end. A asks for the lock, and B asks 3 ms later.
- A gets the lock. No refresh is stored yet, so A calls
/refreshand gets200. The cookies now hold new tokens, and A stores10:00:00.150inlocalStorage. - B gets the lock. It reads
10:00:00.150, which is later than when it asked (.003). So B skips/refreshand returnstrue. - Both tabs reopen their streams with the new cookie. Nobody is logged out, and Keycloak saw exactly one refresh.
What about background tabs?
To save connections, our app closes the SSE stream of a tab that has been hidden for 30 seconds. With the fix, that tab still stays logged in. When the user opens it again, it reconnects, refreshes through the same lock if needed, and reloads its data. The user sees fresh data within a second.
If you need live updates even in hidden tabs, the next step is a leader tab: one tab holds the only stream and passes events to the others through a BroadcastChannel. That's a bigger change, and for us it wasn't needed: nobody looks at a hidden tab.
"Won't a lock slow things down with many users?"
This was my first worry, because "lock" sounds like a server bottleneck. It isn't one here:
- The lock lives in the browser, not on the server. 10,000 users means 10,000 separate locks that never meet.
- Only one user's own tabs share it. With 3 tabs, at most 3 tabs wait for each other.
- It's held for one refresh call, about 100–300 ms, and only when the token expires.
- Normal API calls never touch it. Only the refresh does.
- The server gets less load: one
/refreshper browser instead of one per tab.
Takeaways
- Features that sync tabs can create races you never had. SSE made every tab act at the same millisecond.
- Refresh token rotation and multiple tabs need coordination. Cookies are shared, so one failed refresh logs out every tab.
-
navigator.locksis the browser's own tool for "only one tab at a time". Add a shared timestamp so waiting tabs skip work that's already done.
