Unverified Commit 6902e5b5 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Fix issues where saving next.config.js too quickly could result in multiple dev…

Fix issues where saving next.config.js too quickly could result in multiple dev servers running at once (#48857)

We were adding filling in our cleanup function after we started our server. So If we restarted while the server was starting, we would fire an empty cleanup function which would lead to the server not being cleaned up (once it starts).

The only slight issue is that it won't watch for changes while cleaning up the previous run (waiting for the server from the last run to start and finish).

fix NEXT-1052
parent 1c0c61a6
...@@ -325,16 +325,12 @@ const nextDev: CliCommand = async (argv) => { ...@@ -325,16 +325,12 @@ const nextDev: CliCommand = async (argv) => {
return return
} }
const startDir = dir
if (newDir) {
dir = newDir
}
if (devServerTeardown) { if (devServerTeardown) {
await devServerTeardown() await devServerTeardown()
devServerTeardown = undefined devServerTeardown = undefined
} }
const startDir = dir
if (newDir) { if (newDir) {
dir = newDir dir = newDir
process.env = Object.keys(process.env).reduce((newEnv, key) => { process.env = Object.keys(process.env).reduce((newEnv, key) => {
...@@ -378,6 +374,20 @@ const nextDev: CliCommand = async (argv) => { ...@@ -378,6 +374,20 @@ const nextDev: CliCommand = async (argv) => {
process[fd].write(chunk) process[fd].write(chunk)
} }
let resolveCleanup!: (cleanup: () => Promise<void>) => void
let cleanupPromise = new Promise<() => Promise<void>>((resolve) => {
resolveCleanup = resolve
})
const cleanupWrapper = async () => {
const promise = cleanupPromise
cleanupPromise = Promise.resolve(async () => {})
const cleanup = await promise
await cleanup()
}
cleanupFns.push(cleanupWrapper)
devServerTeardown = cleanupWrapper
try {
devServerOptions.onStdout = (chunk) => { devServerOptions.onStdout = (chunk) => {
filterForkErrors(chunk, 'stdout') filterForkErrors(chunk, 'stdout')
} }
...@@ -385,7 +395,11 @@ const nextDev: CliCommand = async (argv) => { ...@@ -385,7 +395,11 @@ const nextDev: CliCommand = async (argv) => {
filterForkErrors(chunk, 'stderr') filterForkErrors(chunk, 'stderr')
} }
shouldFilter = false shouldFilter = false
devServerTeardown = await startServer(devServerOptions) resolveCleanup(await startServer(devServerOptions))
} finally {
// fallback to noop, if not provided
resolveCleanup(async () => {})
}
if (!config) { if (!config) {
config = await loadConfig( config = await loadConfig(
...@@ -403,7 +417,6 @@ const nextDev: CliCommand = async (argv) => { ...@@ -403,7 +417,6 @@ const nextDev: CliCommand = async (argv) => {
) )
} }
} }
cleanupFns.push(() => devServerTeardown?.())
await setupFork() await setupFork()
await preflight() await preflight()
......
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