From 240ea8a33f11a6aa2aff1a7c1dba3022de258d3e Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:55:44 -0700 Subject: [PATCH 1/4] Workaround for #9041 --- src/js/node/stream.js | 12 +++++++++ test/regression/issue/09041.test.ts | 26 +++++++++++++++++++ .../issue/09041/09041-fixture-test.txt | 18 +++++++++++++ test/regression/issue/09041/09041-fixture.mjs | 4 +++ 4 files changed, 60 insertions(+) create mode 100644 test/regression/issue/09041.test.ts create mode 100644 test/regression/issue/09041/09041-fixture-test.txt create mode 100644 test/regression/issue/09041/09041-fixture.mjs diff --git a/src/js/node/stream.js b/src/js/node/stream.js index 74f2859f4bb757..f30f5763d4ad55 100644 --- a/src/js/node/stream.js +++ b/src/js/node/stream.js @@ -3388,9 +3388,21 @@ var require_readable = __commonJS({ }; Readable.fromWeb = function (readableStream, options) { + // We cache .stream() calls for file descriptors + // This won't create a new ReadableStream each time. + let bunStdinStream = Bun.stdin.stream(); + if (readableStream === bunStdinStream) { + return bunStdinStream; + } + return webStreamsAdapters.newStreamReadableFromReadableStream(readableStream, options); }; Readable.toWeb = function (streamReadable, options) { + // Workaround for https://github.com/oven-sh/bun/issues/9041 + if (streamReadable === process.stdin) { + return Bun.stdin.stream(); + } + return webStreamsAdapters.newReadableStreamFromStreamReadable(streamReadable, options); }; Readable.wrap = function (src, options) { diff --git a/test/regression/issue/09041.test.ts b/test/regression/issue/09041.test.ts new file mode 100644 index 00000000000000..caf673bc4c6866 --- /dev/null +++ b/test/regression/issue/09041.test.ts @@ -0,0 +1,26 @@ +import { test, expect } from "bun:test"; +import { bunEnv, bunExe, tempDirWithFiles } from "harness"; +import { join } from "path"; +import { $ } from "bun"; +import { cp, rm } from "fs/promises"; +test("09041", async () => { + const out = tempDirWithFiles("09041", { + "09041-fixture.mjs": await Bun.file(join(import.meta.dir, "09041", "09041-fixture.mjs")).text(), + "09041-fixture.test.js": await Bun.file(join(import.meta.dir, "09041", "09041-fixture-test.txt")).text(), + "package.json": `{}`, + }); + + let { exited, stderr, stdout } = Bun.spawn({ + cmd: [bunExe(), "test"], + cwd: out, + env: bunEnv, + stdio: ["ignore", "pipe", "inherit"], + }); + + expect(await exited).toBe(0); + const err = await new Response(stderr).text(); + expect(err).toContain("1 pass"); + expect(err).toContain("0 fail"); + + await rm(out, { force: true, recursive: true }); +}, 10000); diff --git a/test/regression/issue/09041/09041-fixture-test.txt b/test/regression/issue/09041/09041-fixture-test.txt new file mode 100644 index 00000000000000..c812d521f9e702 --- /dev/null +++ b/test/regression/issue/09041/09041-fixture-test.txt @@ -0,0 +1,18 @@ +import { $ } from "bun"; +import {join } from 'path'; +test("09041", async () => { + const buffer = Buffer.alloc(1024 * 1024); + buffer.fill("1234567890"); + const buns = Array.from( + { length: 5 }, + () => $`${process.argv0} run ${join(import.meta.dir, "09041-fixture.mjs")} < ${buffer}`, + ); + + const runs = await Promise.all(buns); + for (let i = 0; i < runs.length; i++) { + const run = runs[i]; + expect(run.exitCode).toBe(0); + expect(run.stdout).toHaveLength(1024 * 1024); + expect(run.stdout).toEqual(buffer); + } +}, 10000); diff --git a/test/regression/issue/09041/09041-fixture.mjs b/test/regression/issue/09041/09041-fixture.mjs new file mode 100644 index 00000000000000..931d2a56fb836f --- /dev/null +++ b/test/regression/issue/09041/09041-fixture.mjs @@ -0,0 +1,4 @@ +// This file is intended to be able to run in Node and Bun +import { Readable, pipeline, PassThrough } from "node:stream"; + +pipeline(Readable.toWeb(process.stdin), new PassThrough(), process.stdout, () => {}); From 7ea36690de4c4d23dbf01a67686931b848d10ff1 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sat, 23 Mar 2024 02:58:29 -0700 Subject: [PATCH 2/4] Fix crash with auto install --- src/bun.js/javascript.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index c474b76c66e14e..75b94998843370 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1925,7 +1925,8 @@ pub const VirtualMachine = struct { } const old_log = jsc_vm.log; - var log = logger.Log.init(jsc_vm.allocator); + // the logger can end up being called on another thread, it must not use threadlocal Heap Allocator + var log = logger.Log.init(bun.default_allocator); defer log.deinit(); jsc_vm.log = &log; jsc_vm.bundler.resolver.log = &log; From 1d6e7a714a453c7fed41c4d33305fa67928d414f Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 27 Mar 2024 19:26:33 -0700 Subject: [PATCH 3/4] Fixup this test --- test/regression/issue/09041.test.ts | 5 +++-- test/regression/issue/09041/09041-fixture-test.txt | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/regression/issue/09041.test.ts b/test/regression/issue/09041.test.ts index caf673bc4c6866..91a8a6b094e701 100644 --- a/test/regression/issue/09041.test.ts +++ b/test/regression/issue/09041.test.ts @@ -14,13 +14,14 @@ test("09041", async () => { cmd: [bunExe(), "test"], cwd: out, env: bunEnv, - stdio: ["ignore", "pipe", "inherit"], + stdio: ["ignore", "pipe", "pipe"], }); expect(await exited).toBe(0); const err = await new Response(stderr).text(); expect(err).toContain("1 pass"); expect(err).toContain("0 fail"); - + const std = await new Response(stdout).text(); await rm(out, { force: true, recursive: true }); + expect(std.length).toBeGreaterThan(65 * 1024); }, 10000); diff --git a/test/regression/issue/09041/09041-fixture-test.txt b/test/regression/issue/09041/09041-fixture-test.txt index c812d521f9e702..431e5225db8a90 100644 --- a/test/regression/issue/09041/09041-fixture-test.txt +++ b/test/regression/issue/09041/09041-fixture-test.txt @@ -2,7 +2,10 @@ import { $ } from "bun"; import {join } from 'path'; test("09041", async () => { const buffer = Buffer.alloc(1024 * 1024); - buffer.fill("1234567890"); + let i = 0; + while (i < 1024 * 1024) { + i += buffer.write(Number(i).toString(10), i); + } const buns = Array.from( { length: 5 }, () => $`${process.argv0} run ${join(import.meta.dir, "09041-fixture.mjs")} < ${buffer}`, From cb92d017f6e51cd54bdb267d78507db40fcb7861 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 27 Mar 2024 19:33:54 -0700 Subject: [PATCH 4/4] Update 09041.test.ts --- test/regression/issue/09041.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/regression/issue/09041.test.ts b/test/regression/issue/09041.test.ts index 91a8a6b094e701..5531a2c6261eee 100644 --- a/test/regression/issue/09041.test.ts +++ b/test/regression/issue/09041.test.ts @@ -3,6 +3,7 @@ import { bunEnv, bunExe, tempDirWithFiles } from "harness"; import { join } from "path"; import { $ } from "bun"; import { cp, rm } from "fs/promises"; + test("09041", async () => { const out = tempDirWithFiles("09041", { "09041-fixture.mjs": await Bun.file(join(import.meta.dir, "09041", "09041-fixture.mjs")).text(), @@ -22,6 +23,6 @@ test("09041", async () => { expect(err).toContain("1 pass"); expect(err).toContain("0 fail"); const std = await new Response(stdout).text(); - await rm(out, { force: true, recursive: true }); - expect(std.length).toBeGreaterThan(65 * 1024); + + expect(std.length).toBeGreaterThan(1024 * 1024); }, 10000);