Fix ref-counting of Bun.file(0) etc. (#8687) (#8741)

* Test for #8687

* Fix reference counting of Bun.file(0) etc. (#8687)

Fixes #8687.
This commit is contained in:
argosphil
2024-02-07 02:11:25 +00:00
committed by GitHub
parent 651d5699a0
commit e171f04ce6
2 changed files with 20 additions and 13 deletions

View File

@@ -1553,20 +1553,19 @@ pub const Blob = struct {
break :brk copy;
},
.fd => {
switch (bun.FDTag.get(path_or_fd.fd)) {
.stdin => return Blob.initWithStore(
vm.rareData().stdin(),
const optional_store: ?*Store = switch (bun.FDTag.get(path_or_fd.fd)) {
.stdin => vm.rareData().stdin(),
.stderr => vm.rareData().stderr(),
.stdout => vm.rareData().stdout(),
else => null,
};
if (optional_store) |store| {
store.ref();
return Blob.initWithStore(
store,
globalThis,
),
.stderr => return Blob.initWithStore(
vm.rareData().stderr(),
globalThis,
),
.stdout => return Blob.initWithStore(
vm.rareData().stdout(),
globalThis,
),
else => {},
);
}
break :brk path_or_fd.*;
},

View File

@@ -310,6 +310,14 @@ it("Bun.write(Bun.stderr, 'new TextEncoder().encode(Bun.write STDERR TEST'))", a
expect(await Bun.write(Bun.stderr, new TextEncoder().encode("\nBun.write STDERR TEST\n\n"))).toBe(24);
});
it("Bun.file(0) survives GC", async () => {
for (let i = 0; i < 10; i++) {
let f = Bun.file(0);
await gcTick();
expect(Bun.inspect(f)).toContain("FileRef (fd: 0)");
}
});
// FLAKY TEST
// Since Bun.file is resolved lazily, this needs to specifically be checked
it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", async done => {