From 5886cc661c92328facbd33a669e7fbdfa81ef1d5 Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Wed, 11 Feb 2026 11:26:29 +0100 Subject: [PATCH] feat: implement race condition fix for session ID management in script.js, ensuring correct ID usage across multiple tabs --- public/script.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/public/script.js b/public/script.js index 103b820..eb56cac 100644 --- a/public/script.js +++ b/public/script.js @@ -160,6 +160,23 @@ } } cachedSessionId = generateId(); + // * Race fix: re-read before writing; if another tab wrote in the meantime, use that ID instead + var rawAgain = localStorage.getItem(key); + if (rawAgain) { + try { + var parsedAgain = JSON.parse(rawAgain); + if (parsedAgain && typeof parsedAgain.id === 'string') { + var expiredAgain = ttlMs > 0 && typeof parsedAgain.created === 'number' && (Date.now() - parsedAgain.created > ttlMs); + if (!expiredAgain) { + cachedSessionId = parsedAgain.id; + // #region agent log + try { fetch('http://127.0.0.1:7243/ingest/50587964-c1c6-436a-a7ce-ff2cde3c5b63',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'script.js:getSessionId:local',message:'race fix reused other tab id',data:{sessionIdPrefix:cachedSessionId.substring(0,8)},timestamp:Date.now(),hypothesisId:'E'})}).catch(function(){}); } catch (e4) {} + // #endregion + return cachedSessionId; + } + } + } catch (e2) {} + } localStorage.setItem(key, JSON.stringify({ id: cachedSessionId, created: Date.now() })); // #region agent log try { fetch('http://127.0.0.1:7243/ingest/50587964-c1c6-436a-a7ce-ff2cde3c5b63',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({location:'script.js:getSessionId:local',message:'generated new and wrote to localStorage',data:{sessionIdPrefix:cachedSessionId.substring(0,8)},timestamp:Date.now(),hypothesisId:'C,E'})}).catch(function(){}); } catch (e) {}