fix(nitro): handle terminating uninitialized worker

This commit is contained in:
Pooya Parsa 2021-10-22 15:36:29 +02:00
parent 018302cb0b
commit 1e69bc56bb

View File

@ -43,8 +43,11 @@ function initWorker (filename): Promise<NitroWorker> {
}) })
} }
async function killWorker (worker: NitroWorker) { async function killWorker (worker?: NitroWorker) {
await worker.worker.terminate() if (!worker) {
return
}
await worker.worker?.terminate()
worker.worker = null worker.worker = null
if (worker.address && existsSync(worker.address)) { if (worker.address && existsSync(worker.address)) {
await fsp.rm(worker.address).catch(() => {}) await fsp.rm(worker.address).catch(() => {})
@ -62,9 +65,7 @@ export function createDevServer (nitroContext: NitroContext) {
const newWorker = await initWorker(workerEntry) const newWorker = await initWorker(workerEntry)
// Kill old worker in background // Kill old worker in background
if (currentWorker) { killWorker(currentWorker).catch(err => console.error(err))
killWorker(currentWorker).catch(err => console.error(err))
}
// Replace new worker as current // Replace new worker as current
currentWorker = newWorker currentWorker = newWorker
@ -136,9 +137,7 @@ export function createDevServer (nitroContext: NitroContext) {
if (watcher) { if (watcher) {
await watcher.close() await watcher.close()
} }
if (currentWorker) { await killWorker(currentWorker)
await killWorker(currentWorker)
}
await Promise.all(listeners.map(l => l.close())) await Promise.all(listeners.map(l => l.close()))
listeners = [] listeners = []
} }