Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): should close server after create new server #12379

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,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)
Copy link
Member Author

@sun0day sun0day Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempDirPath maybe removed when server.close(), so add a catch to make clean up process work properly, this will solve sapphi-red's issue.

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
Original file line number Diff line number Diff line change
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