AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(core-utils): don't fail a code step over a rejection that arrives after its result (#16688)
Chaker Atallah4252bb5d7594
diff --git a/brain/knowledge/decisions/000049-a-late-unhandled-rejection-does-not-fail-an-already-returned-code-step.md b/brain/knowledge/decisions/000049-a-late-unhandled-rejection-does-not-fail-an-already-returned-code-step.mdnew file mode 100644index 00000000000..c4283339d92--- /dev/null+++ b/brain/knowledge/decisions/000049-a-late-unhandled-rejection-does-not-fail-an-already-returned-code-step.md@@ -0,0 +1,23 @@+---+status: accepted+---++# A late unhandled rejection does not fail an already-returned code step++## Decision++In the Deno code-step harness (`buildRunProgram` in `deno.ts`), once a step's result has already been emitted (`settled === true`), a later `unhandledrejection` no longer fails the run. It is logged to the step's stderr (`Unhandled rejection after the step already returned a result: <message>`) and the process is left to exit on its own once its pending work drains. A rejection that arrives *before* the result is emitted still fails the run exactly as before.++## Context++A code step that starts an async call without awaiting it (e.g. `return { res: response.json(), ... }`, leaving `response.json()` unawaited) returns successfully, but the orphaned promise keeps running in the Deno process. If it later rejects — a non-JSON response body, a flaky network read — the step's run had already succeeded. Found via a cloud rollout flow (`fetch('https://web.bale.ai/')` with an unawaited `.json()`) that worked on the legacy Node sandbox and started failing after migrating to Deno, surfaced by the deploy-watch alert built for this rollout.++## Why++Legacy's child process (`no-op-code-sandbox.ts`) exits via `process.exit(0)` one tick after sending its success result. `process.exit()` tears down the process unconditionally, so a slower orphaned promise (real I/O takes far longer than one tick) never gets the chance to reject before the process is gone — legacy was never "correct" here, it was just unable to observe the failure. The Deno harness has no such exit call on the success path; the process stays alive until its pending work drains, which is what let the later rejection actually fire and fail the run via `Deno.exit(1)`.++The alternative — force an immediate exit after emitting the result, matching legacy exactly — was rejected: it would also kill deliberate fire-and-forget work the user's code started on purpose (e.g. "send a Slack notification without waiting for it"), which legacy also couldn't support but Deno's lazier shutdown allows today. Logging instead of silently swallowing keeps the rejection visible in the run's logs without retroactively failing a run whose result was already committed.++## Consequences++Orphaned promises that reject late no longer fail a run, matching legacy's effective behavior for this case, but the mistake is still discoverable (step logs carry the message) instead of vanishing the way it did under legacy. A rejection that fires before the result is emitted — including the same-tick case (`Promise.reject(...); return 1;`, pinned by the `'a promise left unhandled after the step returns'` case in `code-sandbox-error-contract.test.ts`) — is unaffected, since microtask-queue rejections there still resolve before the macrotask yield that precedes `emit(success)`.diff --git a/packages/core/utils/src/lib/deno.ts b/packages/core/utils/src/lib/deno.tsindex 5e0e194d096..aed476a8b15 100644--- a/packages/core/utils/src/lib/deno.ts+++ b/packages/core/utils/src/lib/deno.ts@@ -64,8 +64,6 @@ export const deno = {reject(sandboxError.build({ error: message.error, stdout: userOutput, stderr: capturedStderr }))}else if (code !== 0) {- // e.g. an unhandled rejection fired after the result was printed — deno exits- // non-zero, so the run must fail even though a success marker exists.reject(sandboxError.build({ error: `Deno process exited with code ${code} and signal ${signal} after producing a result`, stdout: userOutput, stderr: capturedStderr }))}else {@@ -346,6 +344,11 @@ const emit = (payload) => {};globalThis.addEventListener('unhandledrejection', (event) => {event.preventDefault();+ if (settled) {+ const payload = toErrorPayload(event.reason);+ console.error(\`Unhandled rejection after the step already returned a result: \${payload.message}\`);+ return;+ }emit({ success: false, error: toErrorPayload(event.reason) });Deno.exit(1);});diff --git a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts b/packages/server/engine/test/core/code/deno-code-sandbox.test.tsindex 5a396a15d84..4fb5e28b763 100644--- a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts+++ b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts@@ -522,3 +522,15 @@ describe('deno code sandbox memory limit', () => {await expect(uncapped.runCodeModule({ codeFilePath, inputs: {} })).resolves.toBe(30)})})++describe('post-result rejections', () => {+ it('does not fail the run when a promise the step left unawaited rejects after the result was already returned', async () => {+ const source = `export const code = async () => {+ setTimeout(() => {+ Promise.reject(new Error('late boom'));+ }, 50);+ return 'ok';+};`+ await expect(runModule(source)).resolves.toBe('ok')+ })+})