Why Filesystemwatcher Doesn't Work in Linux Container Watching Windows Volume

Why FileSystemWatcher doesn't work in Linux container watching Windows volume

Switching to PhysicalFileProvider did the job.
It seems to be a more portable implementation for file system watching strategies.

The current implementation of PhysicalFileProvider supports the DOTNET_USE_POLLING_FILE_WATCHER environment variable. I couldn't find any reference of it in FileSystemWatcher implementation.

using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using System;
using System.IO;

namespace fsw_bug_poc
{
class Program
{
private static PhysicalFileProvider _fileProvider;
private static IChangeToken _fileChangeToken;

static void Main(string[] args)
{
_fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "."));
WatchForFileChanges();

Console.ReadKey(false);
}

private static void WatchForFileChanges()
{
_fileChangeToken = _fileProvider.Watch("*.*");
_fileChangeToken.RegisterChangeCallback(Notify, default);
}

private static void Notify(object state)
{
Console.WriteLine("File change detected");
WatchForFileChanges();
}
}
}

FileSystemWatcher locks folder

I've discovered what caused this. There was a second FileSystemWatcher - on a sub directory of the first - which didn't allow renaming the first.

(I'm still surprised, though. A FileSystemWatcher should be "invisible".)



Related Topics



Leave a reply



Submit