Unverified Commit 5584e574 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Merge app renderer process (#54143)

This PR merges the app renderer worker into the router process. This
improves the memory overhead mostly.

There're future work to do to get rid of the IPC server for router and
app renderer, as they're now merged in one process.

Fixes NEXT-1492
parent f0ff3c46
...@@ -26,8 +26,12 @@ type Log = { ...@@ -26,8 +26,12 @@ type Log = {
function replaceProcessEnv(sourceEnv: Env) { function replaceProcessEnv(sourceEnv: Env) {
Object.keys(process.env).forEach((key) => { Object.keys(process.env).forEach((key) => {
if (sourceEnv[key] === undefined || sourceEnv[key] === '') { // Allow mutating internal Next.js env variables after the server has initiated.
delete process.env[key] // This is necessary for dynamic things like the IPC server port.
if (!key.startsWith('__NEXT_PRIVATE')) {
if (sourceEnv[key] === undefined || sourceEnv[key] === '') {
delete process.env[key]
}
} }
}) })
......
...@@ -1935,14 +1935,12 @@ export async function copyTracedFiles( ...@@ -1935,14 +1935,12 @@ export async function copyTracedFiles(
serverOutputPath, serverOutputPath,
`${ `${
moduleType moduleType
? `import http from 'http' ? `import path from 'path'
import path from 'path'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url)) const __dirname = fileURLToPath(new URL('.', import.meta.url))
import { startServer } from 'next/dist/server/lib/start-server.js' import { startServer } from 'next/dist/server/lib/start-server.js'
` `
: ` : `
const http = require('http')
const path = require('path') const path = require('path')
const { startServer } = require('next/dist/server/lib/start-server')` const { startServer } = require('next/dist/server/lib/start-server')`
} }
...@@ -1961,13 +1959,17 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) { ...@@ -1961,13 +1959,17 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
const currentPort = parseInt(process.env.PORT, 10) || 3000 const currentPort = parseInt(process.env.PORT, 10) || 3000
const hostname = process.env.HOSTNAME || 'localhost' const hostname = process.env.HOSTNAME || 'localhost'
let keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
let keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10)
const nextConfig = ${JSON.stringify({ const nextConfig = ${JSON.stringify({
...serverConfig, ...serverConfig,
distDir: `./${path.relative(dir, distDir)}`, distDir: `./${path.relative(dir, distDir)}`,
})} })}
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig) process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig)
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = nextConfig.experimental && nextConfig.experimental.serverActions
? 'experimental'
: 'next'
if ( if (
Number.isNaN(keepAliveTimeout) || Number.isNaN(keepAliveTimeout) ||
......
...@@ -121,7 +121,7 @@ function watchConfigFiles( ...@@ -121,7 +121,7 @@ function watchConfigFiles(
type StartServerWorker = Worker & type StartServerWorker = Worker &
Pick<typeof import('../server/lib/start-server'), 'startServer'> Pick<typeof import('../server/lib/start-server'), 'startServer'>
async function createRouterWorker(): Promise<{ async function createRouterWorker(fullConfig: NextConfigComplete): Promise<{
worker: StartServerWorker worker: StartServerWorker
cleanup: () => Promise<void> cleanup: () => Promise<void>
}> { }> {
...@@ -143,6 +143,9 @@ async function createRouterWorker(): Promise<{ ...@@ -143,6 +143,9 @@ async function createRouterWorker(): Promise<{
: {}), : {}),
WATCHPACK_WATCHER_LIMIT: '20', WATCHPACK_WATCHER_LIMIT: '20',
EXPERIMENTAL_TURBOPACK: process.env.EXPERIMENTAL_TURBOPACK, EXPERIMENTAL_TURBOPACK: process.env.EXPERIMENTAL_TURBOPACK,
__NEXT_PRIVATE_PREBUNDLED_REACT: !!fullConfig.experimental.serverActions
? 'experimental'
: 'next',
}, },
}, },
exposedMethods: ['startServer'], exposedMethods: ['startServer'],
...@@ -394,7 +397,7 @@ const nextDev: CliCommand = async (argv) => { ...@@ -394,7 +397,7 @@ const nextDev: CliCommand = async (argv) => {
} else { } else {
const runDevServer = async (reboot: boolean) => { const runDevServer = async (reboot: boolean) => {
try { try {
const workerInit = await createRouterWorker() const workerInit = await createRouterWorker(config)
if (!!args['--experimental-https']) { if (!!args['--experimental-https']) {
Log.warn( Log.warn(
'Self-signed certificates are currently an experimental feature, use at your own risk.' 'Self-signed certificates are currently an experimental feature, use at your own risk.'
......
...@@ -1040,7 +1040,6 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1040,7 +1040,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const useInvokePath = const useInvokePath =
!useMatchedPathHeader && !useMatchedPathHeader &&
process.env.NEXT_RUNTIME !== 'edge' && process.env.NEXT_RUNTIME !== 'edge' &&
process.env.__NEXT_PRIVATE_RENDER_WORKER &&
invokePath invokePath
if (useInvokePath) { if (useInvokePath) {
...@@ -1131,7 +1130,6 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1131,7 +1130,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if ( if (
process.env.NEXT_RUNTIME !== 'edge' && process.env.NEXT_RUNTIME !== 'edge' &&
process.env.__NEXT_PRIVATE_RENDER_WORKER &&
req.headers['x-middleware-invoke'] req.headers['x-middleware-invoke']
) { ) {
const nextDataResult = await this.normalizeNextData(req, res, parsedUrl) const nextDataResult = await this.normalizeNextData(req, res, parsedUrl)
......
...@@ -80,8 +80,10 @@ export async function initialize(opts: { ...@@ -80,8 +80,10 @@ export async function initialize(opts: {
return result return result
} }
const type = process.env.__NEXT_PRIVATE_RENDER_WORKER! const type = process.env.__NEXT_PRIVATE_RENDER_WORKER
process.title = 'next-render-worker-' + type if (type) {
process.title = 'next-render-worker-' + type
}
let requestHandler: RequestHandler let requestHandler: RequestHandler
let upgradeHandler: any let upgradeHandler: any
......
...@@ -38,15 +38,14 @@ import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-requ ...@@ -38,15 +38,14 @@ import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-requ
const debug = setupDebug('next:router-server:main') const debug = setupDebug('next:router-server:main')
export type RenderWorker = InstanceType< export type RenderWorker = Pick<
typeof import('next/dist/compiled/jest-worker').Worker typeof import('./render-server'),
> & { | 'initialize'
initialize: typeof import('./render-server').initialize | 'deleteCache'
deleteCache: typeof import('./render-server').deleteCache | 'clearModuleContext'
deleteAppClientCache: typeof import('./render-server').deleteAppClientCache | 'deleteAppClientCache'
clearModuleContext: typeof import('./render-server').clearModuleContext | 'propagateServerField'
propagateServerField: typeof import('./render-server').propagateServerField >
}
export interface RenderWorkers { export interface RenderWorkers {
app?: Awaited<ReturnType<typeof createWorker>> app?: Awaited<ReturnType<typeof createWorker>>
...@@ -185,16 +184,19 @@ export async function initialize(opts: { ...@@ -185,16 +184,19 @@ export async function initialize(opts: {
}, },
} as any) } as any)
const { initialEnv } = require('@next/env') as typeof import('@next/env') if (!!config.experimental.appDir) {
process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT = ipcPort + ''
process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY = ipcValidationKey
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = config.experimental
.serverActions
? 'experimental'
: 'next'
renderWorkers.app = await createWorker( renderWorkers.app =
ipcPort, require('./render-server') as typeof import('./render-server')
ipcValidationKey, }
opts.isNodeDebugging,
'app', const { initialEnv } = require('@next/env') as typeof import('@next/env')
config,
initialEnv
)
renderWorkers.pages = await createWorker( renderWorkers.pages = await createWorker(
ipcPort, ipcPort,
ipcValidationKey, ipcValidationKey,
...@@ -272,10 +274,16 @@ export async function initialize(opts: { ...@@ -272,10 +274,16 @@ export async function initialize(opts: {
} }
} }
const logError = async (
type: 'uncaughtException' | 'unhandledRejection',
err: Error | undefined
) => {
await devInstance?.logErrorWithOriginalStack(err, type)
}
const cleanup = () => { const cleanup = () => {
debug('router-server process cleanup') debug('router-server process cleanup')
for (const curWorker of [ for (const curWorker of [
...((renderWorkers.app as any)?._workerPool?._workers || []),
...((renderWorkers.pages as any)?._workerPool?._workers || []), ...((renderWorkers.pages as any)?._workerPool?._workers || []),
] as { ] as {
_child?: import('child_process').ChildProcess _child?: import('child_process').ChildProcess
...@@ -290,8 +298,8 @@ export async function initialize(opts: { ...@@ -290,8 +298,8 @@ export async function initialize(opts: {
process.on('exit', cleanup) process.on('exit', cleanup)
process.on('SIGINT', cleanup) process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup) process.on('SIGTERM', cleanup)
process.on('uncaughtException', cleanup) process.on('uncaughtException', logError.bind(null, 'uncaughtException'))
process.on('unhandledRejection', cleanup) process.on('unhandledRejection', logError.bind(null, 'unhandledRejection'))
const resolveRoutes = getResolveRoutes( const resolveRoutes = getResolveRoutes(
fsChecker, fsChecker,
......
...@@ -2,10 +2,10 @@ import type { TLSSocket } from 'tls' ...@@ -2,10 +2,10 @@ import type { TLSSocket } from 'tls'
import type { FsOutput } from './filesystem' import type { FsOutput } from './filesystem'
import type { IncomingMessage } from 'http' import type { IncomingMessage } from 'http'
import type { NextConfigComplete } from '../../config-shared' import type { NextConfigComplete } from '../../config-shared'
import type { RenderWorker, initialize } from '../router-server'
import url from 'url' import url from 'url'
import { Redirect } from '../../../../types' import { Redirect } from '../../../../types'
import { RenderWorker } from '../router-server'
import setupDebug from 'next/dist/compiled/debug' import setupDebug from 'next/dist/compiled/debug'
import { getCloneableBody } from '../../body-streams' import { getCloneableBody } from '../../body-streams'
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils' import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils'
...@@ -46,7 +46,7 @@ export function getResolveRoutes( ...@@ -46,7 +46,7 @@ export function getResolveRoutes(
ReturnType<typeof import('./filesystem').setupFsCheck> ReturnType<typeof import('./filesystem').setupFsCheck>
>, >,
config: NextConfigComplete, config: NextConfigComplete,
opts: Parameters<typeof import('../router-server').initialize>[0], opts: Parameters<typeof initialize>[0],
renderWorkers: { renderWorkers: {
app?: RenderWorker app?: RenderWorker
pages?: RenderWorker pages?: RenderWorker
......
import type NextServer from '../../next-server' import type NextServer from '../../next-server'
import type { NextConfigComplete } from '../../config-shared' import type { NextConfigComplete } from '../../config-shared'
import type { RenderWorker } from '../router-server'
import { getNodeOptionsWithoutInspect } from '../utils' import { getNodeOptionsWithoutInspect } from '../utils'
import { errorToJSON } from '../../render' import { errorToJSON } from '../../render'
...@@ -7,7 +8,6 @@ import crypto from 'crypto' ...@@ -7,7 +8,6 @@ import crypto from 'crypto'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
import { genRenderExecArgv } from '../worker-utils' import { genRenderExecArgv } from '../worker-utils'
import { deserializeErr } from './request-utils' import { deserializeErr } from './request-utils'
import { RenderWorker } from '../router-server'
import type { Env } from '@next/env' import type { Env } from '@next/env'
// we can't use process.send as jest-worker relies on // we can't use process.send as jest-worker relies on
...@@ -115,13 +115,8 @@ export const createWorker = async ( ...@@ -115,13 +115,8 @@ export const createWorker = async (
__NEXT_PRIVATE_STANDALONE_CONFIG: __NEXT_PRIVATE_STANDALONE_CONFIG:
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG, process.env.__NEXT_PRIVATE_STANDALONE_CONFIG,
NODE_ENV: process.env.NODE_ENV, NODE_ENV: process.env.NODE_ENV,
...(type === 'app' __NEXT_PRIVATE_PREBUNDLED_REACT:
? { type === 'app' ? (useServerActions ? 'experimental' : 'next') : '',
__NEXT_PRIVATE_PREBUNDLED_REACT: useServerActions
? 'experimental'
: 'next',
}
: {}),
...(process.env.NEXT_CPU_PROF ...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.${type}-renderer` } ? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.${type}-renderer` }
: {}), : {}),
...@@ -135,7 +130,8 @@ export const createWorker = async ( ...@@ -135,7 +130,8 @@ export const createWorker = async (
'clearModuleContext', 'clearModuleContext',
'propagateServerField', 'propagateServerField',
], ],
}) as any as RenderWorker }) as any as RenderWorker &
InstanceType<typeof import('next/dist/compiled/jest-worker').Worker>
worker.getStderr().pipe(process.stderr) worker.getStderr().pipe(process.stderr)
worker.getStdout().pipe(process.stdout) worker.getStdout().pipe(process.stdout)
......
import '../node-polyfill-fetch' import '../node-polyfill-fetch'
import '../require-hook'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
...@@ -220,11 +221,15 @@ export async function startServer({ ...@@ -220,11 +221,15 @@ export async function startServer({
server.close() server.close()
process.exit(0) process.exit(0)
} }
const exception = (err: Error) => {
// This is the render worker, we keep the process alive
console.error(err)
}
process.on('exit', cleanup) process.on('exit', cleanup)
process.on('SIGINT', cleanup) process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup) process.on('SIGTERM', cleanup)
process.on('uncaughtException', cleanup) process.on('uncaughtException', exception)
process.on('unhandledRejection', cleanup) process.on('unhandledRejection', exception)
const initResult = await getRequestHandlers({ const initResult = await getRequestHandlers({
dir, dir,
......
...@@ -491,15 +491,6 @@ export default class NextNodeServer extends BaseServer { ...@@ -491,15 +491,6 @@ export default class NextNodeServer extends BaseServer {
) )
} }
private streamResponseChunk(res: ServerResponse, chunk: any) {
res.write(chunk)
// When streaming is enabled, we need to explicitly
// flush the response to avoid it being buffered.
if ('flush' in res) {
;(res as any).flush()
}
}
protected async imageOptimizer( protected async imageOptimizer(
req: NodeNextRequest, req: NodeNextRequest,
res: NodeNextResponse, res: NodeNextResponse,
...@@ -1033,7 +1024,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1033,7 +1024,7 @@ export default class NextNodeServer extends BaseServer {
return { return {
finished: true, finished: true,
} }
} catch (_) {} } catch {}
throw err throw err
} }
......
...@@ -138,7 +138,6 @@ createNextDescribe( ...@@ -138,7 +138,6 @@ createNextDescribe(
expect(parsedData.https).toBe(true) expect(parsedData.https).toBe(true)
expect(parsedData.os).toBe('\n') expect(parsedData.os).toBe('\n')
expect(parsedData.path).toBe('/hello/world/test.txt') expect(parsedData.path).toBe('/hello/world/test.txt')
expect(parsedData.process).toInclude('next-render-worker-app')
expect(parsedData.querystring).toBe('a=b') expect(parsedData.querystring).toBe('a=b')
expect(parsedData.stringDecoder).toBe(true) expect(parsedData.stringDecoder).toBe(true)
expect(parsedData.sys).toBe(true) expect(parsedData.sys).toBe(true)
...@@ -164,7 +163,6 @@ createNextDescribe( ...@@ -164,7 +163,6 @@ createNextDescribe(
expect(parsedData.https).toBe(true) expect(parsedData.https).toBe(true)
expect(parsedData.os).toBe('\n') expect(parsedData.os).toBe('\n')
expect(parsedData.path).toBe('/hello/world/test.txt') expect(parsedData.path).toBe('/hello/world/test.txt')
expect(parsedData.process).toInclude('next-render-worker-app')
expect(parsedData.querystring).toBe('a=b') expect(parsedData.querystring).toBe('a=b')
expect(parsedData.stringDecoder).toBe(true) expect(parsedData.stringDecoder).toBe(true)
expect(parsedData.sys).toBe(true) expect(parsedData.sys).toBe(true)
......
...@@ -3,19 +3,16 @@ ...@@ -3,19 +3,16 @@
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { import {
nextServer,
nextBuild, nextBuild,
startApp,
stopApp,
renderViaHTTP, renderViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp, killApp,
nextStart,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
let server
let app let app
const context = {} const context = {}
...@@ -23,17 +20,14 @@ const context = {} ...@@ -23,17 +20,14 @@ const context = {}
describe('disabled runtime JS', () => { describe('disabled runtime JS', () => {
describe('production mode', () => { describe('production mode', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort()
await nextBuild(appDir) await nextBuild(appDir)
app = nextServer({ app = await nextStart(appDir, appPort)
dir: join(__dirname, '../'),
dev: false,
quiet: true,
})
server = await startApp(app) context.appPort = appPort
context.appPort = appPort = server.address().port
}) })
afterAll(() => stopApp(server)) afterAll(() => killApp(app))
it('should render the page', async () => { it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
......
...@@ -4,30 +4,24 @@ import webdriver from 'next-webdriver' ...@@ -4,30 +4,24 @@ import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import { import {
nextBuild, nextBuild,
nextServer, nextStart,
startApp, killApp,
stopApp, findPort,
renderViaHTTP, renderViaHTTP,
} from 'next-test-utils' } from 'next-test-utils'
let appDir = join(__dirname, '../') let appDir = join(__dirname, '../')
let server
let appPort let appPort
let app
describe('excludeDefaultMomentLocales', () => { describe('excludeDefaultMomentLocales', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort()
await nextBuild(appDir) await nextBuild(appDir)
const app = nextServer({ app = await nextStart(appDir, appPort)
dir: appDir,
dev: false,
quiet: true,
})
server = await startApp(app)
appPort = server.address().port
// wait for it to start up:
await renderViaHTTP(appPort, '/') await renderViaHTTP(appPort, '/')
}) })
afterAll(() => stopApp(server)) afterAll(() => killApp(app))
it('should load momentjs', async () => { it('should load momentjs', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
import { join } from 'path' import { join } from 'path'
import { import {
nextServer,
runNextCommand, runNextCommand,
startApp, nextStart,
stopApp, killApp,
findPort,
renderViaHTTP, renderViaHTTP,
} from 'next-test-utils' } from 'next-test-utils'
...@@ -19,19 +19,13 @@ describe('Production Custom Build Directory', () => { ...@@ -19,19 +19,13 @@ describe('Production Custom Build Directory', () => {
}) })
expect(result.stderr).toBe('') expect(result.stderr).toBe('')
const app = nextServer({ const appPort = await findPort()
dir: join(__dirname, '../build'), const app = await nextStart(join(__dirname, '../build'), appPort)
dev: false,
quiet: true,
})
const server = await startApp(app)
const appPort = server.address().port
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello World/) expect(html).toMatch(/Hello World/)
await stopApp(server) await killApp(app)
}) })
}) })
}) })
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