From d23605d01449706988be2eb77f2654238778fca8 Mon Sep 17 00:00:00 2001 From: sun0day Date: Wed, 15 Mar 2023 23:43:04 +0800 Subject: [PATCH] fix(server): should close server after create new server (#12379) --- packages/vite/src/node/optimizer/index.ts | 7 +++++-- packages/vite/src/node/server/index.ts | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 93989cae446a2f..62b3a9f14d7494 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1296,8 +1296,11 @@ export async function cleanupDepsCacheStaleDirs( for (const dirent of dirents) { if (dirent.isDirectory() && dirent.name.includes('_temp_')) { const tempDirPath = path.resolve(config.cacheDir, dirent.name) - const { mtime } = await fsp.stat(tempDirPath) - if (Date.now() - mtime.getTime() > MAX_TEMP_DIR_AGE_MS) { + const stats = await fsp.stat(tempDirPath).catch((_) => null) + if ( + stats?.mtime && + Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS + ) { await removeDir(tempDirPath) } } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 81d8921a23b663..4644c06f2d379d 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -816,7 +816,6 @@ async function restartServer(server: ViteDevServer) { const { port: prevPort, host: prevHost } = server.config.server const shortcutsOptions: BindShortcutsOptions = server._shortcutsOptions const oldUrls = server.resolvedUrls - await server.close() let inlineConfig = server.config.inlineConfig if (server._forceOptimizeOnRestart) { @@ -834,9 +833,12 @@ async function restartServer(server: ViteDevServer) { server.config.logger.error(err.message, { timestamp: true, }) + server.config.logger.error('server restart failed', { timestamp: true }) return } + await server.close() + // prevent new server `restart` function from calling newServer._restartPromise = server._restartPromise