Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix file enumerator isn't thread safe #24295

Closed
Tracked by #93172 ...
JeremyKuhne opened this issue Nov 30, 2017 · 10 comments · Fixed by #59790
Closed
Tracked by #93172 ...

Unix file enumerator isn't thread safe #24295

JeremyKuhne opened this issue Nov 30, 2017 · 10 comments · Fixed by #59790
Labels
area-System.IO disabled-test The test is disabled in source code against the issue help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@JeremyKuhne
Copy link
Member

New test in dotnet/corefx#25426 uncovered this.

Unhandled Exception of Type System.NullReferenceException
Message :
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace :
   at Interop.Sys.ReadDir(SafeDirectoryHandle dir, DirectoryEntry& outputEntry)
   at System.IO.UnixFileSystem.FileSystemEnumerable`1.<Enumerate>d__11.MoveNext()
   at System.IO.Tests.Regressions.ThreadSafeRepro.Race(IEnumerator`1 s) in /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/System.IO.FileSystem/tests/Directory/Regressions.cs:line 30
   at System.IO.Tests.Regressions.ThreadSafeRepro.Execute(String directory) in /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/System.IO.FileSystem/tests/Directory/Regressions.cs:line 53
   at System.IO.Tests.Regressions.FileEnumeratorIsThreadSafe() in /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/System.IO.FileSystem/tests/Directory/Regressions.cs:line 21
@JeremyKuhne JeremyKuhne self-assigned this Nov 30, 2017
@stephentoub
Copy link
Member

stephentoub commented Nov 30, 2017

It doesn't need to be; thread safety isn't a requirement of an enumerator. In fact, it's impossible to have an enumerator that can be correctly accessed by multiple threads concurrently, because there's no way for the implementation to make the separate MoveNext/Current calls atomic. If you access such enumerators from multiple threads, you're likely to get exceptions, including null refs. What we need to ensure is that you can't violate type safety or cause memory corruption with such access, and I see no evidence of that being a problem here. If there is such a problem, we need to fix it, though... do you see such a problem?

@JeremyKuhne
Copy link
Member Author

Windows never fails. Weird results, but it never throws- shouldn't we meet the same bar here? @jkotas, any thoughts?

@stephentoub
Copy link
Member

stephentoub commented Nov 30, 2017

Weird results, but it never throws- shouldn't we meet the same bar here?

That's not necessary. Tons of instance methods across the framework throw strange exceptions if the methods are used incorrectly.

@jkotas
Copy link
Member

jkotas commented Nov 30, 2017

There is no problem to fix here. I have said a similar thing as Stephen in dotnet/corefx#25426 (comment) :

Race condition in (safe) C# can manifest as odd managed exception, but it should not ever turn into (exploitable) buffer overrun.

Clean System.NullReferenceException is not a memory corruption.

@jkotas jkotas closed this as completed Nov 30, 2017
@JeremyKuhne
Copy link
Member Author

So what should I check or allow?

@JeremyKuhne JeremyKuhne reopened this Nov 30, 2017
@jkotas
Copy link
Member

jkotas commented Nov 30, 2017

You should allow any clean exceptions to be thrown. But the test should not tear down the process (it is what happens in .NET Core unconditionally on memory corruptions that lead to AV).

@JeremyKuhne
Copy link
Member Author

So you're suggesting I try {} catch {} around the entire thing? Note that I did find other issues with handle recycling on this test.

@jkotas
Copy link
Member

jkotas commented Nov 30, 2017

try {} catch {} would be my baseline solution.

Making it more fine grained is fine too e.g. flagging specific exceptions that can be only triggered by handle recycling bug as failures is fine.

@JeremyKuhne
Copy link
Member Author

Ok, thanks.

@stephentoub
Copy link
Member

@JeremyKuhne, the test is still disabled on Unix against this issue:

[Fact]
[ActiveIssue("https://github.com/dotnet/corefx/issues/25613", TestPlatforms.AnyUnix)]
public void FileEnumeratorIsThreadSafe()

@stephentoub stephentoub reopened this Jan 23, 2020
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@jkotas jkotas modified the milestones: 2.1.0, 5.0 Mar 1, 2020
@JeremyKuhne JeremyKuhne removed the untriaged New issue has not been triaged by the area owner label Mar 3, 2020
@JeremyKuhne JeremyKuhne removed their assignment Mar 25, 2020
@carlossanlop carlossanlop modified the milestones: 5.0.0, 6.0.0 Jun 23, 2020
@adamsitnik adamsitnik modified the milestones: 6.0.0, 7.0.0 Jul 22, 2021
@adamsitnik adamsitnik added the help wanted [up-for-grabs] Good issue for external contributors label Jul 22, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Sep 30, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Oct 1, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.IO disabled-test The test is disabled in source code against the issue help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants