-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
LINQ, Union followed by Select on empty IEnumerable fails to invoke Dispose #30607
Comments
This was first asked on Stack Overflow, so I did some digging.
When you call The .NET Framework implementation also relies on Here are reproductions of it: |
Thanks for the report. Would you care to submit a PR? |
Sorry about the radio silence on this. I thought I might have time to give it a try, but it just didn't happen. I'm glad someone picked it up. |
Fixed in dotnet/corefx#40384 |
Is there enough test coverage to ensure that disposing happens reliably? Some sequences hold up resources such as database connections or file handles. If those are dropped this can be a nasty surprise (especially suddenly under load in production). I understand that the LINQ implementations have become a lot more complex internally due to various optimizations. There's now more potential for forgetting to dispose. Most queries are in-memory so this would not be noticed. Update: I now see the referenced pull request that appears to add comprehensive coverage. 🍾 |
It is my understanding that LINQ should always call
IEnumerator
'sDispose
method when finished with anIEnumerator
. I've found a case where this does not happen: Take twoIEnumerator
s that immediately returnfalse
in theirMoveNext()
implementations (i.e. they are empty),Union
them and then perform aSelect
on that. Here is a minimal reproduction that showsDispose()
is not called for eitherIEnumerator
. This reproduces in dotnet core 2.1 & 2.2. It does not reproduce in .NET Framework 4.7.2.If the
Dispose()
was getting called you would see "I got disposed" twice on the console. Instead you get no "I got disposed"s. TheUnion
and theSelect
are required to reproduce the issue.The text was updated successfully, but these errors were encountered: