Skip to content
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

fix: memory leak when reading chunks from a stream #16075

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/bun.js/webcore/streams.zig
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ pub const StreamResult = union(Tag) {
switch (this.*) {
.owned => |*owned| owned.deinitWithAllocator(bun.default_allocator),
.owned_and_done => |*owned_and_done| owned_and_done.deinitWithAllocator(bun.default_allocator),
// .temporary => |*temporary| temporary.deinitWithAllocator(bun.default_allocator),
// .temporary_and_done => |*temporary_and_done| temporary_and_done.deinitWithAllocator(bun.default_allocator),
.err => |err| {
if (err == .JSValue) {
err.JSValue.unprotect();
Expand Down Expand Up @@ -1021,6 +1023,11 @@ pub const StreamResult = union(Tag) {
const value = result.toJS(globalThis);
value.ensureStillAlive();

switch (result.*) {
.temporary => |*temporary| temporary.deinitWithAllocator(bun.default_allocator),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary should mean that the StreamResult don't own the data and should be cloned, the owner (how created the StreamResult) should free it.

.temporary_and_done => |*temporary_and_done| temporary_and_done.deinitWithAllocator(bun.default_allocator),
else => {},
}
result.* = .{ .temporary = .{} };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause JSValues for .owned, etc. variants to point to leaked/undefined memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does line 1046 move or borrow?

Man I miss Rust's borrow checker...

promise.resolve(globalThis, value);
},
Expand Down
Loading