fix(process): preserve discovery ownership across cleanup retries (#2643)
Bind retained descriptors and runtime resources to their admitting operation until cleanup succeeds, then rebind newly admitted work. Verify failed retries, successful recovery, shutdown and CLI projection. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
@@ -175,7 +175,7 @@ export class TsharkCaptureProvider implements PacketCapturePort {
|
||||
];
|
||||
phase = "workspace";
|
||||
root = await this.createRuntime();
|
||||
this.#resources.retain(root, undefined, operation);
|
||||
this.#resources.retain(root, undefined);
|
||||
const snapshotPath = join(root.path, "capture");
|
||||
const configurationPath = join(root.path, "configuration");
|
||||
await mkdir(configurationPath, { mode: 0o700 });
|
||||
@@ -406,7 +406,7 @@ export class TsharkCaptureProvider implements PacketCapturePort {
|
||||
cause instanceof OwnedCommandFailure &&
|
||||
cause.cleanupOwner !== undefined
|
||||
)
|
||||
this.#resources.retain(root, cause.cleanupOwner, operation);
|
||||
this.#resources.retain(root, cause.cleanupOwner);
|
||||
result = err(
|
||||
this.#failure(
|
||||
cause,
|
||||
|
||||
@@ -33,7 +33,10 @@ export type ArtifactAnalysisRuntime = Pick<
|
||||
|
||||
/** Own admission, artifact descriptors, producer cleanup and private workspaces together. */
|
||||
export class ArtifactAnalysisResources {
|
||||
readonly #artifacts = new ArtifactResourceScope();
|
||||
readonly #artifacts: {
|
||||
readonly scope: ArtifactResourceScope;
|
||||
operation: string;
|
||||
};
|
||||
readonly #pending = new Map<
|
||||
ArtifactAnalysisRuntime,
|
||||
{
|
||||
@@ -54,7 +57,9 @@ export class ArtifactAnalysisResources {
|
||||
readonly message: string;
|
||||
readonly details: Readonly<Record<string, JsonValue>>;
|
||||
},
|
||||
) {}
|
||||
) {
|
||||
this.#artifacts = { scope: new ArtifactResourceScope(), operation };
|
||||
}
|
||||
|
||||
run<T>(
|
||||
work: () => Promise<Result<T, AnalysisError>>,
|
||||
@@ -76,8 +81,15 @@ export class ArtifactAnalysisResources {
|
||||
);
|
||||
this.#busy = true;
|
||||
const running = this.#tail
|
||||
.then(() => this.#artifacts.run(work))
|
||||
.catch((cause: unknown) => err(this.#artifactFailure(cause, operation)))
|
||||
.then(() =>
|
||||
this.#artifacts.scope.run(() => {
|
||||
// Failed descriptor cleanup still belongs to the preceding request until
|
||||
// this scope has drained it and admits acquisition for the new request.
|
||||
this.#artifacts.operation = operation;
|
||||
return work();
|
||||
}),
|
||||
)
|
||||
.catch((cause: unknown) => err(this.#artifactFailure(cause)))
|
||||
.finally(() => {
|
||||
this.#busy = false;
|
||||
});
|
||||
@@ -94,20 +106,23 @@ export class ArtifactAnalysisResources {
|
||||
maximumBytes,
|
||||
signal,
|
||||
undefined,
|
||||
this.#artifacts,
|
||||
this.#artifacts.scope,
|
||||
);
|
||||
}
|
||||
|
||||
readLimits(limits: LinuxResourceLimits): Promise<LinuxResourceLimits> {
|
||||
return this.readKernelLimits(limits, this.#artifacts);
|
||||
return this.readKernelLimits(limits, this.#artifacts.scope);
|
||||
}
|
||||
|
||||
retain(
|
||||
root: ArtifactAnalysisRuntime,
|
||||
owner?: ProviderProcessSupervisor,
|
||||
operation = this.operation,
|
||||
): void {
|
||||
this.#pending.set(root, { owner, operation });
|
||||
this.#pending.set(root, {
|
||||
owner,
|
||||
operation:
|
||||
this.#pending.get(root)?.operation ?? this.#artifacts.operation,
|
||||
});
|
||||
}
|
||||
|
||||
get pendingPaths(): readonly string[] {
|
||||
@@ -251,7 +266,7 @@ export class ArtifactAnalysisResources {
|
||||
async #closeResources(): Promise<void> {
|
||||
let descriptorFailure: AnalysisError | undefined;
|
||||
try {
|
||||
await this.#artifacts.close();
|
||||
await this.#artifacts.scope.close();
|
||||
} catch (cause: unknown) {
|
||||
descriptorFailure = this.#artifactFailure(cause);
|
||||
}
|
||||
@@ -262,14 +277,17 @@ export class ArtifactAnalysisResources {
|
||||
throw analysisErrorWithCleanupFailure(
|
||||
descriptorFailure,
|
||||
cause,
|
||||
this.operation,
|
||||
this.#artifacts.operation,
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
if (descriptorFailure !== undefined) throw descriptorFailure;
|
||||
}
|
||||
|
||||
#artifactFailure(cause: unknown, operation = this.operation): AnalysisError {
|
||||
#artifactFailure(
|
||||
cause: unknown,
|
||||
operation = this.#artifacts.operation,
|
||||
): AnalysisError {
|
||||
if (cause instanceof AnalysisError) return cause;
|
||||
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined)
|
||||
return this.cleanupFailure(cause.cleanup, cause, undefined, operation);
|
||||
|
||||
@@ -173,7 +173,7 @@ export class SigrokRecordingProvider implements SignalRecordingPort {
|
||||
];
|
||||
phase = "workspace";
|
||||
root = await this.createRuntime();
|
||||
this.#resources.retain(root, undefined, operation);
|
||||
this.#resources.retain(root, undefined);
|
||||
const snapshotPath = join(root.path, "recording");
|
||||
const execute = async (args: readonly string[]) => {
|
||||
const output = await runOwnedCommand(
|
||||
@@ -371,7 +371,7 @@ export class SigrokRecordingProvider implements SignalRecordingPort {
|
||||
cause instanceof OwnedCommandFailure &&
|
||||
cause.cleanupOwner !== undefined
|
||||
)
|
||||
this.#resources.retain(root, cause.cleanupOwner, operation);
|
||||
this.#resources.retain(root, cause.cleanupOwner);
|
||||
result = err(
|
||||
this.#failure(
|
||||
cause,
|
||||
|
||||
Reference in New Issue
Block a user