diff --git a/.changeset/rude-pianos-marry.md b/.changeset/rude-pianos-marry.md new file mode 100644 index 000000000..38d9aea5e --- /dev/null +++ b/.changeset/rude-pianos-marry.md @@ -0,0 +1,5 @@ +--- +"@voltagent/serverless-hono": patch +--- + +fix(serverless-hono): withWaitUntil must not destroy global state when context has no waitUntil diff --git a/packages/serverless-hono/src/utils/wait-until-wrapper.ts b/packages/serverless-hono/src/utils/wait-until-wrapper.ts index 4ef4dcb78..c555e629e 100644 --- a/packages/serverless-hono/src/utils/wait-until-wrapper.ts +++ b/packages/serverless-hono/src/utils/wait-until-wrapper.ts @@ -34,20 +34,21 @@ export function withWaitUntil(context?: WaitUntilContext | null): () => void { if (currentWaitUntil && typeof currentWaitUntil === "function") { // Bind to context to avoid "Illegal invocation" errors - // And allow errors (like DataCloneError) to propagate so caller can handle fallback - globals.___voltagent_wait_until = currentWaitUntil.bind(context); - } else { - globals.___voltagent_wait_until = undefined; + // Wrap in try/catch so errors (like DataCloneError) are swallowed and don't break the caller + const boundWaitUntil = currentWaitUntil.bind(context); + globals.___voltagent_wait_until = (promise: Promise) => { + try { + boundWaitUntil(promise); + } catch { + // Swallow errors to avoid breaking the caller + } + }; } + // No else branch — don't touch global when context has no waitUntil, + // to avoid destroying a previously set value from an outer scope - // Return cleanup function + // Return cleanup function that always restores the previous state return () => { - if (currentWaitUntil) { - if (previousWaitUntil) { - globals.___voltagent_wait_until = previousWaitUntil; - } else { - globals.___voltagent_wait_until = undefined; - } - } + globals.___voltagent_wait_until = previousWaitUntil; }; }