Skip to content

Commit

Permalink
Fix free port handling (#50248)
Browse files Browse the repository at this point in the history
Follow-up to #48019 ensures we
don't use arbitrary ports by incrementing favoring always getting a
fresh free port instead. Also favors exact address for internal IPC
requests instead of `localhost`.
  • Loading branch information
ijjk authored May 24, 2023
1 parent 6ebc725 commit b5e8d65
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ export async function initialize(opts: {
return reject(err)
}
})
server.listen((await getFreePort()) + 1, opts.hostname)
server.listen(await getFreePort(), opts.hostname)
})
}
6 changes: 4 additions & 2 deletions packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render'
import crypto from 'crypto'
import isError from '../../../lib/is-error'
import { genRenderExecArgv } from '../worker-utils'
import { genRenderExecArgv, getFreePort } from '../worker-utils'

// we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors
Expand Down Expand Up @@ -116,7 +116,9 @@ export const createWorker = async (
}
: {}),
},
execArgv: isNodeDebugging ? genRenderExecArgv(type) : undefined,
execArgv: isNodeDebugging
? genRenderExecArgv(await getFreePort(), type)
: undefined,
},
exposedMethods: [
'initialize',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/server-ipc/invoke-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const invokeRequest = async (

try {
const invokeReq = http.request(
targetUrl,
parsedUrl.toString(),
{
headers: invokeHeaders,
method: requestInit.method,
Expand Down
17 changes: 1 addition & 16 deletions packages/next/src/server/lib/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getFreePort = async (): Promise<number> => {
})
}

export const genRenderExecArgv = (type: 'pages' | 'app') => {
export const genRenderExecArgv = (debugPort: number, type: 'pages' | 'app') => {
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
Expand All @@ -26,21 +26,6 @@ export const genRenderExecArgv = (type: 'pages' | 'app') => {
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)

let debugPort = (() => {
const debugPortStr =
process.execArgv
.find(
(localArg) =>
localArg.startsWith('--inspect') ||
localArg.startsWith('--inspect-brk')
)
?.split('=')[1] ??
process.env.NODE_OPTIONS?.match?.(/--inspect(-brk)?(=(\S+))?( |$)/)?.[3]
return debugPortStr ? parseInt(debugPortStr, 10) : 9229
})()

debugPort += type === 'pages' ? 2 : 3

if (isDebugging || isDebuggingWithBrk) {
Log.info(
`the --inspect${
Expand Down

0 comments on commit b5e8d65

Please sign in to comment.