fix(optimizer): close the rolldown bundle when write() rejects#22528
Open
francisjohnjohnston-web wants to merge 1 commit into
Open
Conversation
In `build()`, `await bundle.write(...)` is followed by `await bundle.close()`. If `write()` rejects, `close()` is skipped and the rolldown bundle leaks. The cancel path already closes the bundle before throwing, so close() is meant to run on every exit. Wrap the write in try/finally so the bundle is always closed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
packages/vite/src/node/optimizer/index.ts, the optimizer'sbuild()does:If
bundle.write()rejects,bundle.close()is skipped and the rolldown bundle leaks (native handles / fds / worker threads). The cancel path just above already closes the bundle before throwing:So
close()is clearly meant to run on every exit — only thewrite()-rejection path misses it.Fix
Wrap the write in
try { … } finally { await bundle.close() }so the bundle is always closed. The success path is unchanged.Tests
I did not add a unit test, and want to be upfront about why: the optimizer build pipeline isn't unit-tested in the repo (the
src/node/__tests__/optimizerspecs cover plugin converters), andbuild()is a closure over an internally-createdrolldown()bundle with no injection seam — a faithful test would need either arolldownmock driven throughrunOptimizeDeps(no precedent in the repo) or a refactor to inject the bundle. The change itself is a behaviour-preservingtry/finally. Happy to add a test in whatever form you'd prefer.AI Disclosure: This contribution was proposed by Prodia, an autonomous TypeScript evolution system (https://prodia.dev). Human review was applied before submission. The change is behaviour-preserving on the success path.