( name: string, timeoutMs: number, operation: () => Promise<T>, )
| 54 | } |
| 55 | |
| 56 | async function runStep<T>( |
| 57 | name: string, |
| 58 | timeoutMs: number, |
| 59 | operation: () => Promise<T>, |
| 60 | ): Promise<ShutdownStepOutcome<T>> { |
| 61 | const startedAt = Date.now(); |
| 62 | let timeoutHandle: NodeJS.Timeout | null = null; |
| 63 | |
| 64 | try { |
| 65 | const timeoutPromise = new Promise<RunStepRaceOutcome<T>>((resolve) => { |
| 66 | timeoutHandle = setTimeout(() => resolve({ kind: 'timed_out' }), timeoutMs); |
| 67 | timeoutHandle.unref?.(); |
| 68 | }); |
| 69 | |
| 70 | const operationOutcome = operation() |
| 71 | .then((value): RunStepRaceOutcome<T> => ({ kind: 'value', value })) |
| 72 | .catch( |
| 73 | (error): RunStepRaceOutcome<T> => ({ |
| 74 | kind: 'error', |
| 75 | error: toErrorMessage(error), |
| 76 | }), |
| 77 | ); |
| 78 | const outcome = await Promise.race([operationOutcome, timeoutPromise]); |
| 79 | |
| 80 | if (outcome.kind === 'timed_out') { |
| 81 | return { |
| 82 | status: 'timed_out', |
| 83 | durationMs: Date.now() - startedAt, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | if (outcome.kind === 'error') { |
| 88 | return { |
| 89 | status: 'failed', |
| 90 | durationMs: Date.now() - startedAt, |
| 91 | error: outcome.error, |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | return { |
| 96 | status: 'completed', |
| 97 | durationMs: Date.now() - startedAt, |
| 98 | value: outcome.value, |
| 99 | }; |
| 100 | } finally { |
| 101 | if (timeoutHandle) { |
| 102 | clearTimeout(timeoutHandle); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | const FAILURE_REASONS: ReadonlySet<McpShutdownReason> = new Set([ |
| 108 | 'startup-failure', |
no test coverage detected