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

Serialize the dispose of Zip(IEnumerable) with MoveNext/Current #1079

Merged
merged 4 commits into from Nov 19, 2019

Conversation

akarnokd
Copy link
Collaborator

IEnumerator.Dispose should not be called while MoveNext or Current is executing as most implementations don't expect a concurrent dispose call I think.

This PR adds a basic trampoline around the calls to MoveNext and Current. This allows a mutual exclusion between them and Dispose while also remembering Dispose should be eventually called:

  1. The operator's Dispose is invoked and _enumerationInProgress atomically updates to 1: no concurrent MoveNext is happening and the enumerator can be disposed. If OnNext runs at this point _enumerationInProgress atomically updates to 2 and the method quits without doing anything.
  2. OnNext atomically changes _enumerationInProgress to 1 and calls MoveNext. A concurrent Dispose on the operator now updates _enumerationInProgress to 2 and doesn't dispose the enumerator. When MoveNext finishes, it decrements _enumerationInProgress and since this is non-zero, it knows to dispose the enumerator and quit without doing any further processing.

Fixes #132

@danielcweber danielcweber reopened this Nov 19, 2019
@danielcweber danielcweber merged commit c61438e into dotnet:master Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enumerable overload of Zip does not synchronize enumerator disposal
2 participants