mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-28 12:03:32 +00:00
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
/** Max time allowed for plugin host cleanup hooks before failing shutdown. */
|
|
export const PLUGIN_HOST_CLEANUP_TIMEOUT_MS = 5_000;
|
|
|
|
/** Error raised when a plugin host cleanup hook exceeds the shutdown timeout. */
|
|
export class PluginHostCleanupTimeoutError extends Error {
|
|
constructor(hookId: string) {
|
|
super(`plugin host cleanup timed out: ${hookId}`);
|
|
this.name = "PluginHostCleanupTimeoutError";
|
|
}
|
|
}
|
|
|
|
/** Runs plugin host cleanup with a bounded timeout and clears the timer afterward. */
|
|
export async function withPluginHostCleanupTimeout<T>(
|
|
hookId: string,
|
|
cleanup: () => T | Promise<T>,
|
|
): Promise<T> {
|
|
let timeout: NodeJS.Timeout | undefined;
|
|
try {
|
|
return await Promise.race([
|
|
Promise.resolve().then(cleanup),
|
|
new Promise<never>((_, reject) => {
|
|
timeout = setTimeout(() => {
|
|
reject(new PluginHostCleanupTimeoutError(hookId));
|
|
}, PLUGIN_HOST_CLEANUP_TIMEOUT_MS);
|
|
timeout.unref?.();
|
|
}),
|
|
]);
|
|
} finally {
|
|
if (timeout) {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|
|
}
|