edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

importing `edge-runtime` breaks process exit behavior

Open EndangeredMassa opened this issue 3 years ago • 2 comments

If I import the edge runtime, then my process has an uncaught exception, my process exits with no error message and an exit code of 0.

Reproduction: https://github.com/EndangeredMassa/reproduction-process-exit-behavior

Example:

import { EdgeRuntime } from 'edge-runtime';

throw new Error('intentional break');

It seems that these lines are the cause: https://github.com/vercel/edge-runtime/blob/ccdb203e6e7e7c46d16c594dba03288e1e967e3a/packages/runtime/src/edge-runtime.ts#L64-L76

EndangeredMassa avatar Sep 22 '22 20:09 EndangeredMassa

Hey @EndangeredMassa! Thank you for this.

@javivelasco and I did a bit of analysis.

The reason why we caught exceptions & rejections at process level within EdgeRuntime is to avoid exiting the runtime itself. This way, the runtime is resilient to exception/rejections occurring within the executed user code. Instead, it reports them using events, allowing caller code to gracefully handle them

But this creates that undesirable side you demonstrated: the simple import of the EdgeRuntime swallows everything! Very bad.

Our plan is the following:

  • make a distinction between errors happening during user code execution, and at process level
  • for the first error group, keep the current behavior (no exit, call event handlers instead)
  • for the second error group, propagate the error, so it stops and sets exit code

How does it sounds to you?

feugy avatar Sep 29 '22 09:09 feugy

That sounds good to me, thanks!

EndangeredMassa avatar Sep 29 '22 14:09 EndangeredMassa