Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createReadStream seems to crash the program/make the file handles hang permanently #7957

Open
robobun opened this issue Jan 3, 2024 · 7 comments
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@robobun
Copy link

robobun commented Jan 3, 2024

createReadStream (on its own or when given a specific file handle) seems to take permanent ownership of its file handle and not let it be closed naturally/manually

bun version: 1.0.20
operating system: debian on wsl 2 (5.10.102.1-microsoft-standard-WSL2)
ulimit size: 1024

code:

import { createReadStream, close } from "fs";
import { open, writeFile } from "fs/promises";

await writeFile("mlem", Buffer.alloc(2 ** 18));

const openAndReadFile = () => new Promise(async (resolve, reject) => {
    const fd = await open("mlem", 'r');
    if (typeof fd === "number")
        createReadStream("", { fd, autoClose: false, emitClose: false })
            .on("data", chunk => { })
            .on("end", () => {
                close(fd, error => void (error ? reject(error) : resolve()));
            })
            .on("error", reject);
    else
        fd.createReadStream({ autoClose: false, emitClose: false })
            .on("data", chunk => { })
            .on("end", () => {
                fd.close().then(resolve).catch(reject);
            })
            .on("error", reject);
});

for (let i = 0; i < 3000; ++i) {
    console.log("opened", i);
    await openAndReadFile();
    console.log("closed");
}

output when ran from node.js:

opened 0
closed
...
opened 2999
closed

output when ran from bun:

opened 0
closed
...
opened 2041
closed
1 | export default "native";
    ^
EMFILE: Too many open files
   errno: -24
 syscall: "dup"
      fd: 13

      at native:1:1
      at #internalConstruct (node:1:62)
      at _read (node:1:50)
      at #internalRead (node:1:465)
      at node:1:1993
      at maybeReadMore (native:1:1)
      at processTicksAndRejections (:61:39)

Originally reported on Discord: createReadStream seems to crash the program/make the file handles hang permanently

@Electroid Electroid added bug Something isn't working node.js Compatibility with Node.js APIs labels Jan 3, 2024
@lmachens
Copy link

lmachens commented Jan 5, 2024

I am facing the same issue with my express server.
Here is a test repository:
https://github.com/lmachens/bun-file-not-closed

Each time a file is opened on the server (e.g. to serve the index.html), the file handlers count increases.).

@sroussey
Copy link
Contributor

sroussey commented Jan 6, 2024

I ran your test (bun 1.0.21) and did not see the error. I am running on a Mac, so not sure if it is the different platform or the different bun version.

@lmachens
Copy link

lmachens commented Jan 6, 2024

I ran your test (bun 1.0.21) and did not see the error. I am running on a Mac, so not sure if it is the different platform or the different bun version.

I have the same issues with 1.0.21, but on Ubuntu and with the official docker image.

@lmachens
Copy link

I tried out 1.0.22 (bun upgrade --canary && bun install) but it's still an issue

@ghost
Copy link

ghost commented Jan 10, 2024

This still needs to be fixed, but in the meanwhile, you can use Bun.file().stream() instead:

const foo = await Bun.file("foo.txt").stream();

@lmachens
Copy link

This still needs to be fixed, but in the meanwhile, you can use Bun.file().stream() instead:

const foo = await Bun.file("foo.txt").stream();

Thx. Any advice how to apply this for Express?

@ghost
Copy link

ghost commented Jan 10, 2024

This still needs to be fixed, but in the meanwhile, you can use Bun.file().stream() instead:

const foo = await Bun.file("foo.txt").stream();

Thx. Any advice how to apply this for Express?

I'm not sure about Express. There's more info about this in the docs though, if you want to look there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

4 participants