Skip to content

Commit 34c77ca

Browse files
committed
fix #4231: memory leak inside bundler
1 parent 4b596ce commit 34c77ca

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* Fix a memory leak when `cancel()` is used on a build context ([#4231](https://github.com/evanw/esbuild/issues/4231))
6+
7+
Calling `rebuild()` followed by `cancel()` in rapid succession could previously leak memory. The bundler uses a producer/consumer model internally, and the resource leak was caused by the consumer being termianted while there were still remaining unreceived results from a producer. To avoid the leak, the consumer now waits for all producers to finish before terminating.
8+
59
* Consider negated bigints to have no side effects
610

711
While esbuild currently considers `1`, `-1`, and `1n` to all have no side effects, it didn't previously consider `-1n` to have no side effects. This is because esbuild does constant folding with numbers but not bigints. However, it meant that unused negative bigint constants were not tree-shaken. With this release, esbuild will now consider these expressions to also be side-effect free:

internal/bundler/bundler.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,17 @@ func ScanBundle(
13571357
}
13581358
}()
13591359

1360+
// Always consume all unused results. Failing to do so could cause a memory
1361+
// leak if a build is cancelled. We can't cancel the producers by closing the
1362+
// channel because doing so in Go causes a panic (which is arguably a design
1363+
// bug with Go).
1364+
defer func() {
1365+
for s.remaining > 0 {
1366+
<-s.resultChannel
1367+
s.remaining--
1368+
}
1369+
}()
1370+
13601371
// Wait for all "onStart" plugins here before continuing. People sometimes run
13611372
// setup code in "onStart" that "onLoad" expects to be able to use without
13621373
// "onLoad" needing to block on the completion of their "onStart" callback.

0 commit comments

Comments
 (0)