Skip to content

Recursive fs.watch crashes on Linux when deleting files #52018

Description

@maksis

Version

v21.7.0

Platform

Linux ARCH-FS 6.7.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Sun, 03 Mar 2024 00:30:36 +0000 x86_64 GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

Run the following code on Linux:

const fs = require('fs')

try {
  fs.mkdirSync('./parent/child', { recursive: true })
} catch (e) {
  console.error(e)
}

fs.writeFileSync('./parent/child/test.tmp', 'test')

fs.watch('./parent', { recursive: true }, (eventType, filename) => {
  console.log(eventType, filename)
})

fs.rmSync('./parent/child/test.tmp')

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

No response

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

Emit a watcher event

What do you see instead?

Error: ENOENT: no such file or directory, stat '/home/maksis/parent/child/test.tmp'
    at statSync (node:fs:1659:25)
    at FSWatcher.<anonymous> (node:internal/fs/recursive_watch:160:28)
    at FSWatcher.emit (node:events:519:28)
    at FSWatcher._handle.onchange (node:internal/fs/watchers:215:12) {

Additional information

No response

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    linuxIssues and PRs related to the Linux platform.
    on Mar 11, 2024
  2. phyreman commented on Mar 23, 2024

    @phyreman

    I've been getting this same error when watching a /parent directory and deleting a subdirectory that contains files.

  3. kylo5aby commented on Mar 26, 2024

    @kylo5aby
    Contributor

    I believe #51406 introduced this behavior, the FSWatch emit callback try to acquire the stat of the deleted file

    const currentStats = statSync(file);

    can you take a look? @mcollina

  4. mcollina commented on Apr 3, 2024

    @mcollina
    SponsorMember

    Here is the fix #52349

  5. linked a pull request that will close this issuefs: fix ENOENT on file deletion #52571on Apr 18, 2024
  6. joscarras commented on Apr 18, 2024

    @joscarras

    I have just being hit with this bug as well for node 20. Was the fix merged? And if not, what version will this fix be included?

  7. aLooseGoose commented on Apr 30, 2024

    @aLooseGoose

    @mcollina this is also a bug in v20.12, will this be backported to that or just for v22+?

  8. mcollina commented on Apr 30, 2024

    @mcollina
    SponsorMember

    it should be backported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.linuxIssues and PRs related to the Linux platform.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions