Skip to content

Commit

Permalink
wip: and another round of windows improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Sep 26, 2021
1 parent 29dd8d1 commit bdd36bd
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/playground/cli/__tests__/serve.js
Expand Up @@ -80,24 +80,25 @@ exports.serve = async function serve(root, isProd) {
const close = async () => {
if (serverProcess) {
let timer
const timeoutError = `server process still alive after 3s`
const timerPromise = new Promise(
(_, reject) =>
(timer = setTimeout(() => {
reject(`server process still alive after 3s`)
reject(timeoutError)
}, 3000))
)

serverProcess.kill('SIGTERM', { forceKillAfterTimeout: 2000 })
if (isWindows) {
execa.commandSync(`taskkill /pid ${serverProcess.pid} /T /F`)
}

try {
await Promise.race([serverProcess, timerPromise]).finally(() => {
const closeTimerRace = Promise.race([
serverProcess,
timerPromise
]).finally(() => {
clearTimeout(timer)
})
killProcess(serverProcess)
await closeTimerRace
} catch (e) {
if (!e.killed && !isWindows) {
if (e === timeoutError || (!serverProcess.killed && !isWindows)) {
collectErrorStreams('server', e)
console.error('failed to end vite cli process', e)
await printStreamsToConsole('server')
Expand Down Expand Up @@ -157,3 +158,16 @@ async function startedOnPort(serverProcess, port, timeout) {
clearTimeout(id)
})
}

// helper function to kill process, uses taskkill on windows to ensure child process is killed too
function killProcess(serverProcess) {
if (isWindows) {
try {
execa.commandSync(`taskkill /pid ${serverProcess.pid} /T /F`)
} catch (e) {
console.error('failed to taskkill:', e)
}
} else {
serverProcess.kill('SIGTERM', { forceKillAfterTimeout: 2000 })
}
}

0 comments on commit bdd36bd

Please sign in to comment.