Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

improve stability of FileSystemWatcher tests - #40543

Merged
wfurt merged 8 commits into
dotnet:masterfrom
wfurt:watcher_38815
Sep 25, 2019
Merged

wfurt merged 8 commits into
dotnet:masterfrom
wfurt:watcher_38815

Conversation

@wfurt

@wfurt wfurt commented Aug 23, 2019

Copy link
Copy Markdown
Member

This is attempt to stabilize FileSystemWatcher test. There were ~400 failures in last 90 days and I could primarily reproduce failure of FileSystemWatcher_File_Create_WatchOwnPath and FileSystemWatcher_File_Create_NotAffectEachOther.

I originally tinker with delays and timeouts. That look like improvement but tests were still failing and I could not get past 1000 runs without failure. I could not reproduce it with running single test or while running under strace.

At some point I used inotify-tools and provided some clues:

inotifywait -r -m /tmp  -e create -e delete
...
...
Couldn't watch new directory /tmp/FileSystemWatcherTests_r1nateep.pmj/FileSystemWatcher_CreateManyConcurrentWatches_560_112cc1f4/36947: No space left on device
/tmp/FileSystemWatcherTests_r1nateep.pmj/FileSystemWatcher_CreateManyConcurrentWatches_560_112cc1f4/ CREATE,ISDIR 36948

The "no space" message is confusing as it suggests tests exhausted /tmp space. However inotify_add_watch can fail with ENOSPC if user limit is reached. Our implementation treats that as special and instead of throwing it will call Error delegate. So I added one and sure enough the error would show up:

Microsoft.DotNet.XUnitConsoleRunner v2.5.0 (64-bit .NET Core 5.0.0)
  Discovering: System.IO.FileSystem.Watcher.Tests (method display = ClassAndMethod, method display options = None)
  Discovered:  System.IO.FileSystem.Watcher.Tests (found 163 of 176 test cases)
  Starting:    System.IO.FileSystem.Watcher.Tests (parallel test collections = on, max threads = 4)
Watcher failed: System.IO.IOException: The configured user limit (65536) on the number of inotify watches has been reached, or the operating system failed to allocate a required resource. source=System.IO.FileSystemWatcher 63403007
Watcher failed: System.IO.IOException: The configured user limit (65536) on the number of inotify watches has been reached, or the operating system failed to allocate a required resource. source=System.IO.FileSystemWatcher 4916187
      Assert.True() Failure
      Expected: True
      Actual:   False
      Stack Trace:
        /home/furt/github/wfurt-corefx-h2/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.MultipleWatchers.cs(160,0): at System.IO.Tests.FileSystemWatcher_Multiple_Test.<FileSystemWatcher_File_Create_WatchOwnPath>b__4_0()
        /home/furt/github/wfurt-corefx-h2/src/System.IO.FileSystem.Watcher/tests/Utility/FileSystemWatcherTest.cs(198,0): at System.IO.Tests.FileSystemWatcherTest.ExecuteWithRetry(Action action, Int32 maxAttempts)
        /home/furt/github/wfurt-corefx-h2/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.MultipleWatchers.cs(138,0): at System.IO.Tests.FileSystemWatcher_Multiple_Test.FileSystemWatcher_File_Create_WatchOwnPath()
  Finished:    System.IO.FileSystem.Watcher.Tests

With that I look back at the inotifywait failure and FileSystemWatcher_CreateManyConcurrentWatches test:

  [OuterLoop("This test has high system resource demands and may cause failures in other concurrent tests")]
  public void FileSystemWatcher_CreateManyConcurrentWatches()

That test already claims it can impact other tests and it does. It creates situation where user limit is exhausted and then any other test (or any other independent application) can silently fail. Without that test I could pass 5000 outerloop runs without any failure!

It seems like this test is for interesting boundary condition but even if all tests are executed with parallelism, it can negatively impact other processes on system. So I made change to make it visible and easy for developer to run it as needed:

   Starting:    System.IO.FileSystem.Watcher.Tests (parallel test collections = on, max threads = 4)
      System.IO.Tests.FileSystemWatcherTests.FileSystemWatcher_CreateManyConcurrentWatches [SKIP]
        Refusing to run without COREFX_IO_RUNDANGEROUSTESTS set.
    Finished:    System.IO.FileSystem.Watcher.Tests
  === TEST EXECUTION SUMMARY ===
     System.IO.FileSystem.Watcher.Tests  Total: 293, Errors: 0, Failed: 0, Skipped: 1, Time: 3.378s

Hopefully the hint would be enough to hint to env variable.

As part of this I was also curious why the retry did not work since this is rare condition. The answer is very simple as it excludes all XUnit exception so when we fail Assert it does nothing. Since it looks like confusing useless craft I decided to remove it from test sets I was working on. Instead I left OnError callback so we would have some visibility to failures.

fixes #38815
fixes #34361

@wfurt wfurt added area-System.IO test enhancement Improvements of test source code labels Aug 23, 2019
@wfurt wfurt self-assigned this Aug 23, 2019

@JeremyKuhne JeremyKuhne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a nit on the conditional fact attribute

@wfurt

wfurt commented Aug 25, 2019

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

[Fact]
public void FileSystemWatcher_File_Create_ExecutionContextFlowed()
{
ExecuteWithRetry(() =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are you removing the retry?

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.

I try to explain it description. It does nothing for cases where we fail on assert. And that is 100% failures I've seen with those tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And that is 100% failures I've seen with those tests.

Really? We've added such retries in the FileSystemWatcher tests because the underlying functionality in the OS can be lossy. I'm concerned that by removing these retries, we'll find a new sporadic failure mode emerge.

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.

The problem is that in such cases we hit assert to get event. And in such cases the retry logic does not kick in anyway. I was originally expecting to see retry with failures.

ExecuteWithRetry:()
...
 catch (Exception e) when (!(e is XunitException) && retry < maxAttempts - 1)

@stephentoub stephentoub Aug 26, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then isn't the fix to change how we handle that failure, rather than getting rid of the retries? Or to remove that clause from the catch?

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.

I was thinking about retrying also with XUnit exception. As part of my tries I did that to see if the retry would actually make tests more stable. And it did but I still saw some failures.

Without the FileSystemWatcher_CreateManyConcurrentWatches I did thousands of runs without seeing any failure. This may not be representative of all CI systems but my current think is to give it try without retries. (and it is already unstable outerloop test) I can watch test result database and investigate if we see failures.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok. Please put the retries back if you see any failures they could have prevented.

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.

yes, I will. My goal is to get outerloop green most of the time. And I can see test history via Kusto query.

{
public abstract partial class FileSystemWatcherTest : FileCleanupTestBase
{
private const string DangerousTestEnvName = "COREFX_IO_RUNDANGEROUSTESTS";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is basically going to mean these tests are never run.

Is the goal here really to avoid running them in CI? There's a trait for that specifically.

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.

Would that report it is skip to test result database or would it just not run silently? I know we do not pay attention that this yet but that may impact reporting in future. Also that still leaves possibly for other test failing randomly for developers.

[Fact]
[ConditionalFact(nameof(RunDangerousTests))]
[PlatformSpecific(TestPlatforms.Linux)] // Reads MaxUsersWatches from Linux OS files
[OuterLoop("This test has high system resource demands and may cause failures in other concurrent tests")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you try the xunit feature that's supposed to let a test run serialized even if the rest of the tests are allowed to run in parallel with each other?

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.

No, I have not. I'm somewhat concern that this impacts rest of the system beyond other tests . That is reason why I wanted to make the skip somewhat more visible.

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.

I moved this to separate no-parallel class instead of making this conditional. That can still possibly break some OS functions but it should not impact other tests as they will not run at the same time.

@wfurt

wfurt commented Aug 27, 2019

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

@wfurt

wfurt commented Sep 24, 2019

Copy link
Copy Markdown
Member Author

I end up disabling parallelization for the one test exhausting all available watchers.
It would be great if you can take another look @stephentoub so we can get it in.
I'll watch for failures and I'll investigate if retry would help if we see more failures in future.

{
_output.WriteLine($"Watcher failed: {e.GetException()} source={source} {source.GetHashCode()}");
// Repeat on Console so it easier to triage in CI.
Console.WriteLine($"Watcher failed: {e.GetException()} source={source} {source.GetHashCode()}");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit:

string msg = $"Watcher failed: {e.GetException()} source={source} {source.GetHashCode()}";
_output.WriteLine(msg);
Console.WriteLine(msg);

@wfurt

wfurt commented Sep 25, 2019

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 4 pipeline(s).

@wfurt
wfurt merged commit e8fd893 into dotnet:master Sep 25, 2019
@wfurt
wfurt deleted the watcher_38815 branch September 25, 2019 21:30
ViktorHofer pushed a commit to ViktorHofer/corefx that referenced this pull request Sep 26, 2019
* improve stability of watcher tests

* feedback from review

* more feedback

* disable parallelization for dangerous tests

* enable FileSystemWatcher_File_Create_ForceLoopRestart  again

* feedback from review
stephentoub pushed a commit that referenced this pull request Sep 27, 2019
* improve stability of watcher tests

* feedback from review

* more feedback

* disable parallelization for dangerous tests

* enable FileSystemWatcher_File_Create_ForceLoopRestart  again

* feedback from review
@wfurt

wfurt commented Oct 14, 2019

Copy link
Copy Markdown
Member Author

We did not see Linux failure since this was submitted. There are still OSX failures. I'll take another look as underlying reason may be different.

@karelz karelz added this to the 5.0 milestone Dec 19, 2019
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* improve stability of watcher tests

* feedback from review

* more feedback

* disable parallelization for dangerous tests

* enable FileSystemWatcher_File_Create_ForceLoopRestart  again

* feedback from review


Commit migrated from dotnet/corefx@e8fd893
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.IO test enhancement Improvements of test source code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants