Skip to content

Commit

Permalink
fix: gracefully shutdown preview server on SIGTERM (fix #12990) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-pap authored Jun 10, 2024
1 parent 6db2515 commit 2207a68
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
25 changes: 23 additions & 2 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ import { htmlFallbackMiddleware } from './server/middlewares/htmlFallback'
import { indexHtmlMiddleware } from './server/middlewares/indexHtml'
import { notFoundMiddleware } from './server/middlewares/notFound'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname, resolveServerUrls, shouldServeFile } from './utils'
import {
resolveHostname,
resolveServerUrls,
setupSIGTERMListener,
shouldServeFile,
teardownSIGTERMListener,
} from './utils'
import { printServerUrls } from './logger'
import { bindCLIShortcuts } from './shortcuts'
import type { BindCLIShortcutsOptions } from './shortcuts'
Expand Down Expand Up @@ -137,11 +143,16 @@ export async function preview(
const options = config.preview
const logger = config.logger

const closeHttpServer = createServerCloseFn(httpServer)

const server: PreviewServer = {
config,
middlewares: app,
httpServer,
close: createServerCloseFn(httpServer),
async close() {
teardownSIGTERMListener(closeServerAndExit)
await closeHttpServer()
},
resolvedUrls: null,
printUrls() {
if (server.resolvedUrls) {
Expand All @@ -155,6 +166,16 @@ export async function preview(
},
}

const closeServerAndExit = async () => {
try {
await server.close()
} finally {
process.exit()
}
}

setupSIGTERMListener(closeServerAndExit)

// apply server hooks from plugins
const postHooks: ((() => void) | void)[] = []
for (const hook of config.getSortedPluginHooks('configurePreviewServer')) {
Expand Down
29 changes: 12 additions & 17 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
promiseWithResolvers,
resolveHostname,
resolveServerUrls,
setupSIGTERMListener,
teardownSIGTERMListener,
} from '../utils'
import { getFsUtils } from '../fsUtils'
import { ssrLoadModule } from '../ssr/ssrModuleLoader'
Expand Down Expand Up @@ -502,8 +504,6 @@ export async function _createServer(
const container = await createPluginContainer(config, moduleGraph, watcher)
const closeHttpServer = createServerCloseFn(httpServer)

let exitProcess: () => void

const devHtmlTransformFn = createDevHtmlTransformFn(config)

const onCrawlEndCallbacks: (() => void)[] = []
Expand Down Expand Up @@ -638,10 +638,7 @@ export async function _createServer(
},
async close() {
if (!middlewareMode) {
process.off('SIGTERM', exitProcess)
if (process.env.CI !== 'true') {
process.stdin.off('end', exitProcess)
}
teardownSIGTERMListener(closeServerAndExit)
}
await Promise.allSettled([
watcher.close(),
Expand Down Expand Up @@ -736,20 +733,18 @@ export async function _createServer(
},
})

if (!middlewareMode) {
exitProcess = async () => {
try {
await server.close()
} finally {
process.exit()
}
}
process.once('SIGTERM', exitProcess)
if (process.env.CI !== 'true') {
process.stdin.on('end', exitProcess)
const closeServerAndExit = async () => {
try {
await server.close()
} finally {
process.exit()
}
}

if (!middlewareMode) {
setupSIGTERMListener(closeServerAndExit)
}

const onHMRUpdate = async (
type: 'create' | 'delete' | 'update',
file: string,
Expand Down
10 changes: 7 additions & 3 deletions packages/vite/src/node/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ const BASE_DEV_SHORTCUTS: CLIShortcut<ViteDevServer>[] = [
key: 'q',
description: 'quit',
async action(server) {
await server.close().finally(() => process.exit())
try {
await server.close()
} finally {
process.exit()
}
},
},
]
Expand All @@ -148,9 +152,9 @@ const BASE_PREVIEW_SHORTCUTS: CLIShortcut<PreviewServer>[] = [
{
key: 'q',
description: 'quit',
action(server) {
async action(server) {
try {
server.httpServer.close()
await server.close()
} finally {
process.exit()
}
Expand Down
16 changes: 16 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,3 +1438,19 @@ export function partialEncodeURIPath(uri: string): string {
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
return filePath.replaceAll('%', '%25') + postfix
}

export const setupSIGTERMListener = (callback: () => Promise<void>): void => {
process.once('SIGTERM', callback)
if (process.env.CI !== 'true') {
process.stdin.on('end', callback)
}
}

export const teardownSIGTERMListener = (
callback: () => Promise<void>,
): void => {
process.off('SIGTERM', callback)
if (process.env.CI !== 'true') {
process.stdin.off('end', callback)
}
}

0 comments on commit 2207a68

Please sign in to comment.