From b79343d5b09d14e68c83ae4bca8447932ff730a5 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:38:53 +0100 Subject: [PATCH] Improve logging in SessionLock (#133) some more diagnostics for this, to help resolve https://github.com/element-hq/element-desktop/issues/1495 --- src/utils/SessionLock.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils/SessionLock.ts b/src/utils/SessionLock.ts index 6a1c65584d..e3d9d5ecc8 100644 --- a/src/utils/SessionLock.ts +++ b/src/utils/SessionLock.ts @@ -66,15 +66,29 @@ export const SESSION_LOCK_CONSTANTS = { * @returns true if any instance is currently active */ export function checkSessionLockFree(): boolean { + const prefixedLogger = logger.getChild(`checkSessionLockFree`); + const lastPingTime = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_PING); if (lastPingTime === null) { // no other holder + prefixedLogger.info("No other session has the lock"); return true; } + const lockHolder = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_OWNER); + // see if it has expired const timeAgo = Date.now() - parseInt(lastPingTime); - return timeAgo > SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS; + + const remaining = SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS - timeAgo; + if (remaining <= 0) { + // another session claimed the lock, but it is stale. + prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is free`); + return true; + } + + prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is taken`); + return false; } /** @@ -95,7 +109,7 @@ export async function getSessionLock(onNewInstance: () => Promise): Promis /** unique ID for this session */ const sessionIdentifier = uuidv4(); - const prefixedLogger = logger.withPrefix(`getSessionLock[${sessionIdentifier}]`); + const prefixedLogger = logger.getChild(`getSessionLock[${sessionIdentifier}]`); /** The ID of our regular task to service the lock. * @@ -133,7 +147,7 @@ export async function getSessionLock(onNewInstance: () => Promise): Promis return 0; } - prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting`); + prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting ${remaining}ms`); return remaining; }