Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void StopRaisingEvents()
{
_cancellation = null;
token.Cancel();
token.Dispose();

@carlossanlop carlossanlop Sep 27, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wfurt I have a question: why do we only cancel and dispose the token here? The RunningInstance class received a copy of this token in its constructor, so why not perform these two actions inside CancellationCallback too? (follow up of Marius' question).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CancellationCallback() did not do anything with the token. It is StartRaisingEvents() where token was created and symmetrically StopRaisingEvents() where token is destroyed. Existing _cancellation = null should be sufficient IMHO to dispose token. I only add explicit Dispose() call to make it explicit and not wait for GC. I did not want to introduce different life cycle for the token and worry about what changed.

}
}

Expand Down Expand Up @@ -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();
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Unix.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
<Link>Common\System\IO\TempDirectory.cs</Link>
</Compile>
</ItemGroup>
</Project>
<ItemGroup Condition=" '$(TargetsLinux)' == 'true' or '$(TargetsOSX)' == 'true' ">
<Compile Include="FileSystemWatcher.Unix.cs" />
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.ResourceLimits.cs">
<Link>Common\Interop\Unix\System.Native\Interop.ResourceLimits.cs</Link>
</Compile>
</ItemGroup>
</Project>