-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(bun): Consume fetch response body to prevent memory leak #19927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Karavil
wants to merge
4
commits into
getsentry:develop
Choose a base branch
from
Karavil:fix/bun-transport-consume-response-body
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c4dd7cb
fix(bun): Consume fetch response body to prevent memory leak
9064c20
refactor: use void instead of await for response body drain
319f434
test(bun): Add transport test for response body consumption
4d256a6
fix(bun): wrap response body drain in try/catch for sync errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import type { EventEnvelope, EventItem } from '@sentry/core'; | ||
| import { createEnvelope, serializeEnvelope } from '@sentry/core'; | ||
| import { afterAll, describe, expect, it, mock } from 'bun:test'; | ||
| import { makeFetchTransport } from '../src/transports'; | ||
|
|
||
| const DEFAULT_TRANSPORT_OPTIONS = { | ||
| url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', | ||
| recordDroppedEvent: () => undefined, | ||
| }; | ||
|
|
||
| const ERROR_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ | ||
| [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, | ||
| ]); | ||
|
|
||
| const mockFetch = mock(); | ||
|
|
||
| const oldFetch = globalThis.fetch; | ||
| globalThis.fetch = mockFetch as typeof fetch; | ||
|
|
||
| afterAll(() => { | ||
| globalThis.fetch = oldFetch; | ||
| }); | ||
|
|
||
| describe('Bun Fetch Transport', () => { | ||
| it('calls fetch with the given URL', async () => { | ||
| mockFetch.mockImplementationOnce(() => | ||
| Promise.resolve({ | ||
| headers: new Headers(), | ||
| status: 200, | ||
| text: () => Promise.resolve(''), | ||
| }), | ||
| ); | ||
|
|
||
| const transport = makeFetchTransport(DEFAULT_TRANSPORT_OPTIONS); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(0); | ||
| await transport.send(ERROR_ENVELOPE); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| expect(mockFetch).toHaveBeenLastCalledWith(DEFAULT_TRANSPORT_OPTIONS.url, { | ||
| body: serializeEnvelope(ERROR_ENVELOPE), | ||
| method: 'POST', | ||
| headers: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it('sets rate limit headers', async () => { | ||
| const headers = { | ||
| get: mock((key: string) => { | ||
| if (key === 'X-Sentry-Rate-Limits') return 'rate-limit-value'; | ||
| if (key === 'Retry-After') return '42'; | ||
| return null; | ||
| }), | ||
| }; | ||
|
|
||
| mockFetch.mockImplementationOnce(() => | ||
| Promise.resolve({ | ||
| headers, | ||
| status: 200, | ||
| text: () => Promise.resolve(''), | ||
| }), | ||
| ); | ||
|
|
||
| const transport = makeFetchTransport(DEFAULT_TRANSPORT_OPTIONS); | ||
|
|
||
| const result = await transport.send(ERROR_ENVELOPE); | ||
|
|
||
| expect(headers.get).toHaveBeenCalledTimes(2); | ||
| expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
| expect(headers.get).toHaveBeenCalledWith('Retry-After'); | ||
| expect(result).toEqual({ | ||
| statusCode: 200, | ||
| headers: { | ||
| 'x-sentry-rate-limits': 'rate-limit-value', | ||
| 'retry-after': '42', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| describe('Response body consumption (issue #18534)', () => { | ||
| it('consumes the response body to prevent memory leaks in Bun', async () => { | ||
| const textMock = mock(() => Promise.resolve('OK')); | ||
| const headers = { | ||
| get: mock(() => null), | ||
| }; | ||
| mockFetch.mockImplementationOnce(() => | ||
| Promise.resolve({ | ||
| headers, | ||
| status: 200, | ||
| text: textMock, | ||
| }), | ||
| ); | ||
|
|
||
| const transport = makeFetchTransport(DEFAULT_TRANSPORT_OPTIONS); | ||
|
|
||
| await transport.send(ERROR_ENVELOPE); | ||
|
|
||
| expect(textMock).toHaveBeenCalledTimes(1); | ||
| expect(headers.get).toHaveBeenCalledTimes(2); | ||
| expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
| expect(headers.get).toHaveBeenCalledWith('Retry-After'); | ||
| }); | ||
|
|
||
| it('handles response body consumption errors gracefully', async () => { | ||
| const textMock = mock(() => Promise.reject(new Error('Body read error'))); | ||
| const headers = { | ||
| get: mock(() => null), | ||
| }; | ||
|
|
||
| mockFetch.mockImplementationOnce(() => | ||
| Promise.resolve({ | ||
| headers, | ||
| status: 200, | ||
| text: textMock, | ||
| }), | ||
| ); | ||
|
|
||
| const transport = makeFetchTransport(DEFAULT_TRANSPORT_OPTIONS); | ||
|
|
||
| // Should not throw even though text() rejects | ||
| const result = await transport.send(ERROR_ENVELOPE); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(textMock).toHaveBeenCalledTimes(1); | ||
| expect(headers.get).toHaveBeenCalledTimes(2); | ||
| expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
| expect(headers.get).toHaveBeenCalledWith('Retry-After'); | ||
| }); | ||
|
|
||
| it('handles a response without a text method', async () => { | ||
| const headers = { | ||
| get: mock(() => null), | ||
| }; | ||
|
|
||
| mockFetch.mockImplementationOnce(() => | ||
| Promise.resolve({ | ||
| headers, | ||
| status: 200, | ||
| // No text method on the response | ||
| }), | ||
| ); | ||
|
|
||
| const transport = makeFetchTransport(DEFAULT_TRANSPORT_OPTIONS); | ||
|
|
||
| // Should not throw even without text() | ||
| const result = await transport.send(ERROR_ENVELOPE); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(headers.get).toHaveBeenCalledTimes(2); | ||
| expect(headers.get).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
| expect(headers.get).toHaveBeenCalledWith('Retry-After'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: I think the following would be enough as we already add a
.catchto the promise.