Skip to content

Commit a2cc2b3

Browse files
authoredMay 24, 2023
fix: gracefully exit when first SIGINT is received (#3407)
1 parent 632eef4 commit a2cc2b3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎packages/vitest/src/node/cli-api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export async function startVitest(
7979

8080
if (process.stdin.isTTY && ctx.config.watch)
8181
registerConsoleShortcuts(ctx)
82+
else
83+
process.on('SIGINT', () => ctx.cancelCurrentRun('keyboard-input'))
8284

8385
ctx.onServerRestart((reason) => {
8486
ctx.report('onServerRestart', reason)

‎packages/vitest/src/node/stdin.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ export function registerConsoleShortcuts(ctx: Vitest) {
2828
let latestFilename = ''
2929

3030
async function _keypressHandler(str: string, key: any) {
31-
// ctrl-c or esc
32-
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c'))
31+
// Cancel run and exit when ctrl-c or esc is pressed.
32+
// If cancelling takes long and key is pressed multiple times, exit forcefully.
33+
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c')) {
34+
if (!ctx.isCancelling) {
35+
await ctx.cancelCurrentRun('keyboard-input')
36+
await ctx.runningPromise
37+
}
3338
return ctx.exit(true)
39+
}
3440

3541
// window not support suspend
3642
if (!isWindows && key && key.ctrl && key.name === 'z') {

0 commit comments

Comments
 (0)
Please sign in to comment.