Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ericstj opened this issue Nov 10, 2020 · 5 comments
Labels
Milestone

Comments

@ericstj
Copy link
Member

ericstj commented Nov 10, 2020

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));
            }
        }
    }
}
@ericstj
Copy link
Member Author

ericstj commented Nov 10, 2020

This appears to be unique to only when the root-level watched directory is removed. If a directory underneath the root directory is removed then the deleted event is observed. I've updated the sample above to better illustrate this.

@ericstj ericstj added the untriaged New issue has not been triaged by the area owner label Nov 10, 2020
@carlossanlop
Copy link
Member

Delete the directory, and no deleted event ever arrives.

It was my understanding that FileSystemWatcher does not watch changes in the monitored directory itself, only on the files and directories inside it.

I remembered that fact from this closed issue, from which I learned that not even Windows reports events from the monitored directory itself. That is how the behavior is described in the Windows API we invoke: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw

@carlossanlop carlossanlop removed the untriaged New issue has not been triaged by the area owner label Nov 14, 2020
@jozkee
Copy link
Member

jozkee commented Nov 14, 2020

Not a regression, but different than other platforms

@ericstj the sample you provided behaves similar to what you describe on Windows and Linux, is there something that I am missing?
This is the output that I get:

Created C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\sub\4fo3tpfq.nwd
Deleted C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\sub\4fo3tpfq.nwd
Created C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\sub\pboshy2t.kar
Deleted C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\sub\pboshy2t.kar
Deleted C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\sub
Created C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\k2lg0a3p.kyq
Deleted C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\k2lg0a3p.kyq
Created C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\2mjivqn4.qf5
Deleted C:\Users\dacantu\AppData\Local\Temp\rr05f1lh.mdy\2mjivqn4.qf5
False

@ericstj
Copy link
Member Author

ericstj commented Nov 14, 2020

This failure is specific to Mac (OSX) which uses a different implementation. On Mac the last deleted event will be missing.

@jozkee
Copy link
Member

jozkee commented Nov 16, 2020

I see now, it is reporting Create twice and not reporting Delete when you erase the root as you mentioned:

Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/noozqidc.ibi
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/noozqidc.ibi
Deleted /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/noozqidc.ibi
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/ibmlfqwy.nie
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/ibmlfqwy.nie
Deleted /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub/ibmlfqwy.nie
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub
Deleted /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/sub
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/xbdmmr50.et3
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/xbdmmr50.et3
Deleted /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/xbdmmr50.et3
Created /var/folders/gz/6z3dn8kn20g5wlk7rt7cs0h40000gq/T/xltqrcpx.vdv/xxfnco43.dmg
False

@jozkee jozkee added the os-mac-os-x macOS aka OSX label Nov 16, 2020
@jozkee jozkee added this to the Future milestone Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants