Skip to content

node:fs opendir async iterator allows use-after-close without throwing ERR_DIR_CLOSED #34900

@kevgeoleo

Description

@kevgeoleo

Version: Deno 2.8.2

Hi,

I would like to report a behavior in Deno that can be reproduced using the below snippet:

import { promises as fs } from "node:fs";

const dir = await fs.opendir(".");

// create async iterator from the Dir handle
const iter = dir[Symbol.asyncIterator]();

// start reading one entry
await iter.next();

// close the directory while iterator is still “live”
await dir.close();

// now try to continue iteration
await iter.next();

Expected behavior

root@KContainer:~/16622# node a.mjs
node:internal/fs/dir:254
      process.nextTick(callback, new ERR_DIR_CLOSED());
                                 ^

Error [ERR_DIR_CLOSED]: Directory handle was closed
    at Dir.close (node:internal/fs/dir:254:34)
    at node:internal/util:500:21
    at new Promise (<anonymous>)
    at Dir.close (node:internal/util:486:12)
    at Dir.entries (node:internal/fs/dir:304:35)
    at async file:///home/c01kele/16622/a.mjs:15:1 {
  code: 'ERR_DIR_CLOSED'
}

Node.js v24.13.1

Actual behavior

No error is thrown; iteration continues or resolves without enforcing the closed state.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions