Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit b79343d

Browse files
authored
Improve logging in SessionLock (#133)
some more diagnostics for this, to help resolve https://github.com/element-hq/element-desktop/issues/1495
1 parent df4a223 commit b79343d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/utils/SessionLock.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,29 @@ export const SESSION_LOCK_CONSTANTS = {
6666
* @returns true if any instance is currently active
6767
*/
6868
export function checkSessionLockFree(): boolean {
69+
const prefixedLogger = logger.getChild(`checkSessionLockFree`);
70+
6971
const lastPingTime = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_PING);
7072
if (lastPingTime === null) {
7173
// no other holder
74+
prefixedLogger.info("No other session has the lock");
7275
return true;
7376
}
7477

78+
const lockHolder = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_OWNER);
79+
7580
// see if it has expired
7681
const timeAgo = Date.now() - parseInt(lastPingTime);
77-
return timeAgo > SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS;
82+
83+
const remaining = SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS - timeAgo;
84+
if (remaining <= 0) {
85+
// another session claimed the lock, but it is stale.
86+
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is free`);
87+
return true;
88+
}
89+
90+
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is taken`);
91+
return false;
7892
}
7993

8094
/**
@@ -95,7 +109,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
95109
/** unique ID for this session */
96110
const sessionIdentifier = uuidv4();
97111

98-
const prefixedLogger = logger.withPrefix(`getSessionLock[${sessionIdentifier}]`);
112+
const prefixedLogger = logger.getChild(`getSessionLock[${sessionIdentifier}]`);
99113

100114
/** The ID of our regular task to service the lock.
101115
*
@@ -133,7 +147,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
133147
return 0;
134148
}
135149

136-
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting`);
150+
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting ${remaining}ms`);
137151
return remaining;
138152
}
139153

0 commit comments

Comments
 (0)