hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.62k stars 65 forks source link

`handler()` does not always resolve, causing memory leak #179

Closed hoangvvo closed 2 years ago

hoangvvo commented 2 years ago

In a normal usage with API Routes, there is a bug in next-connect that causes a possible memory leak. This is a troubling one because from the outside, there would not be any error (API responses will still be served normally to incoming requests)

The promise created by calling handler(req, res) (usually by Next.js internally at https://github.com/vercel/next.js/blob/1c1a4de0e2d38090fcf95ef0a6f6790006aaa124/packages/next/server/api-utils.ts#L108) will never be able to resolve, causing the logic from there onward to not be executable.

Normally, a forever-pending promise does not cause an issue. However, since it makes use of references of request and response, it prevents GC from recycling those in memory, creating memory leaks as requests come in.