fix: commandSenderMap race - embed senderId in SERVER_COMMAND/CMD_ACK instead of global Map

This commit is contained in:
Koala
2026-05-25 11:52:05 +02:00
parent 284b82a910
commit ed24a4c263
2 changed files with 9 additions and 12 deletions
+4 -7
View File
@@ -15,7 +15,6 @@ let pendingHistory = [];
let eventQueue = []; let eventQueue = [];
let isNamespaceJoined = false; let isNamespaceJoined = false;
let lastActionState = { action: null, senderId: null, timestamp: 0, acks: [] }; let lastActionState = { action: null, senderId: null, timestamp: 0, acks: [] };
let commandSenderMap = new Map(); // tabId -> senderId (tracks who initiated each command per tab)
// --- Boot Sequence Lock --- // --- Boot Sequence Lock ---
let restorationTask = null; let restorationTask = null;
@@ -908,14 +907,15 @@ async function routeToContent(action, payload) {
const tabId = parseInt(currentTabId); const tabId = parseInt(currentTabId);
if (isNaN(tabId)) return; if (isNaN(tabId)) return;
commandSenderMap.set(tabId, payload?.senderId || null);
const actionTimestamp = payload?.actionTimestamp || Date.now(); const actionTimestamp = payload?.actionTimestamp || Date.now();
const commandSenderId = payload?.senderId || null;
chrome.tabs.sendMessage(tabId, { chrome.tabs.sendMessage(tabId, {
type: 'SERVER_COMMAND', type: 'SERVER_COMMAND',
action, action,
payload, payload,
actionTimestamp actionTimestamp,
commandSenderId
}).catch(err => { }).catch(err => {
if (err.message.includes('Receiving end does not exist') || err.message.includes('Extension context invalidated')) { if (err.message.includes('Receiving end does not exist') || err.message.includes('Extension context invalidated')) {
chrome.scripting.executeScript({ chrome.scripting.executeScript({
@@ -925,12 +925,10 @@ async function routeToContent(action, payload) {
setTimeout(() => routeToContent(action, payload), 500); setTimeout(() => routeToContent(action, payload), 500);
}).catch(_err => { }).catch(_err => {
addLog(`Auto-reinject failed for tab ${tabId}`, 'warn'); addLog(`Auto-reinject failed for tab ${tabId}`, 'warn');
commandSenderMap.delete(tabId);
}); });
} else { } else {
addLog(`Content Script not responding in tab ${tabId}`, 'warn'); addLog(`Content Script not responding in tab ${tabId}`, 'warn');
currentTabId = null; currentTabId = null;
commandSenderMap.delete(tabId);
updateBadgeStatus(); updateBadgeStatus();
} }
}); });
@@ -1200,8 +1198,7 @@ async function handleAsyncMessage(message, sender, sendResponse) {
} }
sendResponse({ status: 'ok' }); sendResponse({ status: 'ok' });
} else if (message.type === 'CMD_ACK') { } else if (message.type === 'CMD_ACK') {
const tabId = sender.tab ? sender.tab.id : null; const commandSenderId = message.commandSenderId;
const commandSenderId = tabId ? commandSenderMap.get(tabId) : null;
if (commandSenderId && commandSenderId !== peerId) { if (commandSenderId && commandSenderId !== peerId) {
emit(EVENTS.EVENT_ACK, { emit(EVENTS.EVENT_ACK, {
senderId: peerId, senderId: peerId,
+5 -5
View File
@@ -280,15 +280,15 @@
if (action === EVENTS.PLAY) { if (action === EVENTS.PLAY) {
tryMediaAction(EVENTS.PLAY); tryMediaAction(EVENTS.PLAY);
chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp }); chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp, commandSenderId: message.commandSenderId });
actionCompleted = true; actionCompleted = true;
} else if (action === EVENTS.PAUSE) { } else if (action === EVENTS.PAUSE) {
tryMediaAction(EVENTS.PAUSE); tryMediaAction(EVENTS.PAUSE);
chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp }); chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp, commandSenderId: message.commandSenderId });
actionCompleted = true; actionCompleted = true;
} else if (action === EVENTS.SEEK) { } else if (action === EVENTS.SEEK) {
tryMediaAction(EVENTS.SEEK, payload); tryMediaAction(EVENTS.SEEK, payload);
chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp }); chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp, commandSenderId: message.commandSenderId });
actionCompleted = true; actionCompleted = true;
} else if (action === EVENTS.FORCE_SYNC_PREPARE) { } else if (action === EVENTS.FORCE_SYNC_PREPARE) {
if (!payload || payload.targetTime === undefined) return; if (!payload || payload.targetTime === undefined) return;
@@ -312,9 +312,9 @@
}).catch(() => {}); }).catch(() => {});
} }
} else if (action === EVENTS.FORCE_SYNC_EXECUTE) { } else if (action === EVENTS.FORCE_SYNC_EXECUTE) {
stopLobbyPoll(); // Clear any pending lobby on force sync stopLobbyPoll();
tryMediaAction(EVENTS.PLAY); tryMediaAction(EVENTS.PLAY);
chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp }); chrome.runtime.sendMessage({ type: 'CMD_ACK', actionTimestamp: message.actionTimestamp, commandSenderId: message.commandSenderId });
actionCompleted = true; actionCompleted = true;
} }