mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
perf(pcap): stat-verify unchanged Wireshark executables and parallelize tool admission (#13500)
* perf(pcap): stat-verify unchanged Wireshark executables and parallelize tool admission (#13284) * fix(pcap): retain cleanup from every tool admission
This commit is contained in:
1 parent
593020442d
commit
fe1c0d6b94
2 files changed
+190
-54
No files matched your search
+126
-54
@@ -1,3 +1,6 @@
|
||||
import type { BigIntStats } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { sameRegularFileState } from "../filesystem/RegularFile.js";
|
||||
import {
|
||||
classifyProviderFailure,
|
||||
type ProviderFailureDialect,
|
||||
@@ -285,9 +288,13 @@ export class PcapProvider {
|
||||
readonly #resources: ArtifactAnalysisResources;
|
||||
private readonly environment: Readonly<Record<string, string | undefined>>;
|
||||
private readonly launcher: PcapCommandLauncher | undefined;
|
||||
private readonly statVerifyToolIdentity: boolean;
|
||||
private readonly wiresharkIdentities = new Map<
|
||||
string,
|
||||
ArtifactCommandPath & { readonly identity: WiresharkExecutableIdentity }
|
||||
ArtifactCommandPath & {
|
||||
readonly identity: WiresharkExecutableIdentity;
|
||||
readonly initialStat: BigIntStats | undefined;
|
||||
}
|
||||
>();
|
||||
private readonly platform: NodeJS.Platform;
|
||||
private readonly snapshots = new Set<PcapSnapshot>();
|
||||
@@ -307,6 +314,7 @@ export class PcapProvider {
|
||||
this.platform,
|
||||
);
|
||||
this.launcher = options.launcher;
|
||||
this.statVerifyToolIdentity = options.artifactReader === undefined;
|
||||
this.#resources = createArtifactAnalysisResources(
|
||||
"pcap",
|
||||
"inspect_pcap_capture",
|
||||
@@ -347,6 +355,41 @@ export class PcapProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private async verifyWiresharkExecutable(
|
||||
operation: PcapOperation,
|
||||
selected: ArtifactCommandPath & {
|
||||
readonly identity: WiresharkExecutableIdentity;
|
||||
readonly initialStat: BigIntStats | undefined;
|
||||
},
|
||||
options?: {
|
||||
readonly signal?: AbortSignal | undefined;
|
||||
readonly capturedOutput?: AnalysisCapturedOutput | undefined;
|
||||
},
|
||||
): Promise<void> {
|
||||
if (this.statVerifyToolIdentity && selected.initialStat !== undefined) {
|
||||
const currentStat = await stat(selected.resolvedPath, {
|
||||
bigint: true,
|
||||
}).catch(() => undefined);
|
||||
if (
|
||||
currentStat !== undefined &&
|
||||
sameRegularFileState(selected.initialStat, currentStat)
|
||||
)
|
||||
return;
|
||||
}
|
||||
const observed = await this.readToolIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
options?.signal,
|
||||
);
|
||||
assertWiresharkExecutableIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
selected.identity,
|
||||
wiresharkExecutableIdentity(observed),
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
private async run(
|
||||
operation: PcapOperation,
|
||||
stage: "version" | "capinfos" | "tshark",
|
||||
@@ -365,17 +408,7 @@ export class PcapProvider {
|
||||
try {
|
||||
if (selected !== undefined) {
|
||||
await assertArtifactCommandPath(selected, operation, { signal });
|
||||
const before = await this.readToolIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
signal,
|
||||
);
|
||||
assertWiresharkExecutableIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
selected.identity,
|
||||
wiresharkExecutableIdentity(before),
|
||||
);
|
||||
await this.verifyWiresharkExecutable(operation, selected, { signal });
|
||||
}
|
||||
output = await runPcapCommand({
|
||||
operation,
|
||||
@@ -416,21 +449,11 @@ export class PcapProvider {
|
||||
return output.capturedOutput;
|
||||
},
|
||||
});
|
||||
const after = await this.readToolIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
);
|
||||
assertWiresharkExecutableIdentity(
|
||||
operation,
|
||||
selected.resolvedPath,
|
||||
selected.identity,
|
||||
wiresharkExecutableIdentity(after),
|
||||
{
|
||||
get capturedOutput() {
|
||||
return output.capturedOutput;
|
||||
},
|
||||
await this.verifyWiresharkExecutable(operation, selected, {
|
||||
get capturedOutput() {
|
||||
return output.capturedOutput;
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
assertValidPcapCommandText(operation, stage, output);
|
||||
} catch (cause) {
|
||||
@@ -511,37 +534,86 @@ export class PcapProvider {
|
||||
private async client(operation: PcapOperation, signal?: AbortSignal) {
|
||||
const selection = resolvePcapTools(this.environment, this.platform);
|
||||
this.wiresharkIdentities.clear();
|
||||
for (const [name, selected] of [
|
||||
["tshark", selection.tshark],
|
||||
["capinfos", selection.capinfos],
|
||||
] as const) {
|
||||
let commandPath: ArtifactCommandPath;
|
||||
try {
|
||||
commandPath = await selectArtifactCommandPath(selected.command, signal);
|
||||
} catch (cause) {
|
||||
if (
|
||||
cause instanceof Error &&
|
||||
"code" in cause &&
|
||||
(cause.code === "ENOENT" || cause.code === "ENOTDIR")
|
||||
)
|
||||
throw new PcapConfigurationFailure(
|
||||
`${name}_missing`,
|
||||
`The selected ${name} executable ${selected.command} could not be found at its configured path.`,
|
||||
{ command: selected.command },
|
||||
{ cause },
|
||||
const admittedTools = await Promise.allSettled(
|
||||
(
|
||||
[
|
||||
["tshark", selection.tshark],
|
||||
["capinfos", selection.capinfos],
|
||||
] as const
|
||||
).map(async ([name, selected]) => {
|
||||
let commandPath: ArtifactCommandPath;
|
||||
try {
|
||||
commandPath = await selectArtifactCommandPath(
|
||||
selected.command,
|
||||
signal,
|
||||
);
|
||||
throw cause;
|
||||
} catch (cause) {
|
||||
if (
|
||||
cause instanceof Error &&
|
||||
"code" in cause &&
|
||||
(cause.code === "ENOENT" || cause.code === "ENOTDIR")
|
||||
)
|
||||
throw new PcapConfigurationFailure(
|
||||
`${name}_missing`,
|
||||
`The selected ${name} executable ${selected.command} could not be found at its configured path.`,
|
||||
{ command: selected.command },
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
const beforeStat = this.statVerifyToolIdentity
|
||||
? await stat(commandPath.resolvedPath, { bigint: true }).catch(
|
||||
() => undefined,
|
||||
)
|
||||
: undefined;
|
||||
const snapshot = await this.readToolIdentity(
|
||||
operation,
|
||||
commandPath.resolvedPath,
|
||||
signal,
|
||||
);
|
||||
const afterStat =
|
||||
beforeStat !== undefined
|
||||
? await stat(commandPath.resolvedPath, { bigint: true }).catch(
|
||||
() => undefined,
|
||||
)
|
||||
: undefined;
|
||||
const initialStat =
|
||||
beforeStat !== undefined &&
|
||||
afterStat !== undefined &&
|
||||
sameRegularFileState(beforeStat, afterStat)
|
||||
? afterStat
|
||||
: undefined;
|
||||
return {
|
||||
command: selected.command,
|
||||
entry: {
|
||||
...commandPath,
|
||||
identity: wiresharkExecutableIdentity(snapshot),
|
||||
initialStat,
|
||||
},
|
||||
};
|
||||
}),
|
||||
);
|
||||
const rejected = admittedTools.filter((item) => item.status === "rejected");
|
||||
const first = rejected[0];
|
||||
if (first !== undefined) {
|
||||
let primary: unknown = first.reason;
|
||||
for (const { reason } of rejected.slice(1)) {
|
||||
if (!(reason instanceof AnalysisError) || !reason.cleanupIncomplete)
|
||||
continue;
|
||||
primary = analysisErrorWithCleanupDependencies(
|
||||
classifyProviderFailure(primary, this.#failureDialect(operation)),
|
||||
reason,
|
||||
operation,
|
||||
);
|
||||
}
|
||||
const snapshot = await this.readToolIdentity(
|
||||
operation,
|
||||
commandPath.resolvedPath,
|
||||
signal,
|
||||
);
|
||||
this.wiresharkIdentities.set(selected.command, {
|
||||
...commandPath,
|
||||
identity: wiresharkExecutableIdentity(snapshot),
|
||||
});
|
||||
throw primary;
|
||||
}
|
||||
for (const settled of admittedTools)
|
||||
if (settled.status === "fulfilled")
|
||||
this.wiresharkIdentities.set(
|
||||
settled.value.command,
|
||||
settled.value.entry,
|
||||
);
|
||||
const identity = await inspectPcapClient(
|
||||
selection,
|
||||
async (command) =>
|
||||
|
||||
@@ -609,6 +609,70 @@ it.skipIf(isWindows).each([
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["availability", "execution"] as const)(
|
||||
"preserves a capinfos cleanup failure after missing TShark during %s",
|
||||
async (entry) => {
|
||||
const root = await createTestTempDirectory("rea-pcap-mixed-admission-");
|
||||
const missing = join(root, "missing-tshark");
|
||||
const capinfosPath = await realpath(capinfosStub);
|
||||
let allowClose = false;
|
||||
const release = vi.fn(async () => {
|
||||
if (!allowClose) throw new Error("controlled capinfos close denial");
|
||||
});
|
||||
const provider = new PcapProvider({
|
||||
environment: {
|
||||
REA_TSHARK_COMMAND: missing,
|
||||
REA_CAPINFOS_COMMAND: capinfosStub,
|
||||
},
|
||||
artifactReader: async (path, _maximumBytes, _signal, _kind, scope) => {
|
||||
expect(path).toBe(capinfosPath);
|
||||
const lease = new AnalysisCleanupLease(release, [path]);
|
||||
scope?.retain({ kind: "lease", resource: path, lease });
|
||||
throw new ArtifactReaderFailure(
|
||||
"io",
|
||||
"capinfos descriptor close denied",
|
||||
{
|
||||
cleanup: { reason: "controlled close denial", resources: [path] },
|
||||
cleanupLeases: [lease],
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
const request = async () => {
|
||||
if (entry === "availability") return provider.inspectAvailability();
|
||||
const result = await provider.execute({
|
||||
operation: "inspect_pcap_client",
|
||||
input: {},
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
return result.value;
|
||||
};
|
||||
try {
|
||||
const result = await request().then(
|
||||
() => {
|
||||
throw new Error("expected retained capinfos cleanup");
|
||||
},
|
||||
(error: unknown) => error,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
cleanupResources: [capinfosPath],
|
||||
cleanupPrimary: { message: expect.stringContaining(missing) },
|
||||
});
|
||||
allowClose = true;
|
||||
await provider.close();
|
||||
expect(release).toHaveBeenCalled();
|
||||
expect(result).toMatchObject({
|
||||
cleanupIncomplete: false,
|
||||
cleanupResources: [],
|
||||
});
|
||||
} finally {
|
||||
allowClose = true;
|
||||
await provider.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(!hasNativeBound)(
|
||||
"keeps cleanupIncomplete true when snapshot closes before a failed primary cleanup releases",
|
||||
async () => {
|
||||
|
||||
Reference in new issue
Block a user