diff --git a/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs b/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs
index 7298e00f7f81..4cd82557d481 100644
--- a/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs
+++ b/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs
@@ -72,6 +72,7 @@ private void StopRaisingEvents()
{
_cancellation = null;
token.Cancel();
+ token.Dispose();
}
}
@@ -259,18 +260,21 @@ private static void WatchForFileSystemEventsThreadStart(object args)
private void CancellationCallback()
{
- if (!_stopping && _eventStream != null)
+ SafeEventStreamHandle eventStream = _eventStream;
+ if (!_stopping && eventStream != null)
{
_stopping = true;
+ _eventStream = null;
try
{
// When we get here, we've requested to stop so cleanup the EventStream and unschedule from the RunLoop
- Interop.EventStream.FSEventStreamStop(_eventStream);
+ Interop.EventStream.FSEventStreamStop(eventStream);
}
finally
{
- StaticWatcherRunLoopManager.UnscheduleFromRunLoop(_eventStream);
+ StaticWatcherRunLoopManager.UnscheduleFromRunLoop(eventStream);
+ eventStream.Close();
}
}
}
diff --git a/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Unix.cs b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Unix.cs
new file mode 100644
index 000000000000..615706d692f7
--- /dev/null
+++ b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Unix.cs
@@ -0,0 +1,56 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Threading;
+using Microsoft.DotNet.XUnitExtensions;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace System.IO.Tests
+{
+ public partial class DangerousFileSystemWatcherTests
+ {
+ [ConditionalFact]
+ [OuterLoop("Slow test with significant resource usage.")]
+ public void FileSystemWatcher_Unix_DoesNotLeak()
+ {
+ Interop.Sys.GetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, out Interop.Sys.RLimit limits);
+ _output.WriteLine("File descriptor limit is {0}", limits.CurrentLimit);
+
+ if (limits.CurrentLimit > 50_000)
+ {
+ throw new SkipTestException($"File descriptor limit is too high {limits.CurrentLimit}.");
+ }
+
+ try {
+ for (int i = 0; i < (int)limits.CurrentLimit; i++)
+ {
+ using (var watcher = new FileSystemWatcher(TestDirectory))
+ {
+ watcher.Created += (s, e) => { } ;
+ watcher.EnableRaisingEvents = true;
+ }
+
+ // On some OSes system resource is released in finalizer.
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ }
+ }
+ catch (Exception e)
+ {
+ // If we use all handles we may not have luck writing out errors.
+ // Try our best here.
+ Console.WriteLine(e.Message);
+ _output.WriteLine(e.Message);
+ throw;
+ }
+ }
+ }
+}
diff --git a/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs
index 2dd0613f2398..66ca7c3a7f92 100644
--- a/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs
+++ b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs
@@ -1088,8 +1088,15 @@ public void FileSystemWatcher_ModifyFiltersConcurrentWithEvents()
}
[Collection("NoParallelTests")]
- public class DangerousFileSystemWatcherTests : FileSystemWatcherTest
+ public partial class DangerousFileSystemWatcherTests : FileSystemWatcherTest
{
+ private readonly ITestOutputHelper _output;
+
+ public DangerousFileSystemWatcherTests(ITestOutputHelper output)
+ {
+ _output = output;
+ }
+
[PlatformSpecific(TestPlatforms.Linux)] // Reads MaxUsersWatches from Linux OS files
[OuterLoop("This test will use all available watchers and can cause failures in other concurrent tests or system processes.")]
[Fact]
diff --git a/src/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj b/src/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj
index 24421e5b1655..8bca04fe1232 100644
--- a/src/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj
+++ b/src/System.IO.FileSystem.Watcher/tests/System.IO.FileSystem.Watcher.Tests.csproj
@@ -32,4 +32,13 @@
Common\System\IO\TempDirectory.cs
-
\ No newline at end of file
+
+
+
+ Common\Interop\Unix\Interop.Libraries.cs
+
+
+ Common\Interop\Unix\System.Native\Interop.ResourceLimits.cs
+
+
+