Files
rea/src/resources/AnalysisCleanupLease.ts
T
morluto 7107abf8b8 fix(adb): run adb commands under owned supervision and start the adb server explicitly (#11772)
* fix(adb): run adb commands under owned supervision and start the adb server explicitly

The adb version probe and device commands now run through runOwnedCommand
in their own process group, keeping adb's SIGTERM-first stop through the
termination policy. A device command's escaped descendants are swept with
its cleanup, and cleanup that fails stays owned by the provider, which
retries it before its next operation.

Device operations start the adb server with an explicit `adb start-server`
first. The server is an external shared service: REA may start it but never
stops it, so a device command never forks it into its own cleanup.

runOwnedCommand gains a byte-limited stdout collector: output beyond
maxStdoutBytes is an output-limit failure, like an exceeded diagnostic
budget, and the collected bytes are kept exactly for binary protocols such
as screencap. Ownership preparation that cannot start its own helper for
lack of host capacity is reported as the command's host-capacity launch
failure.

* fix(adb): preserve completed refusals, server startup facts, and lazy server start

* fix(adb): scope runtime-root command cleanup and preserve incomplete command cleanup

* fix(adb): preserve completed bugreports and staging directories across command cleanup failures

* fix(adb): preserve bugreport command cleanup across readback failures and project live cleanup resources
2026-11-16 21:13:43 -05:00

146 lines
4.4 KiB
TypeScript

import type {
AnalysisError,
AnalysisCleanupReleaseClaim,
AnalysisCleanupResourceOwner,
} from "../domain/analysisErrorBase.js";
/** Link one resource's confirmed release to its originating analysis error. */
export class AnalysisCleanupLease {
#released = false;
#releasePromise: Promise<void> | undefined;
readonly #groups = new Set<AnalysisCleanupLeaseGroup>();
/** Whether this exact owner has confirmed release. */
get released(): boolean {
return this.#released;
}
readonly #resourceOwner: AnalysisCleanupResourceOwner | undefined;
readonly #resources: readonly string[] | undefined;
get cleanupResources(): readonly string[] | undefined {
if (this.#released) return [];
return this.#resourceOwner === undefined
? this.#resources
: this.#resourceOwner.cleanupResources;
}
/** Adopt exact close authority while retaining its live labels across retries. */
static forCloseOwner(
owner: {
close(): Promise<void>;
readonly cleanupResources?: readonly string[] | undefined;
},
resource: string,
): AnalysisCleanupLease {
return new AnalysisCleanupLease(
() => owner.close(),
!("cleanupResources" in owner)
? [resource]
: {
get cleanupResources() {
return owner.cleanupResources;
},
},
);
}
constructor(
private readonly cleanup: () => Promise<void>,
resources?: readonly string[] | AnalysisCleanupResourceOwner,
) {
if (resources !== undefined && "cleanupResources" in resources) {
this.#resourceOwner = resources;
this.#resources = undefined;
} else {
this.#resourceOwner = undefined;
this.#resources = resources === undefined ? undefined : [...resources];
}
}
release(): Promise<void> {
if (this.#released) return Promise.resolve();
if (this.#releasePromise !== undefined) return this.#releasePromise;
const attempt = Promise.resolve()
.then(async () => {
await this.cleanup();
this.#released = true;
for (const group of this.#groups) group.ownerReleased(this);
this.#groups.clear();
})
.finally(() => {
if (this.#releasePromise === attempt) this.#releasePromise = undefined;
});
this.#releasePromise = attempt;
return attempt;
}
/** Binding can follow a concurrent retry that already confirmed release. */
bindCleanupError(error: AnalysisError): void {
new AnalysisCleanupLeaseGroup(error, [this], {
preserveResourceAttribution: true,
});
}
/** Bind one aggregate error to every exact lease in a failed retry set. */
bindCleanupGroup(group: AnalysisCleanupLeaseGroup): void {
if (this.#released) {
group.ownerReleased(this);
return;
}
for (const retained of this.#groups)
if (!retained.hasLiveError) this.#groups.delete(retained);
this.#groups.add(group);
}
}
/**
* Conjunctive cleanup binding for an error covering multiple exact owners.
* The error reads `cleanupReleased` when asked; release is never pushed to it.
*/
export class AnalysisCleanupLeaseGroup implements AnalysisCleanupReleaseClaim {
readonly #error: WeakRef<AnalysisError>;
readonly #pending: Set<AnalysisCleanupLease>;
constructor(
error: AnalysisError,
owners: readonly AnalysisCleanupLease[],
options: { readonly preserveResourceAttribution?: boolean } = {},
) {
this.#error = new WeakRef(error);
this.#pending = new Set(owners);
if (this.#pending.size === 0) return;
error.bindCleanupReleaseClaim(this);
if (options.preserveResourceAttribution === true)
error.bindCleanupCompletionOwner(this);
else error.bindCleanupResourceOwner(this);
for (const owner of this.#pending) owner.bindCleanupGroup(this);
}
/** Unlabelled owners keep attribution unknown; never match resources by text. */
get cleanupResources(): readonly string[] | undefined {
if (
[...this.#pending].some((owner) => owner.cleanupResources === undefined)
)
return undefined;
return [
...new Set(
[...this.#pending].flatMap((owner) => owner.cleanupResources ?? []),
),
];
}
get hasLiveError(): boolean {
return this.#error.deref() !== undefined;
}
/** Every owner in the set confirmed release. */
get cleanupReleased(): boolean {
return this.#pending.size === 0;
}
ownerReleased(owner: AnalysisCleanupLease): void {
this.#pending.delete(owner);
}
}