Unverified Commit 662344c3 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

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)
parent dcad00d6
...@@ -13,6 +13,7 @@ import path from 'path' ...@@ -13,6 +13,7 @@ import path from 'path'
import type { NextConfig } from '../types' import type { NextConfig } from '../types'
import type { NextConfigComplete } from '../server/config-shared' import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared' import { traceGlobals } from '../trace/shared'
import cluster from 'cluster'
let isTurboSession = false let isTurboSession = false
let sessionStopHandled = false let sessionStopHandled = false
...@@ -416,39 +417,53 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit ...@@ -416,39 +417,53 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
await telemetry.flush() await telemetry.flush()
return server return server
} else { } else {
startServer(devServerOptions) // we're using a sub worker to avoid memory leaks. When memory usage exceeds 90%, we kill the worker and restart it.
.then(async (app) => { // this is a temporary solution until we can fix the memory leaks.
const appUrl = `http://${app.hostname}:${app.port}` // the logic for the worker killing itself is in `packages/next/server/lib/start-server.ts`
startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${app.port}`) if (!process.env.__NEXT_DISABLE_MEMORY_WATCHER && cluster.isMaster) {
// Start preflight after server is listening and ignore errors: cluster.fork()
preflight().catch(() => {}) cluster.on('exit', (worker) => {
// Finalize server bootup: if (worker.exitedAfterDisconnect) {
await app.prepare() cluster.fork()
} else {
process.exit(1)
}
}) })
.catch((err) => { } else {
if (err.code === 'EADDRINUSE') { startServer(devServerOptions)
let errorMessage = `Port ${port} is already in use.` .then(async (app) => {
const pkgAppPath = require('next/dist/compiled/find-up').sync( const appUrl = `http://${app.hostname}:${app.port}`
'package.json', startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${app.port}`)
{ // Start preflight after server is listening and ignore errors:
cwd: dir, preflight().catch(() => {})
} // Finalize server bootup:
) await app.prepare()
const appPackage = require(pkgAppPath) })
if (appPackage.scripts) { .catch((err) => {
const nextScript = Object.entries(appPackage.scripts).find( if (err.code === 'EADDRINUSE') {
(scriptLine) => scriptLine[1] === 'next' let errorMessage = `Port ${port} is already in use.`
const pkgAppPath = require('next/dist/compiled/find-up').sync(
'package.json',
{
cwd: dir,
}
) )
if (nextScript) { const appPackage = require(pkgAppPath)
errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.` if (appPackage.scripts) {
const nextScript = Object.entries(appPackage.scripts).find(
(scriptLine) => scriptLine[1] === 'next'
)
if (nextScript) {
errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
}
} }
console.error(errorMessage)
} else {
console.error(err)
} }
console.error(errorMessage) process.nextTick(() => process.exit(1))
} else { })
console.error(err) }
}
process.nextTick(() => process.exit(1))
})
} }
for (const CONFIG_FILE of CONFIG_FILES) { for (const CONFIG_FILE of CONFIG_FILES) {
......
...@@ -2,17 +2,28 @@ import type { NextServerOptions, NextServer, RequestHandler } from '../next' ...@@ -2,17 +2,28 @@ import type { NextServerOptions, NextServer, RequestHandler } from '../next'
import { warn } from '../../build/output/log' import { warn } from '../../build/output/log'
import http from 'http' import http from 'http'
import next from '../next' import next from '../next'
import cluster from 'cluster'
import v8 from 'v8'
interface StartServerOptions extends NextServerOptions { interface StartServerOptions extends NextServerOptions {
allowRetry?: boolean allowRetry?: boolean
keepAliveTimeout?: number keepAliveTimeout?: number
} }
const MAXIMUM_HEAP_SIZE_ALLOWED =
(v8.getHeapStatistics().heap_size_limit / 1024 / 1024) * 0.9
export function startServer(opts: StartServerOptions) { export function startServer(opts: StartServerOptions) {
let requestHandler: RequestHandler let requestHandler: RequestHandler
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
return requestHandler(req, res) return requestHandler(req, res).finally(() => {
if (
cluster.worker &&
process.memoryUsage().heapUsed / 1024 / 1024 > MAXIMUM_HEAP_SIZE_ALLOWED
) {
cluster.worker.kill()
}
})
}) })
if (opts.keepAliveTimeout) { if (opts.keepAliveTimeout) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment