Skip to content

Commit

Permalink
fix: exit code on SIGTERM (#18741)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Nov 25, 2024
1 parent bac1089 commit cc55e36
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ export async function preview(
},
}

const closeServerAndExit = async () => {
const closeServerAndExit = async (_: unknown, exitCode?: number) => {
try {
await server.close()
} finally {
process.exitCode ??= exitCode ? 128 + exitCode : undefined
process.exit()
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,11 @@ export async function _createServer(
},
})

const closeServerAndExit = async () => {
const closeServerAndExit = async (_: unknown, exitCode?: number) => {
try {
await server.close()
} finally {
process.exitCode ??= exitCode ? 128 + exitCode : undefined
process.exit()
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1524,15 +1524,17 @@ export function partialEncodeURIPath(uri: string): string {
return filePath.replaceAll('%', '%25') + postfix
}

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

export const teardownSIGTERMListener = (
callback: () => Promise<void>,
callback: Parameters<typeof setupSIGTERMListener>[0],
): void => {
process.off('SIGTERM', callback)
if (process.env.CI !== 'true') {
Expand Down

0 comments on commit cc55e36

Please sign in to comment.