Skip to content

fs.rmSync(path, { recursive: true, maxRetries: manyManyRetries}) throws on ENFILE #55555

Open
@LiviaMedeiros

Description

@LiviaMedeiros

Version

v22.8.0

Platform

Linux tumba 6.11.4-gentoo-yuran-r7 #8 SMP Sat Oct 19 16:45:43 +08 2024 x86_64 Intel(R) Core(TM)2 Duo CPU     E6550 GenuineIntel GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

Set low limits or high FILE_COUNT, run the following:

import fs from 'node:fs';
import { readFile } from 'node:fs/promises';

const OUTPUT_DIRECTORY = "/tmp/emfile-check";
const FILE_COUNT = 150_000; // adjust this according to ulimit -n, ulimit -Hn, /proc/sys/fs/file-max, etc.

function generateFiles() {
  fs.rmSync(OUTPUT_DIRECTORY, { recursive: true, force: true, maxRetries: 8192 });
  fs.mkdirSync(OUTPUT_DIRECTORY, { recursive: true });
  for (let i = 0; i < FILE_COUNT; i++) {
    const fileName = `file_${i}.js`;
    const fileContent = `// This is file ${i}`;
    fs.writeFileSync(`${OUTPUT_DIRECTORY}/${fileName}`, fileContent);
  }
}

function generateEmFileError() {
  return Promise.all(
    Array.from({ length: FILE_COUNT }, (_, i) => {
      const fileName = `file_${i}.js`;
      return readFile(`${OUTPUT_DIRECTORY}/${fileName}`);
    })
  );
}

console.log(`Generating ${FILE_COUNT} files in ${OUTPUT_DIRECTORY}...`);
generateFiles();

console.log("Checking that this number of files would cause an EMFILE error...");
await generateEmFileError()
  .then(() => {
    throw new Error("Test failure: not enough files to encounter EMFILE.");
  })
  .catch(error => {
    if (error.code === "EMFILE") {
      console.log("Successfully got intentional EMFILE:", error.message);
    } else if (error.code === "ENFILE") {
      console.log("Successfully got intentional ENFILE:", error.message);
    } else {
      console.error("Failed with unknown error:", error.message);
      throw error;
    }
  })
  .finally(() => {
    console.log(`Attempting to remove ${OUTPUT_DIRECTORY}`);
    fs.rmSync(OUTPUT_DIRECTORY, { recursive: true, force: true, maxRetries: 8192 });
    console.log(`Successfully removed ${OUTPUT_DIRECTORY}`);
  });

How often does it reproduce? Is there a required condition?

Might be flaky depending on system configuration (under some conditions exhausting fd limits may render system unresponsive).

What is the expected behavior? Why is that the expected behavior?

Generating 150000 files in /tmp/emfile-check...
Checking that this number of files would cause an EMFILE error...
Successfully got intentional ENFILE: ENFILE: file table overflow, open '/tmp/emfile-check/file_63275.js'
Attempting to remove /tmp/emfile-check
Successfully removed /tmp/emfile-check

We should encounter the ENFILE via generateEmFileError function, catch the error, and remove the directory. Directory removal might encounter errors due to unfinished jobs, but it should retry with 100ms delay 8192 times (more than 13 minutes).

What do you see instead?

Generating 150000 files in /tmp/emfile-check...
Checking that this number of files would cause an EMFILE error...
Successfully got intentional ENFILE: ENFILE: file table overflow, open '/tmp/emfile-check/file_63275.js'
Attempting to remove /tmp/emfile-check
node:fs:1502
  const result = binding.readdir(
                         ^

Error: ENFILE: file table overflow, scandir '/tmp/emfile-check'
    at readdirSync (node:fs:1502:26)
    at _rmdirSync (node:internal/fs/rimraf:249:29)
    at rimrafSync (node:internal/fs/rimraf:192:7)
    at Object.rmSync (node:fs:1247:10)
    at file:///tmp/repro/t.mjs:52:8
    at <anonymous>
    at async file:///tmp/repro/t.mjs:36:1 {
  errno: -23,
  code: 'ENFILE',
  syscall: 'scandir',
  path: '/tmp/emfile-check'
}

Node.js v22.8.0

Additional information

According to the docs, it must continue in case of ENFILE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions