unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

Unhandled infinite loops on event handlers #2496

Closed imlautaro closed 3 months ago

imlautaro commented 3 months ago

Environment

Node version: 20.11.0 Nitro version: 2.9.6

Reproduction

You can just create an infinite loop in any event handler:

export default eventHandler((event) => {
  const response = [];
  while (true) {
    response.push("Hello, Nitro!");
  }
  return response;
});

Describe the bug

When you create an infinite loop by accident in an event handler, it makes the app crash (regardless of whether you use nuxt or not) but it doesn't give you a clear error message so it can be really hard to debug.

Additional context

No response

Logs

#
# Fatal error in , line 0
# Fatal JavaScript invalid size error 169220804 (see crbug.com/1201626)
#
#
#
#FailureMessage Object: 000000CD6BBFE110
----- Native stack trace -----

 1: 00007FF7209B6E7B node::SetCppgcReference+16075
 2: 00007FF7208B622F node::TriggerNodeReport+70111
 3: 00007FF7217817F2 V8_Fatal+162
 4: 00007FF721218A55 v8::Platform::SystemClockTimeMillis+855861
 5: 00007FF721099BE3 v8::base::Thread::StartSynchronously+1456675
 6: 00007FF7210B8583 v8::Object::GetIsolate+15459
 7: 00007FF720EDA303 v8::CodeEvent::GetFunctionName+181699
 8: 00007FF72144EFAE v8::PropertyDescriptor::writable+677134
 9: 00007FF6C15E259B
 ELIFECYCLE  Command failed with exit code 2147483651.
pi0 commented 3 months ago

I know it is frustrating with how js single threads and Node.js errors happen but it is not something related to Nitro.

import { createServer } from 'node:http'

createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  const response = [];
  while (true) {
    response.push("Hello, Nitro!");
  }
  // res.end('Hello World\n')
}).listen(3000, () => console.log('Server running on http://localhost:3000/'))

This pure Node.js code leads to exactly same

image
imlautaro commented 3 months ago

Oh, okay. At least that's good to know. Thanks!