Skip to content

Commit

Permalink
fix: worker build
Browse files Browse the repository at this point in the history
When we have multiple workers, the config cannot be used as web map key since the config can mutate.

Since we're not in a multi-thread environment we can use a fixed key to store the promise of the current worker build.

You can use the reproduction provided in vitejs#13367 , when building the third worker, there is a pending promise since the config changes and there is no way to delete it and then the error.
  • Loading branch information
userquin committed Oct 13, 2023
1 parent cd82648 commit 3e797be
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function saveEmitWorkerAsset(
// Ensure that only one rollup build is called at the same time to avoid
// leaking state in plugins between worker builds.
// TODO: Review if we can parallelize the bundling of workers.
const workerKey = { key: 'vite:worker' }
const workerConfigSemaphore = new WeakMap<
ResolvedConfig,
Promise<OutputChunk>
Expand All @@ -53,14 +54,14 @@ export async function bundleWorkerEntry(
id: string,
query: Record<string, string> | null,
): Promise<OutputChunk> {
const processing = workerConfigSemaphore.get(config)
const processing = workerConfigSemaphore.get(workerKey)
if (processing) {
await processing
return bundleWorkerEntry(config, id, query)
}
const promise = serialBundleWorkerEntry(config, id, query)
workerConfigSemaphore.set(config, promise)
promise.then(() => workerConfigSemaphore.delete(config))
workerConfigSemaphore.set(workerKey, promise)
promise.then(() => workerConfigSemaphore.delete(workerKey))
return promise
}

Expand Down

0 comments on commit 3e797be

Please sign in to comment.