Skip to content

fs.Dir behaves differently in bun compared to node when using async iteration #28894

@huseeiin

Description

@huseeiin

What version of Bun is running?

1.3.11+af24e281e

What platform is your computer?

Linux 6.12.74+deb13+1-amd64 x86_64 unknown

What steps can reproduce the bug?

import { opendir } from "node:fs/promises";

async function main() {
  const dir = await opendir(".");

  try {
    for await (const entry of dir) {
      console.log(entry.name);
      break;
    }
  } finally {
    console.log("closing");
    await dir.close();
    console.log("closed");
  }
}

await main();

What is the expected behavior?

  • for await (const entry of dir) exits early via break

  • node automatically closes the directory handle

  • calling await dir.close() afterward should throw an error (already closed)

  • node throws something like:

Error: Directory handle was closed

What do you see instead?

  • loop exits the same way
  • calling await dir.close() does not throw
  • program continues and prints:
closing
closed

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions