oven-sh / bun

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

Unexpected behavior using `for await (const line of console)` with UNIX pipes #11428

Open francisfontoura opened 3 months ago

francisfontoura commented 3 months ago

What version of Bun is running?

1.1.10+5102a9443

What platform is your computer?

Linux 5.15.0-107-generic x86_64 x86_64

What steps can reproduce the bug?

Save the following code as test.ts and give +x permission:

#!/usr/bin/env bun
for await (const line of console) {
  await Bun.write(Bun.stdout, line);
}

So, in shell:

curl -s https://www.meilisearch.com/movies.json | ./test.ts | ./test.ts

What is the expected behavior?

It's expected to see the contents of movies.json printed to console.

What do you see instead?

Nothing is printed but the execution remains active.

Additional information

In some cases, with some different code inside the for block, the unexpected behavior occurs without double piping, just cat large-file | ./test.ts.

Jarred-Sumner commented 3 months ago

Confirming this is a bug

Jarred-Sumner commented 3 months ago

It seems to finish after some number of minutes

akomm commented 1 month ago

May this be related with pulling just one line?

const line = (await console[Symbol.asyncIterator]().next()).value

It will pull one line, but the execution remains active, might be related to the execution remaining active in this issue.

I guess the resource is not disposed when you pull like that, bun keeps the process alive.