Skip to content

Commit

Permalink
fix(cli): respect SIGTERM and SIGINT exit codes in next build (vercel…
Browse files Browse the repository at this point in the history
…#64871)

This PR updates the SIGTERM and SIGINT exit codes to 143 and 130
respectively. These updated codes are what other tooling, such as
Nx/Turborepo, expects when running commands that did not completely
successfully due to receiving SIGTERM and SIGINT.

For Nx, the SIGINT (e.g. `Ctrl+C`) causes a bad build cache. Both Nx and
Turborepo will have bad cache when SIGTERM is received, which can happen
in a CI environment if a job times out, runs out of memory, etc.

I have a [repo here](https://github.com/jaysoo/next-cli-signals) that
demonstrates the wrong behavior with Turborepo. By exiting with the
wrong code, it can result in bad cache in CI or Docker.

Closes: vercel#62906

---------

Co-authored-by: Sam Ko <[email protected]>
Co-authored-by: Zack Tanner <[email protected]>
  • Loading branch information
3 people authored and stipsan committed Nov 6, 2024
1 parent 7048444 commit 36b59a3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/cli/next-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export type NextBuildOptions = {
}

const nextBuild = (options: NextBuildOptions, directory?: string) => {
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))
process.on('SIGTERM', () => process.exit(143))
process.on('SIGINT', () => process.exit(130))

const {
debug,
Expand Down
9 changes: 5 additions & 4 deletions test/integration/cli/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const runAndCaptureOutput = async ({ port }) => {
const testExitSignal = async (
killSignal = '',
args = [],
readyRegex = /Creating an optimized production/
readyRegex = /Creating an optimized production/,
expectedCode = 0
) => {
let instance
const killSigint = (inst) => {
Expand All @@ -76,7 +77,7 @@ const testExitSignal = async (
// See: https://nodejs.org/api/process.html#process_signal_events
const expectedExitSignal = process.platform === `win32` ? killSignal : null
expect(signal).toBe(expectedExitSignal)
expect(code).toBe(0)
expect(code).toBe(expectedCode)
}

describe('CLI Usage', () => {
Expand Down Expand Up @@ -342,11 +343,11 @@ describe('CLI Usage', () => {
})

test('should exit when SIGINT is signalled', async () => {
await testExitSignal('SIGINT', ['build', dirBasic])
await testExitSignal('SIGINT', ['build', dirBasic], undefined, 130)
})

test('should exit when SIGTERM is signalled', async () => {
await testExitSignal('SIGTERM', ['build', dirBasic])
await testExitSignal('SIGTERM', ['build', dirBasic], undefined, 143)
})

test('invalid directory', async () => {
Expand Down

0 comments on commit 36b59a3

Please sign in to comment.