fix(hopper): keep an observed launcher exit over a later cancellation (#5816)

* fix(hopper): keep an observed launcher exit over a later cancellation

`#connect` projected every interrupted wait as `#startupInterruption`
without consulting the launcher the owner already held, so a launcher that
exited 3 before the caller aborted was reported as `HopperCancelledError`.

An exit or signal the owned process already holds is now projected as the
physical startup failure first; a cancellation observed before any exit is
unchanged, and successful exits keep their previous behaviour.

Fixes #5429

* fix(hopper): arbitrate startup exits through process owner

---------

Co-authored-by: XiaoBinGan <[email protected]>
Co-authored-by: morluto <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-23 02:23:16 +08:00
1 parent 87657c6297
commit 5c8d5b2a8a
3 files changed
+273 -26

No files matched your search

+99 -25
View File
@@ -100,6 +100,13 @@ export interface HopperClientOptions {
type HopperStartupResult = Result<HopperServerInfo, AnalysisError>;
type HopperStartupAttempt = SharedStartupAttempt<HopperStartupResult>;
type HopperStartupOutcome =
| { readonly kind: "interrupted" }
| {
readonly kind: "launcher-exit";
readonly exitCode: number | null;
readonly ownsProviderLifetime: boolean;
};
type HopperClientCloseOptions = {
readonly progress?: ProgressReporter;
@@ -134,6 +141,8 @@ export class HopperClient {
#closing = false;
#launcherExitCode: number | null | undefined;
#launcherFailureDiagnostic: HopperStartupFailureDiagnostic | undefined;
#startupOutcome: HopperStartupOutcome | undefined;
#startupSignal: AbortSignal | undefined;
#operationFailure: OperationFailureRecord | undefined;
#startupFailureResult: AnalysisError | undefined;
#runId: string | undefined;
@@ -394,12 +403,22 @@ export class HopperClient {
this.#options.startupTimeoutMs,
signal,
);
this.#startupOutcome = undefined;
this.#startupSignal = deadline.signal;
const onStartupAbort = (): void => {
if (this.#startupOutcome === undefined)
this.#startupOutcome = { kind: "interrupted" };
};
deadline.signal.addEventListener("abort", onStartupAbort, { once: true });
try {
const result = await this.#startWithin(deadline);
return result.ok && deadline.signal.aborted
? this.#startupFailure(await this.#startupInterruption(deadline))
? this.#startupFailure(await this.#interruptionFailure(deadline))
: result;
} finally {
deadline.signal.removeEventListener("abort", onStartupAbort);
if (this.#startupSignal === deadline.signal)
this.#startupSignal = undefined;
deadline.dispose();
}
}
@@ -473,7 +492,7 @@ export class HopperClient {
}
const remainingMs = deadline.remainingMs();
if (remainingMs <= 0) {
return this.#startupFailure(await this.#startupInterruption(deadline));
return this.#startupFailure(await this.#interruptionFailure(deadline));
}
const health = await this.#request(
"health",
@@ -486,7 +505,7 @@ export class HopperClient {
if (!health.ok) {
return this.#startupFailure(
deadline.signal.aborted || health.error instanceof HopperTimeoutError
? await this.#startupInterruption(deadline)
? await this.#interruptionFailure(deadline)
: health.error,
);
}
@@ -497,12 +516,12 @@ export class HopperClient {
} catch (cause: unknown) {
return this.#startupFailure(
deadline.signal.aborted
? await this.#startupInterruption(deadline)
? await this.#interruptionFailure(deadline)
: new HopperStartError({ cause }),
);
}
if (deadline.signal.aborted)
return this.#startupFailure(await this.#startupInterruption(deadline));
return this.#startupFailure(await this.#interruptionFailure(deadline));
return parsed;
}
@@ -788,31 +807,36 @@ export class HopperClient {
deadline: ProviderStartupDeadline,
): Promise<Result<undefined, HopperError>> {
while (deadline.remainingMs() > 0) {
if (deadline.signal.aborted)
return err(await this.#startupInterruption(deadline));
if (this.#closing) return err(new HopperProcessError(null));
if (
this.#launcherExitCode !== undefined &&
this.#resources.launch !== undefined &&
(this.#resources.launch.providerLifetime === "launcher-process" ||
this.#launcherExitCode !== 0)
) {
const failure = await this.#launcherStartupFailure(
this.#resources.launch.providerLifetime === "launcher-process",
this.#launcherExitCode,
deadline,
const observedExit =
this.#startupOutcome?.kind === "launcher-exit"
? this.#startupOutcome
: this.#startupOutcome?.kind === "interrupted"
? undefined
: this.#observedLauncherExit();
// A launcher exit the owned process already holds is a physical startup
// failure; a cancellation that arrives afterwards must not replace it.
if (deadline.signal.aborted && observedExit === undefined)
return err(
this.#startupOutcome?.kind === "interrupted"
? await this.#startupInterruption(deadline)
: await this.#interruptionFailure(deadline),
);
if (this.#closing) return err(new HopperProcessError(null));
if (observedExit !== undefined)
return err(
await this.#launcherStartupFailure(
observedExit.ownsProviderLifetime,
observedExit.exitCode,
deadline,
),
);
return deadline.interruption === "cancelled"
? err(await this.#startupInterruption(deadline))
: err(failure);
}
try {
await access(socketPath);
} catch (cause: unknown) {
// best-effort cleanup: socket polling; absence means keep waiting.
void cause;
if ((await deadline.wait(50)) === "aborted")
return err(await this.#startupInterruption(deadline));
return err(await this.#interruptionFailure(deadline));
continue;
}
const attempt = await connectHopperSocketOnce(
@@ -825,9 +849,46 @@ export class HopperClient {
return ok(undefined);
}
if ((await deadline.wait(50)) === "aborted")
return err(await this.#startupInterruption(deadline));
return err(await this.#interruptionFailure(deadline));
}
return err(await this.#startupInterruption(deadline));
return err(await this.#interruptionFailure(deadline));
}
/** A launcher exit the owned process already holds, when it is a failure. */
#observedLauncherExit():
| {
readonly exitCode: number | null;
readonly ownsProviderLifetime: boolean;
}
| undefined {
const launch = this.#resources.launch;
if (launch === undefined) return undefined;
const exit = this.#resources.processSupervisor?.observedExit();
if (exit === undefined) return undefined;
const exitCode = exit.code;
const ownsProviderLifetime = launch.providerLifetime === "launcher-process";
return ownsProviderLifetime || exitCode === null || exitCode !== 0
? { exitCode, ownsProviderLifetime }
: undefined;
}
/** Project an interruption, keeping an exit the owner already observed. */
async #interruptionFailure(
deadline: ProviderStartupDeadline,
): Promise<HopperError> {
const observed =
this.#startupOutcome?.kind === "launcher-exit"
? this.#startupOutcome
: this.#startupOutcome?.kind === "interrupted"
? undefined
: this.#observedLauncherExit();
return observed === undefined
? this.#startupInterruption(deadline)
: this.#launcherStartupFailure(
observed.ownsProviderLifetime,
observed.exitCode,
deadline,
);
}
async #startupInterruption(
@@ -1070,6 +1131,19 @@ export class HopperClient {
if (event.type !== "exit") return;
this.#launcherExitCode = event.code;
this.#launcherFailureDiagnostic = hopperLauncherFailureDiagnostic(event);
if (
this.#startupSignal !== undefined &&
this.#startupOutcome === undefined
) {
const ownsProviderLifetime =
launch.providerLifetime === "launcher-process";
if (ownsProviderLifetime || event.code === null || event.code !== 0)
this.#startupOutcome = {
kind: "launcher-exit",
exitCode: event.code,
ownsProviderLifetime,
};
}
this.#options.onDiagnostic?.({
type: "launcher-exit",
code: event.code,