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

proposal: throw exception if Yield.ReturnAsync called before it's ready #32

Open
zivkan opened this issue Feb 2, 2019 · 3 comments
Open

Comments

@zivkan
Copy link

zivkan commented Feb 2, 2019

A user of your library posted this question on stack overflow.

Basically, they're using AsyncEnumerable<T>.ForEachAsync, but rather than producing items serially, they tried to produce concurrently by creating multiple concurrent generator tasks within the delegate that's passed into the AsyncEnumerable constructor. In other words, they were trying to use this library for the multi producer, single consumer pattern.

The problem is that the incorrect usage did not result in an error, but instead their program hung, and they didn't understand why. I believe it's related to calling Yield.ReturnAsync multiple times, before the previous task returned by ReturnAsync is in a completed state, causing the last call to overwrite information from the previous calls.

I believe if Yield.ReturnAsync throws an InvalidOperationException when it detects a call when the previous call is not completed, then it would allow your users to more quickly detect problems in their own code.

@kind-serge
Copy link
Member

@zivkan, thanks for the suggestion. You are absolutely right about the sequential producer pattern. This might not be obvious to everyone, however, there are many more standard types that don't support concurrent programming model - take Dictionary<,> for example.

It is possible to add protection from parallel produce attempts, but it comes with a performance cost that would impact the majority of normal cases. On the other hand, we can update the documentation to explicitly call out this point.

What are your thoughts?

@kind-serge
Copy link
Member

P.S. I replied to the SO question as well:
https://stackoverflow.com/questions/54402544/c-sharp-asyncenumerable-running-awaiting-multiple-tasks-never-finishes/56189780#56189780
I've been thinking about adding a helper method for such scenarios with concurrent producers/consumers.

@zivkan
Copy link
Author

zivkan commented May 18, 2019

I completely understand the performance concern. I think a helper method sounds best, but docs are good too. But honestly, I don't use your library, I was just trying to help out after investigating an interesting Stack Overflow question :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants