d08b3ffa50
- Both the standalone server and the `startServer` function it calls attempt to stop the server on `SIGINT` and `SIGTERM` in different ways. This lets `server.js` yield to `startServer` - The cleanup function in `startServer` was not waiting for the server to close before calling `process.exit`. This lets it wait for any in-flight requests to finish processing before exiting the process - Sends `SIGKILL` to the child process in `next dev`, which should have the same effect of immediately shutting down the server on `SIGTERM` or `SIGINT` fixes: #53661 refs: #59551 ------ Previously #59551 attempted to fix #53661, but had broken some tests in the process. It looks like the final commit was also missing an intended change to `utils.ts`. This should fix those issues as well as introduce a new set of tests for the graceful shutdown feature. In the last PR I was squashing and force-pushing updates along the way but it made it difficult to track the changes. This time I'm pushing quite a few commits to make it easier to track the changes and refactors I've made, with the idea that this should be squashed before being merged. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
236 lines
6.6 KiB
TypeScript
236 lines
6.6 KiB
TypeScript
import { join } from 'path'
|
|
import { NextInstance, createNext, FileRef } from 'e2e-utils'
|
|
import {
|
|
fetchViaHTTP,
|
|
findPort,
|
|
initNextServerScript,
|
|
killApp,
|
|
launchApp,
|
|
nextBuild,
|
|
nextStart,
|
|
waitFor,
|
|
} from 'next-test-utils'
|
|
import fs from 'fs-extra'
|
|
import glob from 'glob'
|
|
import { LONG_RUNNING_MS } from './src/pages/api/long-running'
|
|
import { once } from 'events'
|
|
|
|
const appDir = join(__dirname, './src')
|
|
let appPort
|
|
let app
|
|
|
|
function assertDefined<T>(value: T | void): asserts value is T {
|
|
expect(value).toBeDefined()
|
|
}
|
|
|
|
describe('Graceful Shutdown', () => {
|
|
describe('development (next dev)', () => {
|
|
beforeEach(async () => {
|
|
appPort = await findPort()
|
|
app = await launchApp(appDir, appPort)
|
|
})
|
|
afterEach(() => killApp(app))
|
|
|
|
runTests(true)
|
|
})
|
|
;(process.env.TURBOPACK ? describe.skip : describe)(
|
|
'production (next start)',
|
|
() => {
|
|
beforeAll(async () => {
|
|
await nextBuild(appDir)
|
|
})
|
|
beforeEach(async () => {
|
|
appPort = await findPort()
|
|
app = await nextStart(appDir, appPort)
|
|
})
|
|
afterEach(() => killApp(app))
|
|
|
|
runTests()
|
|
}
|
|
)
|
|
;(process.env.TURBOPACK ? describe.skip : describe)(
|
|
'production (standalone mode)',
|
|
() => {
|
|
let next: NextInstance
|
|
let serverFile
|
|
|
|
const projectFiles = {
|
|
'next.config.mjs': `export default { output: 'standalone' }`,
|
|
}
|
|
|
|
for (const file of glob.sync('*', { cwd: appDir, dot: false })) {
|
|
projectFiles[file] = new FileRef(join(appDir, file))
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
next = await createNext({
|
|
files: projectFiles,
|
|
dependencies: {
|
|
swr: 'latest',
|
|
},
|
|
})
|
|
|
|
await next.stop()
|
|
|
|
await fs.move(
|
|
join(next.testDir, '.next/standalone'),
|
|
join(next.testDir, 'standalone')
|
|
)
|
|
|
|
for (const file of await fs.readdir(next.testDir)) {
|
|
if (file !== 'standalone') {
|
|
await fs.remove(join(next.testDir, file))
|
|
}
|
|
}
|
|
const files = glob.sync('**/*', {
|
|
cwd: join(next.testDir, 'standalone/.next/server/pages'),
|
|
dot: true,
|
|
})
|
|
|
|
for (const file of files) {
|
|
if (file.endsWith('.json') || file.endsWith('.html')) {
|
|
await fs.remove(join(next.testDir, '.next/server', file))
|
|
}
|
|
}
|
|
|
|
serverFile = join(next.testDir, 'standalone/server.js')
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
appPort = await findPort()
|
|
app = await initNextServerScript(
|
|
serverFile,
|
|
/- Local:/,
|
|
{ ...process.env, PORT: appPort.toString() },
|
|
undefined,
|
|
{ cwd: next.testDir }
|
|
)
|
|
})
|
|
afterEach(() => killApp(app))
|
|
|
|
afterAll(() => next.destroy())
|
|
|
|
runTests()
|
|
}
|
|
)
|
|
})
|
|
|
|
function runTests(dev = false) {
|
|
if (dev) {
|
|
it('should shut down child immediately', async () => {
|
|
const appKilledPromise = once(app, 'exit')
|
|
|
|
// let the dev server compile the route before running the test
|
|
await expect(
|
|
fetchViaHTTP(appPort, '/api/long-running')
|
|
).resolves.toBeDefined()
|
|
|
|
const resPromise = fetchViaHTTP(appPort, '/api/long-running')
|
|
|
|
// yield event loop to kick off request before killing the app
|
|
await waitFor(20)
|
|
process.kill(app.pid, 'SIGTERM')
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// `next dev` should kill the child immediately
|
|
let start = Date.now()
|
|
await expect(resPromise).rejects.toThrow()
|
|
expect(Date.now() - start).toBeLessThan(LONG_RUNNING_MS)
|
|
|
|
// `next dev` parent process is still running cleanup
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// App finally shuts down
|
|
await appKilledPromise
|
|
expect(app.exitCode).toBe(0)
|
|
})
|
|
} else {
|
|
it('should wait for requests to complete before exiting', async () => {
|
|
const appKilledPromise = once(app, 'exit')
|
|
|
|
let responseResolved = false
|
|
const resPromise = fetchViaHTTP(appPort, '/api/long-running')
|
|
.then((res) => {
|
|
responseResolved = true
|
|
return res
|
|
})
|
|
.catch(() => {})
|
|
|
|
// yield event loop to kick off request before killing the app
|
|
await waitFor(20)
|
|
process.kill(app.pid, 'SIGTERM')
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// Long running response should still be running after a bit
|
|
await waitFor(LONG_RUNNING_MS / 2)
|
|
expect(app.exitCode).toBe(null)
|
|
expect(responseResolved).toBe(false)
|
|
|
|
// App responds as expected without being interrupted
|
|
const res = await resPromise
|
|
assertDefined(res)
|
|
expect(res.status).toBe(200)
|
|
expect(await res.json()).toStrictEqual({ hello: 'world' })
|
|
|
|
// App is still running briefly after response returns
|
|
expect(app.exitCode).toBe(null)
|
|
expect(responseResolved).toBe(true)
|
|
|
|
// App finally shuts down
|
|
await appKilledPromise
|
|
expect(app.exitCode).toBe(0)
|
|
})
|
|
|
|
describe('should not accept new requests during shutdown cleanup', () => {
|
|
it('when request is made before shutdown', async () => {
|
|
const appKilledPromise = once(app, 'exit')
|
|
|
|
const resPromise = fetchViaHTTP(appPort, '/api/long-running')
|
|
|
|
// yield event loop to kick off request before killing the app
|
|
await waitFor(20)
|
|
process.kill(app.pid, 'SIGTERM')
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// Long running response should still be running after a bit
|
|
await waitFor(LONG_RUNNING_MS / 2)
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// Second request should be rejected
|
|
await expect(
|
|
fetchViaHTTP(appPort, '/api/long-running')
|
|
).rejects.toThrow()
|
|
|
|
// Original request responds as expected without being interrupted
|
|
await expect(resPromise).resolves.toBeDefined()
|
|
const res = await resPromise
|
|
expect(res.status).toBe(200)
|
|
expect(await res.json()).toStrictEqual({ hello: 'world' })
|
|
|
|
// App is still running briefly after response returns
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// App finally shuts down
|
|
await appKilledPromise
|
|
expect(app.exitCode).toBe(0)
|
|
})
|
|
|
|
it('when there is no activity', async () => {
|
|
const appKilledPromise = once(app, 'exit')
|
|
|
|
process.kill(app.pid, 'SIGTERM')
|
|
expect(app.exitCode).toBe(null)
|
|
|
|
// yield event loop to allow server to start the shutdown process
|
|
await waitFor(20)
|
|
await expect(
|
|
fetchViaHTTP(appPort, '/api/long-running')
|
|
).rejects.toThrow()
|
|
|
|
// App finally shuts down
|
|
await appKilledPromise
|
|
expect(app.exitCode).toBe(0)
|
|
})
|
|
})
|
|
}
|
|
}
|