fix(mcp): retry failed shutdown resources and retain failure reasons
This commit is contained in:
+86
-31
@@ -8,6 +8,11 @@ import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { RuntimeDependencies } from "./types.js";
|
||||
import { MCP_SHUTDOWN_FAILED } from "./messages.js";
|
||||
|
||||
interface ShutdownCleanupOwner {
|
||||
readonly close: () => Promise<void>;
|
||||
closed: boolean;
|
||||
}
|
||||
|
||||
export const createShutdown = (input: {
|
||||
readonly handle: StdioServerHandle;
|
||||
readonly closeAndroid?: () => Promise<void>;
|
||||
@@ -19,22 +24,51 @@ export const createShutdown = (input: {
|
||||
readonly request: () => void;
|
||||
} => {
|
||||
const { handle, session, dependencies, serverLogger } = input;
|
||||
let shutdownPromise: Promise<void> | undefined;
|
||||
let shutdownAttempt: Promise<void> | undefined;
|
||||
const cleanupOwners: ShutdownCleanupOwner[] = [
|
||||
{ close: () => handle.close(), closed: false },
|
||||
...(input.closeAndroid === undefined
|
||||
? []
|
||||
: [{ close: input.closeAndroid, closed: false }]),
|
||||
{
|
||||
close: async () => {
|
||||
const closed = await session.close({ retainProviderDocuments: true });
|
||||
if (!closed.ok) throw closed.error;
|
||||
},
|
||||
closed: false,
|
||||
},
|
||||
];
|
||||
let shutdownUnregistered = false;
|
||||
let unregisterShutdown = (): void => undefined;
|
||||
const shutdown = async (): Promise<void> => {
|
||||
shutdownPromise ??= (async () => {
|
||||
unregisterShutdown();
|
||||
const results = await Promise.allSettled([
|
||||
handle.close(),
|
||||
input.closeAndroid?.(),
|
||||
session.close({ retainProviderDocuments: true }).then((closed) => {
|
||||
if (!closed.ok) throw closed.error;
|
||||
if (shutdownAttempt !== undefined) return shutdownAttempt;
|
||||
const pending = cleanupOwners.filter(({ closed }) => !closed);
|
||||
const attempt = (async () => {
|
||||
const results = await Promise.allSettled(
|
||||
pending.map(async (owner) => {
|
||||
await owner.close();
|
||||
owner.closed = true;
|
||||
}),
|
||||
]);
|
||||
);
|
||||
const failures: unknown[] = [];
|
||||
for (const result of results)
|
||||
if (result.status === "rejected") throw result.reason;
|
||||
if (result.status === "rejected") failures.push(result.reason);
|
||||
if (failures.length === 1) throw failures[0];
|
||||
if (failures.length > 1)
|
||||
throw new AggregateError(
|
||||
failures,
|
||||
"Multiple MCP cleanup operations failed",
|
||||
);
|
||||
if (!shutdownUnregistered) {
|
||||
unregisterShutdown();
|
||||
shutdownUnregistered = true;
|
||||
}
|
||||
})();
|
||||
return shutdownPromise;
|
||||
shutdownAttempt = attempt;
|
||||
void attempt.catch(() => {
|
||||
if (shutdownAttempt === attempt) shutdownAttempt = undefined;
|
||||
});
|
||||
return attempt;
|
||||
};
|
||||
const requestShutdown = (): void => {
|
||||
shutdown().catch((cause: unknown) => {
|
||||
@@ -55,26 +89,47 @@ export const createShutdown = (input: {
|
||||
|
||||
const describeShutdownFailure = (
|
||||
cause: unknown,
|
||||
): Readonly<Record<string, JsonValue>> =>
|
||||
describeShutdownFailureNode(cause, new WeakSet<object>());
|
||||
|
||||
const describeShutdownFailureNode = (
|
||||
cause: unknown,
|
||||
active: WeakSet<object>,
|
||||
): Readonly<Record<string, JsonValue>> => {
|
||||
if (cause instanceof AnalysisError)
|
||||
return { analysis_error: projectAnalysisError(cause) };
|
||||
if (cause instanceof Error) {
|
||||
const code = "code" in cause ? cause.code : undefined;
|
||||
return {
|
||||
name: cause.name,
|
||||
message: cause.message,
|
||||
...(typeof code === "string" ||
|
||||
(typeof code === "number" && Number.isFinite(code))
|
||||
? { code }
|
||||
: {}),
|
||||
};
|
||||
if (typeof cause === "object" && cause !== null) {
|
||||
if (active.has(cause)) return { type: "cyclic_error" };
|
||||
active.add(cause);
|
||||
}
|
||||
try {
|
||||
if (cause instanceof AnalysisError)
|
||||
return { analysis_error: projectAnalysisError(cause) };
|
||||
if (cause instanceof Error) {
|
||||
const code = "code" in cause ? cause.code : undefined;
|
||||
return {
|
||||
name: cause.name,
|
||||
message: cause.message,
|
||||
...(typeof code === "string" ||
|
||||
(typeof code === "number" && Number.isFinite(code))
|
||||
? { code }
|
||||
: {}),
|
||||
...(cause instanceof AggregateError
|
||||
? {
|
||||
errors: cause.errors.map((error: unknown) =>
|
||||
describeShutdownFailureNode(error, active),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
if (
|
||||
cause === null ||
|
||||
typeof cause === "string" ||
|
||||
typeof cause === "boolean" ||
|
||||
(typeof cause === "number" && Number.isFinite(cause))
|
||||
)
|
||||
return { type: cause === null ? "null" : typeof cause, value: cause };
|
||||
return { type: typeof cause };
|
||||
} finally {
|
||||
if (typeof cause === "object" && cause !== null) active.delete(cause);
|
||||
}
|
||||
if (
|
||||
cause === null ||
|
||||
typeof cause === "string" ||
|
||||
typeof cause === "boolean" ||
|
||||
(typeof cause === "number" && Number.isFinite(cause))
|
||||
)
|
||||
return { type: cause === null ? "null" : typeof cause, value: cause };
|
||||
return { type: typeof cause };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user