Skip to content

FileSystemWatcher doesn't trigger Deleted event for file when watched directory is deleted on OSX #44484

@ericstj

Description

@ericstj

Description

Create a FileSystemWatcher that watches a directory.
Create a file in that directory and observe Created event.
Delete the file and observe Deleted event.
Create another file in that directory and observe Created event.
Delete the directory, and no deleted event ever arrives.

Configuration

  • Which version of .NET is the code running on? .NET 5.0
  • What OS and version, and what distro if applicable? OSX 10.13.6
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? It appears to be specific to OSX

Regression?

Not a regression, but different than other platforms

Other information

May be similar to #30415
Discovered when working on #41426

Repro:

using System;
using System.IO;
using System.Threading.Tasks;


namespace fileWatch
{
    class Program
    {
        static async Task Main(string[] args)
        {
            int waitTime = 1000;
            var rootPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var subDir = Path.Combine(rootPath, "sub");
            
            Directory.CreateDirectory(rootPath);
            Directory.CreateDirectory(subDir);

            using (var fsw = new FileSystemWatcher(rootPath, "*"))
            {
                fsw.Created += (o, e) => Console.WriteLine($"Created {e.FullPath}");
                fsw.Deleted += (o, e) => Console.WriteLine($"Deleted {e.FullPath}");
                fsw.IncludeSubdirectories = true;
                fsw.EnableRaisingEvents = true;

                var fileLocation = Path.Combine(subDir, Path.GetRandomFileName());
                File.WriteAllText(fileLocation, "temp");
                await Task.Delay(waitTime);
                File.Delete(fileLocation);
                await Task.Delay(waitTime);

                fileLocation = Path.Combine(subDir, Path.GetRandomFileName());
                File.WriteAllText(fileLocation, "temp");
                await Task.Delay(waitTime);
                Directory.Delete(subDir, true);
                await Task.Delay(waitTime);

                fileLocation = Path.Combine(rootPath, Path.GetRandomFileName());
                File.WriteAllText(fileLocation, "temp");
                await Task.Delay(waitTime);
                File.Delete(fileLocation);
                await Task.Delay(waitTime);

                fileLocation = Path.Combine(rootPath, Path.GetRandomFileName());
                File.WriteAllText(fileLocation, "temp");
                await Task.Delay(waitTime);
                Directory.Delete(rootPath, true);
                await Task.Delay(waitTime);
                Console.WriteLine(File.Exists(fileLocation));
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions