mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 03:20:46 +00:00
Merged via squash.
Prepared head SHA: ec58a6212b
Co-authored-by: 100yenadmin <239388517+100yenadmin@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
31 lines
803 B
TypeScript
31 lines
803 B
TypeScript
export const PLUGIN_HOST_CLEANUP_TIMEOUT_MS = 5_000;
|
|
|
|
export class PluginHostCleanupTimeoutError extends Error {
|
|
constructor(hookId: string) {
|
|
super(`plugin host cleanup timed out: ${hookId}`);
|
|
this.name = "PluginHostCleanupTimeoutError";
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|