perf(process): share batched token observation across ownership checks
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from "./ProcessOwnershipObservation.js";
|
||||
import { launcherIdentityFailure } from "./ProcessOwnershipIdentity.js";
|
||||
import { descendantsOf, liveProcesses } from "./ProcessOwnershipProcessTree.js";
|
||||
import { readProcessRunTokens } from "./ProcessRunTokenObservations.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
@@ -90,10 +91,14 @@ export interface ProcessOwnershipHost {
|
||||
close?(): Promise<void>;
|
||||
/** List current processes; startup inspection may be cancelled by the caller. */
|
||||
listProcesses(signal?: AbortSignal): Promise<readonly ProcessTableEntry[]>;
|
||||
environment(pid: number): Promise<Readonly<Record<string, string>>>;
|
||||
environment(
|
||||
pid: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Readonly<Record<string, string>>>;
|
||||
/** Read only run-token values in one host operation when the OS supports it. */
|
||||
runTokens?(
|
||||
processes: readonly ProcessTableEntry[],
|
||||
signal?: AbortSignal,
|
||||
): Promise<ReadonlyMap<number, ProcessRunTokenObservation>>;
|
||||
/** Read stable identities; omit the signal during cleanup so it can finish. */
|
||||
processIdentities?(
|
||||
@@ -678,29 +683,10 @@ const processRunTokenFailures = async (
|
||||
host: ProcessOwnershipHost,
|
||||
): Promise<readonly ProcessOwnershipValidationFailure[]> => {
|
||||
const failures: ProcessOwnershipValidationFailure[] = [];
|
||||
let runTokenObservations:
|
||||
| ReadonlyMap<number, ProcessRunTokenObservation>
|
||||
| undefined;
|
||||
let runTokenReadFailure: string | undefined;
|
||||
if (host.runTokens !== undefined) {
|
||||
try {
|
||||
runTokenObservations = await host.runTokens(members);
|
||||
} catch (cause: unknown) {
|
||||
runTokenReadFailure = errorMessage(cause);
|
||||
}
|
||||
}
|
||||
for (const member of members) {
|
||||
const batchObservation = runTokenObservations?.get(member.pid);
|
||||
let observation =
|
||||
batchObservation ?? (await readEnvironmentRunToken(host, member.pid));
|
||||
if (
|
||||
runTokenReadFailure !== undefined &&
|
||||
observation.state === "unavailable"
|
||||
)
|
||||
observation = {
|
||||
state: "unavailable",
|
||||
reason: `${observation.reason}; run-token batch failed: ${runTokenReadFailure}`,
|
||||
};
|
||||
for await (const { process: member, observation } of readProcessRunTokens(
|
||||
host,
|
||||
members,
|
||||
)) {
|
||||
if (observation.state === "readable") {
|
||||
if (observation.runId !== runId)
|
||||
failures.push({ pid: member.pid, reason: "run-token-mismatch" });
|
||||
@@ -823,26 +809,14 @@ const scanTokenOwnedProcesses = async (
|
||||
identity?.state === "readable" ? identity.identity : null,
|
||||
);
|
||||
};
|
||||
let bulkTokens: ReadonlyMap<number, ProcessRunTokenObservation> | undefined;
|
||||
if (host.runTokens !== undefined)
|
||||
bulkTokens = await host.runTokens(candidates);
|
||||
const unreadable: UnreadableTokenCandidate[] = [];
|
||||
for (const process of candidates) {
|
||||
const bulkToken = bulkTokens?.get(process.pid);
|
||||
if (bulkToken !== undefined) {
|
||||
if (bulkToken.state === "readable") {
|
||||
if (bulkToken.runId === runId) recordOwned(process);
|
||||
continue;
|
||||
}
|
||||
unreadable.push({ process, diagnostic: bulkToken.reason });
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if ((await host.environment(process.pid)).REA_PROCESS_RUN_ID === runId)
|
||||
recordOwned(process);
|
||||
} catch (cause: unknown) {
|
||||
unreadable.push({ process, diagnostic: errorMessage(cause) });
|
||||
}
|
||||
for await (const { process, observation } of readProcessRunTokens(
|
||||
host,
|
||||
candidates,
|
||||
)) {
|
||||
if (observation.state === "readable") {
|
||||
if (observation.runId === runId) recordOwned(process);
|
||||
} else unreadable.push({ process, diagnostic: observation.reason });
|
||||
}
|
||||
const remaining = await settleUnreadableTokenCandidates(
|
||||
unreadable,
|
||||
@@ -968,31 +942,20 @@ const settleUnreadableTokenCandidates = async (
|
||||
void cause;
|
||||
live = undefined;
|
||||
}
|
||||
let observations:
|
||||
| ReadonlyMap<number, ProcessRunTokenObservation>
|
||||
| undefined;
|
||||
if (host.runTokens !== undefined) {
|
||||
try {
|
||||
observations = await host.runTokens(
|
||||
remaining.map(({ process }) => process),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
void cause;
|
||||
observations = undefined;
|
||||
}
|
||||
}
|
||||
const stillUnreadable: UnreadableTokenCandidate[] = [];
|
||||
for (const candidate of remaining) {
|
||||
const pid = candidate.process.pid;
|
||||
if (live !== undefined && !live.has(pid)) continue;
|
||||
const observation =
|
||||
observations?.get(pid) ?? (await readEnvironmentRunToken(host, pid));
|
||||
const candidates = remaining
|
||||
.map(({ process }) => process)
|
||||
.filter(({ pid }) => live === undefined || live.has(pid));
|
||||
for await (const { process, observation } of readProcessRunTokens(
|
||||
host,
|
||||
candidates,
|
||||
)) {
|
||||
if (observation.state === "readable") {
|
||||
if (observation.runId === runId) recordOwned(candidate.process);
|
||||
if (observation.runId === runId) recordOwned(process);
|
||||
continue;
|
||||
}
|
||||
stillUnreadable.push({
|
||||
process: candidate.process,
|
||||
process,
|
||||
diagnostic: observation.reason,
|
||||
});
|
||||
}
|
||||
@@ -1001,20 +964,6 @@ const settleUnreadableTokenCandidates = async (
|
||||
return remaining;
|
||||
};
|
||||
|
||||
const readEnvironmentRunToken = async (
|
||||
host: ProcessOwnershipHost,
|
||||
pid: number,
|
||||
): Promise<ProcessRunTokenObservation> => {
|
||||
try {
|
||||
return {
|
||||
state: "readable",
|
||||
runId: (await host.environment(pid)).REA_PROCESS_RUN_ID,
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
return { state: "unavailable", reason: errorMessage(cause) };
|
||||
}
|
||||
};
|
||||
|
||||
const processIsGone = async (
|
||||
host: ProcessOwnershipHost,
|
||||
pid: number,
|
||||
|
||||
Reference in New Issue
Block a user