Skip to content

Commit c4dd7cb

Browse files
AlpAlp
authored andcommitted
fix(bun): Consume fetch response body to prevent memory leak
Bun's fetch implementation retains the backing ArrayBuffer of unconsumed response bodies indefinitely. This causes a memory leak when sending many Sentry envelopes, as each response's ArrayBuffer accumulates in memory. This applies the same fix that was made for the Cloudflare transport in getsentry#18545 — consuming the response body with response.text() after extracting the needed headers. In production, this leak manifests as ~8KB ArrayBuffers accumulating at ~8/sec (one per envelope), leading to OOM kills after ~5 hours on containers with 4GB memory limits.
1 parent 8808ea1 commit c4dd7cb

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

packages/bun/src/transports/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ export function makeFetchTransport(options: BaseTransportOptions): Transport {
1414

1515
try {
1616
return suppressTracing(() => {
17-
return fetch(options.url, requestOptions).then(response => {
17+
return fetch(options.url, requestOptions).then(async response => {
18+
// Consume the response body to prevent memory leaks in Bun's fetch implementation.
19+
// Bun retains the backing ArrayBuffer of unconsumed response bodies indefinitely,
20+
// causing memory to accumulate when sending many Sentry envelopes.
21+
// See: https://github.com/getsentry/sentry-javascript/issues/18534
22+
try {
23+
await response.text();
24+
} catch {
25+
// We don't care about the response body, but consuming it is necessary
26+
// to prevent memory leaks in Bun's fetch implementation
27+
}
28+
1829
return {
1930
statusCode: response.status,
2031
headers: {

0 commit comments

Comments
 (0)