oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.83k stars 2.64k forks source link

`Bun.stdin.stream()` and `process.stdin` never closes. #3255

Open paperdave opened 1 year ago

paperdave commented 1 year ago

What version of Bun is running?

No response

What platform is your computer?

No response

What steps can reproduce the bug?

const stream = Bun.stdin.stream().getReader();

while (true) {
  const { done, value } = await stream.read();
  if (done) break;
  console.log(value);
}

console.log("Exiting...");
process.stdin.on("close", () => {
  console.log("Exiting...");
});

process.stdin.on("data", (data) => {
  console.log(`Data: ${data}`);
});

run and press ctrl+d. should print Exiting... and exit. on bun, it doesn't do anything and the shell prints ^D

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

This is related to #2333 and #1873. Opening a separate issue for specifically using Bun.stdin on it's own instead of how it acts in readline, but this might fix the other bugs.

paperdave commented 1 year ago

extra note: works with Bun.file("/dev/stdin").stream().getReader() but not Bun.file(0).stream().getReader()

Jarred-Sumner commented 1 year ago

I don't think this is a bug

Closing the stdin file descriptor is a very destructive action. It should basically only happen when the application is closing

MatthiasPortzel commented 1 year ago

await Bun.stdin.text() also never returns making it functionally useless, which is frustrating. I suspect it shares a root cause with this bug.

guest271314 commented 2 months ago

extra note: works with Bun.file("/dev/stdin").stream().getReader() but not Bun.file(0).stream().getReader()

Mentioning Bun.file() helps here https://github.com/oven-sh/bun/issues/11553#issuecomment-2146578443. And I suspect here https://github.com/oven-sh/bun/issues/8695.

Bun.stdin.stream() does not work as expected, consistently hangs on 983036 bytes in async iteration where 1048576 are expected.

Bun.file(0).stream() behaves the same as Bun.stdin.stream(), yielding 983036 of 1048576 bytes expected.

That was not the case as recently as this commit https://github.com/guest271314/NativeMessagingHosts/commit/4878edcb14401d3f2b5aac08448f8e0f0916956, Feb 16 of this year.

Bun.file("/dev/stdin").stream() and Bun.file("/proc/self/fd/0").stream() do work as expected.

if (runtime.startsWith("Bun")) {
  // Doesn't work
  // readable = Bun.stdin.stream();
  // readable = Bun.file(0).stream();
  // Works
  // readable = Bun.file("/dev/stdin").stream();
  readable = Bun.file("/proc/self/fd/0").stream();
  writable = new WritableStream({
    async write(value) {
      await Bun.write(Bun.stdout, value);
    },
  }, new CountQueuingStrategy({ highWaterMark: Infinity }));
  ({ exit } = process);
  ({ argv: args } = Bun);
}
MatthiasPortzel commented 2 months ago

@guest271314 Sounds like a different bug, if it originated after this bug was created.

The behavior I was experiencing no longer occurs.

guest271314 commented 2 months ago

Alright. This is as close to what I am reproducing. I searched before reporting documentation and runtime issues. Bun.stdin.stream() stops reading consistently at 983036. Thanks.