Skip to content

Commit

Permalink
fix: ipc.onConnect: check if the new port is alive
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Jan 22, 2025
1 parent 14d73e5 commit 02b57e9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/services/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ function onConnect(port: browser.runtime.Port) {
const srcType = portNameData.srcType
const srcWinId = portNameData.srcWinId ?? NOID
const srcTabId = portNameData.srcTabId ?? NOID
// const dbgPrefix = `IPC.onConnect(${getInstanceName(srcType)}, ${srcWinId ?? srcTabId}):`
// Logs.info(dbgPrefix)
const dbgPrefix = `IPC.onConnect(${getInstanceName(srcType)}, ${srcWinId ?? srcTabId}):`
Logs.info(dbgPrefix)

// Check connection data
const fromBg = srcType === InstanceType.bg
Expand Down Expand Up @@ -716,13 +716,23 @@ function onConnect(port: browser.runtime.Port) {
return
}

// Check if new remote port is alive
try {
port.postMessage(-99)
} catch (err) {
Logs.warn(dbgPrefix, 'New port is already dead...')
return
}

// Find/Create connection
let connection: ConnectionInfo
let connectionIsNew = false
const existedConnection = getConnection(srcType, id)
if (existedConnection) {
Logs.info(dbgPrefix, 'Connection already exists')
connection = existedConnection
} else {
Logs.info(dbgPrefix, 'New connection')
connectionIsNew = true
connection = createConnection(srcType, id)
}
Expand Down

0 comments on commit 02b57e9

Please sign in to comment.