Skip to content

Commit

Permalink
fix(server): should close server after create new server (#12379)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Mar 15, 2023
1 parent 96f36a9 commit d23605d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -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)
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -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) {
Expand All @@ -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

Expand Down

0 comments on commit d23605d

Please sign in to comment.