Skip to content

Disposing the FileSystemWatcher does not close associated file descriptors #30784

Closed
dotnet/corefx
#41013

Description

@mrward

Filed this against Mono, which uses corefx, but the problem also affects corefx.

mono/mono#16709

Steps to Reproduce

Related problem - mono/mono#15931

  1. Sample project - https://github.com/mrward/test-file-watcher-dispose - multi-target project that builds .NET 4.7.2 and .NET Core 2.1
  2. Build this project.
  3. From the command line run dotnet bin/debug/netcoreapp2.1/FileWatcherDisposeTest.dll
  4. Run lsof -p pid. Process id is output from the console app.
  5. See that the file watcher creates file descriptors for several directories.
  6. After 10 seconds the file watcher will be disposed.
  7. Run lsof -p pid again.

Current Behavior

lsof still lists the directories.

dotnet 3972 user    4r DIR   1,12      170   51740695 /Volumes/Drive/projects/tests/FileWatcherDisposeTest/FileWatcherDisposeTest/bin/Debug/net472
dotnet 3972 user    5r DIR   1,12      136   51740600 /Volumes/Drive/projects/tests/FileWatcherDisposeTest/FileWatcherDisposeTest/bin/Debug
dotnet 3972 user    6r DIR   1,12      102   51740599 /Volumes/Drive/projects/tests/FileWatcherDisposeTest/FileWatcherDisposeTest/bin
dotnet 3972 user    7r DIR   1,12      204   51740594 /Volumes/Drive/projects/tests/FileWatcherDisposeTest/FileWatcherDisposeTest
dotnet 3972 user    8r DIR   1,12      170   51740593 /Volumes/Drive/projects/tests/FileWatcherDisposeTest
dotnet 3972 user    9r DIR   1,12     6324     185300 /Volumes/Drive/projects/tests
dotnet 3972 user   10r DIR   1,12     2312        278 /Volumes/Drive/projects
dotnet 3972 user   11r DIR   1,12      646          2 /Volumes/Drive
dotnet 3972 user   12r DIR    1,4      160     420284 /Volumes

If you change the console project to run GC.Collect after the dispose does not fix the problem (this does work with Mono)

Expected Behavior

File watcher directories not listed by lsof.

Note that

On which platforms did you notice this

[x] macOS
[ ] Linux
[ ] Windows

Version Used:

dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview9-014004
Commit: 8e7ef240a5

Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/3.0.100-preview9-014004/

Host (useful for support):
Version: 3.0.0-preview9-19423-09
Commit: 2be172345a

.NET Core SDKs installed:
2.1.701 [/usr/local/share/dotnet/sdk]
3.0.100-preview9-014004 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview9.19424.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview9-19423-09 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Stacktrace

Activity

  1. danmoseley commented on Sep 6, 2019

    @danmoseley
    Contributor
  2. scalablecory commented on Sep 7, 2019

    @scalablecory
    Contributor

    @wfurt was recently looking at FileSystemWatcher on OSX

  3. wfurt commented on Sep 7, 2019

    @wfurt
    Member

    I can take a look as well I can run repro on Linux for comparison. Also note that there was issue with Sockets that in some cases underlying handle would not be properly closed. That would show up if you target 2.1 even if you use 3.0 SDK.

  4. marek-safar commented on Sep 7, 2019

    @marek-safar
    Contributor

    @danmosemsft @karelz this is impacting VS4M, it would be great to prioritize the investigation so we know what the options are for us.

  5. Therzok commented on Sep 7, 2019

    @Therzok
    Contributor

    Profiling this, I found that the RunningInstance is being alive by these 2 paths. Sadly, there are no field names, as the mono profiler does not report field names.

    Screenshot 2019-09-07 at 13 55 37

    Screenshot 2019-09-07 at 13 55 34

  6. Therzok commented on Sep 7, 2019

    @Therzok
    Contributor

    These both keep the EventStreamSafeHandle and EventStreamCallback instances alive, leading to the native one not being released.

  7. Therzok commented on Sep 7, 2019

    @Therzok
    Contributor

    To note, the System.IO.CoreFX.FileSystemWatcher namespace is due to a change in mono:
    https://github.com/mono/corefx/blob/master/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs#L19-L24

  8. Therzok commented on Sep 7, 2019

    @Therzok
    Contributor
  9. danmoseley commented on Sep 7, 2019

    @danmoseley
    Contributor

    @Therzok if you modify the code to do that can you confirm it solves it?

  10. Therzok commented on Sep 10, 2019

    @Therzok
    Contributor

    @danmosemsft Don't have cycles available to take a look at it right now, sorry!

  11. danmoseley commented on Sep 10, 2019

    @danmoseley
    Contributor

    @Therzok not a problem, thanks for adding the information.

  12. slluis commented on Sep 10, 2019

    @slluis
    Contributor

    It would be great to have a quick fix for this. It has an important impact in VSMac.

  13. danmoseley commented on Sep 10, 2019

    @danmoseley
    Contributor

    @slluis could you tell us more about the impact? Is this somehow a new problem?

    What versions of .NET Core would VSMac require a fix in?

  14. mrward commented on Sep 10, 2019

    @mrward
    Author

    @danmosemsft The fix in corefx would be backported to Mono. Not sure what version of corefix Mono would be using. @steveisok may know more.

    Currently this causes VS Mac to keep file descriptors around after file watchers are disposed. Eventually this breaks Mono's networking stack, which has a limit of 1024 file descriptors.

  15. self-assigned this
    on Sep 10, 2019
  16. 5 remaining items

  17. Therzok commented on Sep 11, 2019

    @Therzok
    Contributor

    It fixes it!

  18. mrward commented on Sep 12, 2019

    @mrward
    Author

    The PR fixes the problem for console test app that uses one file watcher. If I create two or more then only the first seems to have its file descriptors closed.

    VS Mac running with Mono 6.4.0.194 (2019-06/7da9a041b3b Thu Sep 12 04:09:42 EDT 2019) which has the fix still has problems here.

    Will look at creating updating the sample project.

  19. mrward commented on Sep 12, 2019

    @mrward
    Author

    There is an updated test console project that creates more than file watcher - https://github.com/mrward/test-file-watcher-dispose

  20. Therzok commented on Sep 12, 2019

    @Therzok
    Contributor

    Validated the corefx change against local corefx:
    https://gist.github.com/Therzok/37489ee04505d5e1c4a2d43ca85abed9

    Definitely fixed here, the rest of the problems are in mono. Thank you @wfurt!

  21. danmoseley commented on Sep 17, 2019

    @danmoseley
    Contributor

    @steveisok can you confirm that VS for Mac does not need this back ported in corefx (eg., to 3.0.1) since evidently VS for Mac is consuming it out of the Mono sources?

  22. steveisok commented on Sep 18, 2019

    @steveisok
    Member

    Correct. We do not need it backported.

  23. wfurt commented on Sep 18, 2019

    @wfurt
    Member

    the extra code is to make sure Dispose() works concurrently from multiple threads. That may not be VS/Mac use case. I also put more notes to #30415 (just in case it matters for VS use)

  24. danmoseley commented on Sep 20, 2019

    @danmoseley
    Contributor

    @wfurt since VS for mac does not need a backport my suggestion is to close this and wait see whether we get customer feedback that justifies a backport. Taht also gives more time for it in master. If you agree -we can close this.

  25. wfurt commented on Sep 20, 2019

    @wfurt
    Member

    should we get this to master @danmosemsft ? I was waiting for somebody from core team to review this before merging to master.

  26. danmoseley commented on Sep 20, 2019

    @danmoseley
    Contributor

    Oh - yes for sure @wfurt. I missed that it wasn't in master yet.

  27. transferred this issue fromdotnet/corefxon Feb 1, 2020
  28. added this to the 5.0 milestone on Feb 1, 2020
  29. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions