fix(gateway): avoid false foreign gateway detection (#176)

This commit is contained in:
晴天
2026-04-02 18:15:52 +08:00
parent dec0bf4d82
commit c6b6707d6c
9 changed files with 91 additions and 53 deletions

View File

@@ -2125,7 +2125,7 @@ function currentGatewayOwnerSignature() {
}
}
function isCurrentGatewayOwner(owner, pid = null) {
function matchesCurrentGatewayOwnerSignature(owner) {
if (!owner || owner.startedBy !== 'clawpanel') return false
const current = currentGatewayOwnerSignature()
if (Number(owner.port || 0) !== current.port) return false
@@ -2133,10 +2133,19 @@ function isCurrentGatewayOwner(owner, pid = null) {
const ownerCliPath = canonicalCliPath(owner.cliPath)
if (!ownerCliPath || ownerCliPath !== current.cliPath) return false
if (!owner.openclawDir || path.resolve(owner.openclawDir) !== current.openclawDir) return false
if (pid != null && owner.pid != null && Number(owner.pid) !== Number(pid)) return false
return true
}
function gatewayOwnerPidNeedsRefresh(owner, pid = null) {
if (!matchesCurrentGatewayOwnerSignature(owner)) return false
if (!Number.isInteger(pid) || pid <= 0) return false
return !Number.isInteger(owner?.pid) || Number(owner.pid) !== Number(pid)
}
function isCurrentGatewayOwner(owner, pid = null) {
return matchesCurrentGatewayOwnerSignature(owner)
}
function writeGatewayOwner(pid = null) {
const ownerPath = gatewayOwnerFilePath()
const ownerDir = path.dirname(ownerPath)
@@ -2164,7 +2173,11 @@ function foreignGatewayError(pid = null) {
}
function ensureOwnedGatewayOrThrow(pid = null) {
if (isCurrentGatewayOwner(readGatewayOwner(), pid)) return true
const owner = readGatewayOwner()
if (isCurrentGatewayOwner(owner, pid)) {
if (gatewayOwnerPidNeedsRefresh(owner, pid)) writeGatewayOwner(pid)
return true
}
throw foreignGatewayError(pid)
}
@@ -2753,7 +2766,11 @@ const handlers = {
}
const cliInstalled = !!resolveOpenclawCliPath()
const ownedByCurrentInstance = !!running && isCurrentGatewayOwner(readGatewayOwner(), pid || null)
const owner = readGatewayOwner()
const ownedByCurrentInstance = !!running && isCurrentGatewayOwner(owner, pid || null)
if (ownedByCurrentInstance && gatewayOwnerPidNeedsRefresh(owner, pid || null)) {
writeGatewayOwner(pid || null)
}
const ownership = !running ? 'stopped' : ownedByCurrentInstance ? 'owned' : 'foreign'
return [{ label, running, pid, description: 'OpenClaw Gateway', cli_installed: cliInstalled, ownership, owned_by_current_instance: ownedByCurrentInstance }]