Skip to content

Commit

Permalink
fix: properly close optimizer on server restart (#10028)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Sep 23, 2022
1 parent 7f548e8 commit a32777f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
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 @@ -398,6 +398,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

0 comments on commit a32777f

Please sign in to comment.