Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 0 additions & 24 deletions src/bun.js/jsc/array_buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,30 +250,6 @@ pub const ArrayBuffer = extern struct {
return this.value;
}

// If it's not a mimalloc heap buffer, we're not going to call a deallocator
if (this.len > 0 and !bun.mimalloc.mi_is_in_heap_region(this.ptr)) {
log("toJS but will never free: {d} bytes", .{this.len});

if (this.typed_array_type == .ArrayBuffer) {
return makeArrayBufferWithBytesNoCopy(
ctx,
this.ptr,
this.byte_len,
null,
null,
);
}

return makeTypedArrayWithBytesNoCopy(
ctx,
this.typed_array_type.toTypedArrayType(),
this.ptr,
this.byte_len,
null,
null,
);
}

return this.toJSUnchecked(ctx);
}

Expand Down
2 changes: 2 additions & 0 deletions src/bun.js/webcore/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ pub const FetchTasklet = struct {
readable.ptr.Bytes.onData(
.{
.owned_and_done = bun.ByteList.moveFromList(scheduled_response_buffer),
// Investigate: owned_and_done is the correct behavior, is having bigger memory usage than temporary_and_done
// .temporary_and_done = bun.ByteList.fromBorrowedSliceDangerous(chunk),
},
bun.default_allocator,
);
Expand Down
2 changes: 1 addition & 1 deletion src/http/HTTPThread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn init(opts: *const InitOpts) void {
pub fn onStart(opts: InitOpts) void {
Output.Source.configureNamedThread("HTTP Client");
bun.http.default_arena = Arena.init();
bun.http.default_allocator = bun.http.default_arena.allocator();
bun.http.default_allocator = bun.default_allocator;

const loop = bun.jsc.MiniEventLoop.initGlobal(null, null);

Expand Down
24 changes: 24 additions & 0 deletions test/js/web/fetch/fetch-leak-test-fixture-6.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/js/web/fetch/fetch-leak.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tls as COMMON_CERT, gc, isCI } from "harness";
import { bunEnv, bunExe, bunRun, tls as COMMON_CERT, gc, isCI } from "harness";
import { once } from "node:events";
import { createServer } from "node:http";
import { join } from "node:path";
Expand Down Expand Up @@ -184,3 +184,21 @@ test("do not leak", async () => {
}
}, 1e3);
});

test("should not leak using readable stream", async () => {
const buffer = Buffer.alloc(1024 * 128, "b");
using server = Bun.serve({
port: 0,
fetch: req => {
return new Response(buffer);
},
});

const { stdout, stderr } = bunRun(join(import.meta.dir, "fetch-leak-test-fixture-6.js"), {
...bunEnv,
SERVER_URL: server.url.href,
MAX_MEMORY_INCREASE: "5", // in MB
});
expect(stderr).toBe("");
expect(stdout).toContain("done");
});