Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: use Web Lock API and BroadcastChannel for cross-tab refresh coordination (#18746)

p0ny718474fc4f62
diff --git a/frontend/src/grpcweb/refreshToken.ts b/frontend/src/grpcweb/refreshToken.ts
index 6bc5b4442f..697bab07f1 100644
--- a/frontend/src/grpcweb/refreshToken.ts
+++ b/frontend/src/grpcweb/refreshToken.ts
@@ -1,18 +1,14 @@
-import { v4 as uuidv4 } from "uuid";
import { authServiceClientConnect } from "@/grpcweb";
-const LOCK_KEY = "bb_refresh_lock";
-const LOCK_TIMEOUT_MS = 10000; // 10 seconds max lock hold time
-const POLL_INTERVAL_MS = 50;
-
-// Unique tab identifier - prevents race when two tabs call Date.now() in same millisecond
-const TAB_ID = uuidv4();
+const LOCK_NAME = "bb_token_refresh";
+const CHANNEL_NAME = "bb_token_refresh";
+const WAIT_TIMEOUT_MS = 10000;
let localPromise: Promise<void> | null = null;
/**
* Refresh the access token using the refresh token cookie.
- * Uses localStorage lock for cross-tab coordination.
+ * Uses Web Lock API for cross-tab coordination.
* Only one tab performs the refresh; others wait for completion.
*/
export async function refreshTokens(): Promise<void> {
@@ -29,82 +25,52 @@ export async function refreshTokens(): Promise<void> {
}
async function doRefresh(): Promise<void> {
- // Try to acquire lock
- if (!tryAcquireLock()) {
- // Another tab is refreshing - wait for it to complete
- await waitForLockRelease();
+ if (await tryAcquireAndRefresh()) {
return;
}
- try {
- await authServiceClientConnect.refresh({});
- } finally {
- releaseLock();
- }
-}
-
-function tryAcquireLock(): boolean {
- const now = Date.now();
- const existing = localStorage.getItem(LOCK_KEY);
-
- if (existing) {
- const lockTime = parseLockTime(existing);
- // Lock is still valid (not expired)
- if (lockTime !== null && now - lockTime < LOCK_TIMEOUT_MS) {
- return false;
- }
- // Lock expired or invalid - we can take over
- }
-
- const lockValue = `${TAB_ID}_${now}`;
- localStorage.setItem(LOCK_KEY, lockValue);
+ // Another tab is refreshing - wait for broadcast
+ await waitForBroadcast();
- // Double-check we got the lock (handles near-simultaneous writes)
- const check = localStorage.getItem(LOCK_KEY);
- return check === lockValue;
+ // After wait, try once more (handles race condition or failed refresh)
+ await tryAcquireAndRefresh();
}
-function parseLockTime(lockValue: string): number | null {
- // Format: "tabId_timestamp"
- const parts = lockValue.split("_");
- if (parts.length < 2) {
- return null;
- }
- const timestamp = parseInt(parts[parts.length - 1], 10);
- return isNaN(timestamp) ? null : timestamp;
-}
-
-function releaseLock(): void {
- localStorage.removeItem(LOCK_KEY);
+async function tryAcquireAndRefresh(): Promise<boolean> {
+ return navigator.locks.request(
+ LOCK_NAME,
+ { ifAvailable: true },
+ async (lock) => {
+ if (!lock) {
+ return false;
+ }
+ try {
+ await authServiceClientConnect.refresh({});
+ } finally {
+ // Notify waiting tabs regardless of success/failure
+ const channel = new BroadcastChannel(CHANNEL_NAME);
+ channel.postMessage("complete");
+ channel.close();
+ }
+ return true;
+ }
+ );
}
-async function waitForLockRelease(): Promise<void> {
- const startTime = Date.now();
-
+function waitForBroadcast(): Promise<void> {
return new Promise((resolve) => {
- const checkLock = () => {
- const existing = localStorage.getItem(LOCK_KEY);
- const lockTime = existing ? parseLockTime(existing) : null;
+ const channel = new BroadcastChannel(CHANNEL_NAME);
- // Lock released or expired
- if (
- !existing ||
- lockTime === null ||
- Date.now() - lockTime >= LOCK_TIMEOUT_MS
- ) {
- resolve();
- return;
- }
+ const cleanup = () => {
+ channel.close();
+ resolve();
+ };
- // Safety timeout - don't wait forever
- if (Date.now() - startTime >= LOCK_TIMEOUT_MS) {
- resolve();
- return;
- }
+ const timeout = setTimeout(cleanup, WAIT_TIMEOUT_MS);
- setTimeout(checkLock, POLL_INTERVAL_MS);
+ channel.onmessage = () => {
+ clearTimeout(timeout);
+ cleanup();
};
-
- checkLock();
});
}