diff --git a/src/process/ProcessOwnership.ts b/src/process/ProcessOwnership.ts index 8bec5ef68c..ce0c621fd4 100644 --- a/src/process/ProcessOwnership.ts +++ b/src/process/ProcessOwnership.ts @@ -1085,14 +1085,14 @@ const createOwnedCleanupPlan = async ( const rootMembers = relationSnapshot.currentRelations.filter( ({ processGroupId }) => processGroupId === ownership.processGroupId, ); - const unresolved: ProcessOwnershipValidationFailure[] = []; + const unresolved = new Map(); for (const failure of tokenOwned.failures) mergeProcessOwnershipFailure(unresolved, failure); // The later bracketed token scan retains the current, more useful failure // for a PID whose earlier membership read was also unavailable. - for (const failure of relationSnapshot.failures) - if (!unresolved.some(({ pid }) => pid === failure.pid)) - mergeProcessOwnershipFailure(unresolved, failure); + for (const failure of relationSnapshot.failures) { + if (!unresolved.has(failure.pid)) unresolved.set(failure.pid, failure); + } for (const process of tokenOwned.owned) if (omittedFromListedTable(process.pid)) mergeProcessOwnershipFailure(unresolved, { @@ -1110,11 +1110,11 @@ const createOwnedCleanupPlan = async ( sampledProcessGroupIds.has(processGroupId), ) ) - return unresolved.length > 0 + return unresolved.size > 0 ? { signalOrder: [], tokenOwnedIdentities: tokenOwned.identities, - unresolved, + unresolved: [...unresolved.values()], unverified: tokenOwned.unverified, } : { @@ -1184,17 +1184,23 @@ const createOwnedCleanupPlan = async ( tokenOwned.identities, exitSettlement, ); - if (failures.length === 0 && unverifiableSampledGroups.has(processGroupId)) - unresolved.push( - ...validatedMembers.map(({ pid }) => ({ - pid, - reason: "process-identity-unavailable" as const, - diagnostic: - "sampled process-group leader identity could not be verified", - })), - ); + if ( + failures.length === 0 && + unverifiableSampledGroups.has(processGroupId) + ) { + for (const { pid } of validatedMembers) { + if (!unresolved.has(pid)) + unresolved.set(pid, { + pid, + reason: "process-identity-unavailable", + diagnostic: + "sampled process-group leader identity could not be verified", + }); + } + } + const memberPids = new Set(members.map(({ pid }) => pid)); const scanFailures = tokenOwned.failures.filter(({ pid }) => - members.some((member) => member.pid === pid), + memberPids.has(pid), ); for (const failure of failures) mergeProcessOwnershipFailure(unresolved, failure); @@ -1205,8 +1211,12 @@ const createOwnedCleanupPlan = async ( ) signalableGroups.add(processGroupId); } - if (ownership.sweepTokenOwnedProcesses !== true && unresolved.length > 0) - return cleanupValidationFailure(unresolved, tokenOwned.unverified); + const unresolvedFailures = [...unresolved.values()]; + if ( + ownership.sweepTokenOwnedProcesses !== true && + unresolvedFailures.length > 0 + ) + return cleanupValidationFailure(unresolvedFailures, tokenOwned.unverified); return { signalOrder: [...signalableGroups] .filter((groupId) => processGroupIds.has(groupId)) @@ -1218,28 +1228,23 @@ const createOwnedCleanupPlan = async ( : left - right, ), tokenOwnedIdentities: tokenOwned.identities, - unresolved, + unresolved: unresolvedFailures, unverified: tokenOwned.unverified, }; }; /** Keep one current, most useful ownership diagnosis for each live PID. */ const mergeProcessOwnershipFailure = ( - failures: ProcessOwnershipValidationFailure[], + failures: Map, next: ProcessOwnershipValidationFailure, ): void => { - const index = failures.findIndex(({ pid }) => pid === next.pid); - if (index < 0) { - failures.push(next); - return; - } - const previous = failures[index]; + const previous = failures.get(next.pid); if ( - previous !== undefined && + previous === undefined || ownershipFailurePriority(next.reason) >= ownershipFailurePriority(previous.reason) ) - failures[index] = next; + failures.set(next.pid, next); }; const ownershipFailurePriority = ( @@ -1888,7 +1893,7 @@ const scanWithOwnershipEvidence = async ( const retainedByPid = new Map( retained.map((candidate) => [candidate.pid, candidate]), ); - const failures: ProcessOwnershipValidationFailure[] = []; + const failures = new Map(); const unverified: OwnershipSweepUnverifiedProcess[] = []; const owned: ProcessTableEntry[] = []; const identities = new Map(); @@ -1900,13 +1905,13 @@ const scanWithOwnershipEvidence = async ( // cleanup failure; table omission alone is never proof of exit. if (processIdentityHasExited(fact.identityAfter)) continue; if (fact.state === "replaced") - failures.push({ + mergeProcessOwnershipFailure(failures, { pid: fact.process.pid, reason: "process-identity-unavailable", diagnostic: "process identity changed during token validation", }); else if (fact.state === "unavailable") - failures.push({ + mergeProcessOwnershipFailure(failures, { pid: fact.process.pid, reason: "process-identity-unavailable", diagnostic: "process identity was unavailable during token validation", @@ -1919,7 +1924,7 @@ const scanWithOwnershipEvidence = async ( fact.token.runId !== runId && retainedCandidate !== undefined ) - failures.push({ + mergeProcessOwnershipFailure(failures, { pid: fact.process.pid, // An earlier observation without an identity cannot bind that // observation to this currently readable process generation. @@ -1974,7 +1979,10 @@ const scanWithOwnershipEvidence = async ( fact.identityAfter.state === "readable" && fact.identityAfter.identity === retainedCandidate.identity) ) - failures.push(unreadableTokenFailure(fact, fact.token.reason)); + mergeProcessOwnershipFailure( + failures, + unreadableTokenFailure(fact, fact.token.reason), + ); } for (const candidate of retained) { @@ -1982,9 +1990,9 @@ const scanWithOwnershipEvidence = async ( if (fact?.state !== "same-generation") { if ( fact?.identityAfter.state !== "absent" && - !failures.some(({ pid }) => pid === candidate.pid) + !failures.has(candidate.pid) ) - mergeProcessOwnershipFailure(failures, { + failures.set(candidate.pid, { pid: candidate.pid, reason: "process-identity-unavailable", diagnostic: "retained candidate identity is not currently verified", @@ -2005,7 +2013,7 @@ const scanWithOwnershipEvidence = async ( ) continue; if (identity.relation?.state !== "observed") { - failures.push({ + mergeProcessOwnershipFailure(failures, { pid: candidate.pid, reason: "process-identity-unavailable", diagnostic: "native process membership was unavailable", @@ -2020,10 +2028,7 @@ const scanWithOwnershipEvidence = async ( identities.set(candidate.pid, candidate.identity); } - const mergedFailures: ProcessOwnershipValidationFailure[] = []; - for (const failure of failures) - mergeProcessOwnershipFailure(mergedFailures, failure); - return { owned, failures: mergedFailures, unverified, identities }; + return { owned, failures: [...failures.values()], unverified, identities }; }; /** diff --git a/src/process/ProcessOwnershipObservation.ts b/src/process/ProcessOwnershipObservation.ts index 4434e51632..53709cf7bf 100644 --- a/src/process/ProcessOwnershipObservation.ts +++ b/src/process/ProcessOwnershipObservation.ts @@ -110,6 +110,7 @@ export const observeOwnedProcessGroups = async ( host, signal, ); + const groupIdSet = new Set(groupIds); const candidateProcesses = observationRows.filter((process) => { if (retainedPids.has(process.pid)) return true; const identity = firstIdentities.get(process.pid); @@ -117,12 +118,12 @@ export const observeOwnedProcessGroups = async ( if (processIdentityHasExited(identity)) return false; if ( identity.relation?.state === "observed" && - groupIds.includes(identity.relation.processGroupId) + groupIdSet.has(identity.relation.processGroupId) ) return true; - return groupIds.includes(process.processGroupId); + return groupIdSet.has(process.processGroupId); } - return groupIds.includes(process.processGroupId); + return groupIdSet.has(process.processGroupId); }); const facts = await readProcessOwnershipFacts( candidateProcesses, @@ -135,7 +136,7 @@ export const observeOwnedProcessGroups = async ( listedRows: processes.length, listedPids: new Set(processes.map(({ pid }) => pid)), leaderPid: -1, - processGroupIds: new Set(groupIds), + processGroupIds: groupIdSet, scopeFacts: facts, }); const observations = new Map( @@ -146,7 +147,7 @@ export const observeOwnedProcessGroups = async ( // An initial unreadable membership remains uncertain only while the // final native fact does not establish that this candidate has exited. if ( - groupIds.includes(fact.process.processGroupId) && + groupIdSet.has(fact.process.processGroupId) && (fact.identityBefore.state !== "readable" || fact.identityBefore.relation?.state !== "observed") ) @@ -159,13 +160,16 @@ export const observeOwnedProcessGroups = async ( fact.identityAfter.relation?.state === "observed" ? fact.identityAfter.relation.processGroupId : undefined; - const candidateGroups = groupIds.filter( - (groupId) => - nativeGroupId === groupId || - (nativeGroupId === undefined && - fact.process.processGroupId === groupId), - ); - for (const groupId of candidateGroups) { + const matchedGroupId = + nativeGroupId !== undefined + ? groupIdSet.has(nativeGroupId) + ? nativeGroupId + : undefined + : groupIdSet.has(fact.process.processGroupId) + ? fact.process.processGroupId + : undefined; + if (matchedGroupId !== undefined) { + const groupId = matchedGroupId; if (observations.get(groupId)?.state === "unverifiable") continue; if ( fact.state !== "same-generation" || @@ -349,6 +353,9 @@ export const observeOwnedProcessLineage = async ( latestRefreshPids = new Set(refreshed.map(({ pid }) => pid)); } } + const refreshDescendantPids = new Set( + refreshDescendants.map(({ pid }) => pid), + ); return { status: "verified", observedAt: new Date().toISOString(), @@ -362,7 +369,7 @@ export const observeOwnedProcessLineage = async ( ({ pid }) => (latestRefreshPids === undefined || latestRefreshPids.has(pid)) && !suppressedRefreshPids.has(pid) && - !refreshDescendants.some((descendant) => descendant.pid === pid), + !refreshDescendantPids.has(pid), ), ...refreshDescendants, ] @@ -1593,17 +1600,20 @@ export const createSystemProcessOwnershipHost = ( ({ pid }) => identities.get(pid)?.state !== "readable", ); if (unreadable.length > 0) { + const unreadablePids = new Set(unreadable.map(({ pid }) => pid)); const stillLive = liveProcesses(await safeListProcesses(signal)).filter( - ({ pid }) => unreadable.some((entry) => entry.pid === pid), + ({ pid }) => unreadablePids.has(pid), ); const retried = await processIdentities(stillLive, signal); identities = new Map([...identities, ...retried]); const unresolved = stillLive.filter( ({ pid }) => identities.get(pid)?.state !== "readable", ); - const liveAfterRetry = liveProcesses(await safeListProcesses(signal)); + const liveAfterRetryPids = new Set( + liveProcesses(await safeListProcesses(signal)).map(({ pid }) => pid), + ); const stillUnresolved = unresolved.filter(({ pid }) => - liveAfterRetry.some((entry) => entry.pid === pid), + liveAfterRetryPids.has(pid), ); if (stillUnresolved.length > 0) throw new ProcessAdmissionError("identity_snapshot_unavailable", {