rsnext/packages/next/server
Jimmy Lai 662344c3a5
next-dev: restart dev server exceeds the memory limits (#43958)
## Context

There has been [some reports ](https://github.com/vercel/next.js/issues/42514)of OOMs-related crashes with Next 13. Whilst we're fixing the memory leaks that are causing this, some of which are caused by upstream issues, this PR makes Next.js' dev server restarts if it detects that it is gonna crash soon.

You can disable this behaviour by passing `__NEXT_DISABLE_MEMORY_WATCHER=1` to the env process.

## Details

Under the hood, we're using Node's cluster API to create a child worker that will basically watch the memory usage after every request and then kill itself if it goes over 90% of the maximum heap allowance.


## Test plan

I added manually a  leaking function that I called before handling a request. I then manually tested that the server re-started when we were near the limit.

```

function createMemoryLeak() {
  console.log('createMemoryLeak', process.memoryUsage().heapUsed / 1024 / 1024)
  for (let i = 0; i < 10; i++) {
    buffer.push(new Array(1000000).fill('a'))
  }
}

```


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-12-12 18:54:55 +00:00
..
api-utils Remove unstable_revalidate (#43119) 2022-11-19 15:54:19 -08:00
base-http Improve types for <Image /> and responseLimit (#40863) 2022-09-26 22:41:25 -07:00
dev Increase stack trace limit on the server (#43800) 2022-12-11 22:35:55 -06:00
lib next-dev: restart dev server exceeds the memory limits (#43958) 2022-12-12 18:54:55 +00:00
response-cache Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
send-payload Fix app static generation cases (#41172) 2022-10-06 21:43:23 +02:00
web Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
accept-header.ts Enable @typescript-eslint/no-use-before-define for functions (#39602) 2022-08-15 10:29:51 -04:00
app-render.tsx Use proxy to ensure Flight is referencing to the latest module during development (#43823) 2022-12-08 19:00:52 +00:00
base-server.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
body-streams.ts fix(stream): Allows body larger than 16 KiB with middleware (#41270) 2022-10-17 15:26:16 -07:00
config-schema.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
config-shared.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
config-utils.ts Prebundle react for appDir (#41337) 2022-10-18 16:32:23 -07:00
config.ts Improve @next/font error handling (#43298) 2022-11-23 16:08:28 -08:00
crypto-utils.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
font-utils.ts Updating size-adjust calculation (#41406) 2022-10-20 10:58:26 -07:00
get-app-route-from-entrypoint.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
get-page-files.ts Add initial handling for routing tests (#36635) 2022-05-03 10:37:23 +00:00
get-route-from-entrypoint.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
google-font-metrics.json Updating avg width metrics for google fonts (#41755) 2022-10-24 21:17:32 -07:00
htmlescape.ts Subresource Integrity for App Directory (#39729) 2022-09-08 15:17:15 -07:00
image-optimizer.ts Refactor image optimization util (#43868) 2022-12-12 05:02:52 +00:00
initialize-require-hook.ts Disable esm resolving for appDir and alias react (#41687) 2022-10-23 20:04:23 -07:00
internal-utils.ts Rename RSC and Router headers (#42482) 2022-11-07 16:35:32 -08:00
load-components.ts BREAKING CHANGE: Remove target: serverless (#41495) 2022-10-18 09:47:13 -07:00
match-bundle.ts App Build Stats (#38884) 2022-08-10 19:31:01 +00:00
next-server.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00
next-typescript.ts Add auto completion for prop names and types to the TS plugin (#43909) 2022-12-10 20:07:56 +01:00
next.ts Remove react root condition and always use concurrent mode (#42141) 2022-10-29 20:34:03 +00:00
node-polyfill-fetch.js Support next option for Request (#41614) 2022-10-23 20:24:35 -07:00
node-polyfill-web-streams.js Update Edge Runtime (#38862) 2022-07-21 18:29:19 +00:00
node-web-streams-helper.ts Revert "Adding head element checking for root layout" (#43760) 2022-12-06 15:41:29 +01:00
optimize-amp.ts Move next-server directory files to server directory (#26756) 2021-06-30 13:44:40 +02:00
post-process.ts Fix replaceAll usage in post-process (#42714) 2022-11-09 17:26:55 -08:00
render-result.ts Fix app static generation cases (#41172) 2022-10-06 21:43:23 +02:00
render.tsx Implement loadable with lazy and suspense for next dynamic (#42589) 2022-12-07 19:42:10 +01:00
request-meta.ts Fix middleware rewrite for _next/data (#41341) 2022-10-11 12:23:22 -07:00
require.ts perf: refactor path logic in router + add LRU cache (#41365) 2022-10-20 15:42:50 +00:00
router.ts Add missing matcher support (#42660) 2022-11-08 16:48:27 -08:00
serve-static.ts fix(#39706): add avif support for node serve static (#39733) 2022-08-18 17:57:12 +00:00
server-route-utils.ts Update @types/node to 14.14.31 (#41634) 2022-10-22 11:19:14 -07:00
utils.ts Remove react root condition and always use concurrent mode (#42141) 2022-10-29 20:34:03 +00:00
web-server.ts Update cache handling for app (#43659) 2022-12-02 23:32:49 -08:00