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: properly close optimizer on server restart #10028

Merged
merged 1 commit into from
Sep 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface DepsOptimizer {
resetRegisteredIds: () => void
ensureFirstRun: () => void

close: () => Promise<void>

options: DepOptimizationOptions
}

Expand Down
29 changes: 25 additions & 4 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ async function createDepsOptimizer(

let handle: NodeJS.Timeout | undefined

let closed = false

let metadata =
cachedMetadata || initDepsOptimizerMetadata(config, ssr, sessionTimestamp)

Expand All @@ -118,6 +120,7 @@ async function createDepsOptimizer(
delayDepsOptimizerUntil,
resetRegisteredIds,
ensureFirstRun,
close,
options: getDepOptimizationConfig(config, ssr)
}

Expand Down Expand Up @@ -159,6 +162,13 @@ async function createDepsOptimizer(

let postScanOptimizationResult: Promise<DepOptimizationResult> | undefined

let optimizingNewDeps: Promise<DepOptimizationResult> | undefined
async function close() {
closed = true
await postScanOptimizationResult
await optimizingNewDeps
}

if (!cachedMetadata) {
// Enter processing state until crawl of static imports ends
currentlyProcessing = true
Expand Down Expand Up @@ -288,15 +298,24 @@ async function createDepsOptimizer(
// Ensure that a rerun will not be issued for current discovered deps
if (handle) clearTimeout(handle)

if (Object.keys(metadata.discovered).length === 0) {
if (closed || Object.keys(metadata.discovered).length === 0) {
currentlyProcessing = false
return
}

currentlyProcessing = true

try {
const processingResult = preRunResult ?? (await optimizeNewDeps())
const processingResult =
preRunResult ?? (await (optimizingNewDeps = optimizeNewDeps()))
optimizingNewDeps = undefined

if (closed) {
currentlyProcessing = false
processingResult.cancel()
resolveEnqueuedProcessingPromises()
return
}

const newData = processingResult.metadata

Expand Down Expand Up @@ -665,7 +684,7 @@ async function createDepsOptimizer(
function ensureFirstRun() {
if (!firstRunEnsured && !firstRunCalled && registeredIds.length === 0) {
setTimeout(() => {
if (registeredIds.length === 0) {
if (!closed && registeredIds.length === 0) {
onCrawlEnd()
}
}, runOptimizerIfIdleAfterMs)
Expand Down Expand Up @@ -699,7 +718,7 @@ async function createDepsOptimizer(
waitingOn = next.id
const afterLoad = () => {
waitingOn = undefined
if (!workersSources.has(next.id)) {
if (!closed && !workersSources.has(next.id)) {
if (registeredIds.length > 0) {
runOptimizerWhenIdle()
} else {
Expand Down Expand Up @@ -745,6 +764,8 @@ async function createDevSsrDepsOptimizer(
delayDepsOptimizerUntil: (id: string, done: () => Promise<any>) => {},
resetRegisteredIds: () => {},
ensureFirstRun: () => {},

close: async () => {},
options: config.ssr.optimizeDeps
}
devSsrDepsOptimizerMap.set(config, depsOptimizer)
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ export async function createServer(
watcher.close(),
ws.close(),
container.close(),
getDepsOptimizer(server.config)?.close(),
getDepsOptimizer(server.config, true)?.close(),
closeHttpServer()
])
server.resolvedUrls = null
Expand Down