Skip to content

Commit

Permalink
Make PhysicalFilesWatcher exclusively use polling when pollForChanges…
Browse files Browse the repository at this point in the history
…=true and no FSW is provided (#41426)

When a FileSystemWatcher is not passed to the PhysicalFileWatcher
we'll permit construction (if polling is enabled) and have it behave as
exclusively polling.

The PhysicalFileProvider will use this mode when both
UsePollingFileWatcher and UseActivePolling are set which is what happens
when the DOTNET_USE_POLLING_FILE_WATCHER environment variable is set.
  • Loading branch information
ericstj authored Nov 12, 2020
1 parent 731dee4 commit 457ed11
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ internal PhysicalFilesWatcher FileWatcher
internal PhysicalFilesWatcher CreateFileWatcher()
{
string root = PathUtils.EnsureTrailingSlash(Path.GetFullPath(Root));
return new PhysicalFilesWatcher(root, new FileSystemWatcher(root), UsePollingFileWatcher, _filters)

// When both UsePollingFileWatcher & UseActivePolling are set, we won't use a FileSystemWatcher.
FileSystemWatcher watcher = UsePollingFileWatcher && UseActivePolling ? null : new FileSystemWatcher(root);
return new PhysicalFilesWatcher(root, watcher, UsePollingFileWatcher, _filters)
{
UseActivePolling = UseActivePolling,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,23 @@ public PhysicalFilesWatcher(
bool pollForChanges,
ExclusionFilters filters)
{
if (fileSystemWatcher == null && !pollForChanges)
{
throw new ArgumentNullException(nameof(fileSystemWatcher), SR.Error_FileSystemWatcherRequiredWithoutPolling);
}

_root = root;
_fileWatcher = fileSystemWatcher;
_fileWatcher.IncludeSubdirectories = true;
_fileWatcher.Created += OnChanged;
_fileWatcher.Changed += OnChanged;
_fileWatcher.Renamed += OnRenamed;
_fileWatcher.Deleted += OnChanged;
_fileWatcher.Error += OnError;

if (fileSystemWatcher != null)
{
_fileWatcher = fileSystemWatcher;
_fileWatcher.IncludeSubdirectories = true;
_fileWatcher.Created += OnChanged;
_fileWatcher.Changed += OnChanged;
_fileWatcher.Renamed += OnRenamed;
_fileWatcher.Deleted += OnChanged;
_fileWatcher.Error += OnError;
}

PollForChanges = pollForChanges;
_filters = filters;
Expand Down Expand Up @@ -361,27 +370,33 @@ private void ReportChangeForMatchedEntries(string path)

private void TryDisableFileSystemWatcher()
{
lock (_fileWatcherLock)
if (_fileWatcher != null)
{
if (_filePathTokenLookup.IsEmpty &&
_wildcardTokenLookup.IsEmpty &&
_fileWatcher.EnableRaisingEvents)
lock (_fileWatcherLock)
{
// Perf: Turn off the file monitoring if no files to monitor.
_fileWatcher.EnableRaisingEvents = false;
if (_filePathTokenLookup.IsEmpty &&
_wildcardTokenLookup.IsEmpty &&
_fileWatcher.EnableRaisingEvents)
{
// Perf: Turn off the file monitoring if no files to monitor.
_fileWatcher.EnableRaisingEvents = false;
}
}
}
}

private void TryEnableFileSystemWatcher()
{
lock (_fileWatcherLock)
if (_fileWatcher != null)
{
if ((!_filePathTokenLookup.IsEmpty || !_wildcardTokenLookup.IsEmpty) &&
!_fileWatcher.EnableRaisingEvents)
lock (_fileWatcherLock)
{
// Perf: Turn off the file monitoring if no files to monitor.
_fileWatcher.EnableRaisingEvents = true;
if ((!_filePathTokenLookup.IsEmpty || !_wildcardTokenLookup.IsEmpty) &&
!_fileWatcher.EnableRaisingEvents)
{
// Perf: Turn off the file monitoring if no files to monitor.
_fileWatcher.EnableRaisingEvents = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Error_FileSystemWatcherRequiredWithoutPolling" xml:space="preserve">
<value>The fileSystemWatcher parameter must be non-null when pollForChanges is false.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders.Internal;
using Microsoft.Extensions.FileProviders.Physical;
Expand All @@ -16,6 +18,7 @@ namespace Microsoft.Extensions.FileProviders
public class PhysicalFileProviderTests
{
private const int WaitTimeForTokenToFire = 500;
private const int WaitTimeForTokenCallback = 10000;

[Fact]
public void GetFileInfoReturnsNotFoundFileInfoForNullPath()
Expand Down Expand Up @@ -87,6 +90,54 @@ public void GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes_U
GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes(path);
}

[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public void PollingFileProviderShouldntConsumeINotifyInstances()
{
List<IDisposable> disposables = new List<IDisposable>();
using (var root = new DisposableFileSystem())
{
string maxInstancesFile = "/proc/sys/fs/inotify/max_user_instances";
Assert.True(File.Exists(maxInstancesFile));
int maxInstances = int.Parse(File.ReadAllText(maxInstancesFile));

// choose an arbitrary number that exceeds max
int instances = maxInstances + 16;

AutoResetEvent are = new AutoResetEvent(false);

var oldPollingInterval = PhysicalFilesWatcher.DefaultPollingInterval;
try
{
PhysicalFilesWatcher.DefaultPollingInterval = TimeSpan.FromMilliseconds(WaitTimeForTokenToFire);
for (int i = 0; i < instances; i++)
{
PhysicalFileProvider pfp = new PhysicalFileProvider(root.RootPath)
{
UsePollingFileWatcher = true,
UseActivePolling = true
};
disposables.Add(pfp);
disposables.Add(pfp.Watch("*").RegisterChangeCallback(_ => are.Set(), null));
}

// trigger an event
root.CreateFile("test.txt");

// wait for at least one event.
Assert.True(are.WaitOne(WaitTimeForTokenCallback));
}
finally
{
PhysicalFilesWatcher.DefaultPollingInterval = oldPollingInterval;
foreach (var disposable in disposables)
{
disposable.Dispose();
}
}
}
}

private void GetFileInfoReturnsNotFoundFileInfoForIllegalPathWithLeadingSlashes(string path)
{
using (var provider = new PhysicalFileProvider(Path.GetTempPath()))
Expand Down Expand Up @@ -1479,5 +1530,30 @@ public void CreateFileWatcher_CreatesWatcherWithPollingAndActiveFlags()
Assert.True(fileWatcher.UseActivePolling);
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public async Task CanDeleteWatchedDirectory(bool useActivePolling)
{
using (var root = new DisposableFileSystem())
using (var provider = new PhysicalFileProvider(root.RootPath))
{
var fileName = Path.GetRandomFileName();
PollingFileChangeToken.PollingInterval = TimeSpan.FromMilliseconds(10);

provider.UsePollingFileWatcher = true; // We must use polling due to https://github.com/dotnet/runtime/issues/44484
provider.UseActivePolling = useActivePolling;

root.CreateFile(fileName);
var token = provider.Watch(fileName);
Directory.Delete(root.RootPath, true);

await Task.Delay(WaitTimeForTokenToFire).ConfigureAwait(false);

Assert.True(token.HasChanged);
}
}
}
}

0 comments on commit 457ed11

Please sign in to comment.