-
Notifications
You must be signed in to change notification settings - Fork 233
Fix Double Publish with PublishAsync #149
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
Conversation
| Action publishAction = () => { PublishInternal<TMessage>(message); }; | ||
|
|
||
| await Task.Run(publishAction.Invoke); | ||
| PublishInternal<TMessage>(message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simplify this into something like this? We should keep this simple and Publish Internal doesn't need to be async as it's a synchronous operation and can run inline (the callback can be awaited).
PublishInternal<TMessage>(message);
// invoke callback here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You right! Since the async void can't be awaited and is asynchronously therefor. I personally don't like async void at all but in this case it might make sense to use it for readability and performance reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prior to the mistake in my PR, the original code was async via BeginInvoke (and still is for the not NETSTANDARD case). As I understand it, BeginInvoke queues to a Thread Pool. The only way to be synchronous is to “join” with the EndInvoke method.
Unless you want to change that aspect of the Publish method, I think something needs to occur to avoid callbacks blocking the caller thread.
https://devblogs.microsoft.com/dotnet/migrating-delegate-begininvoke-calls-for-net-core/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@niemyjski, @gkarabin. Thanks for the quick and good feedback. I've tried to simplify the method as much as possible while keeping the interface. With the current void method I don't think there's a simpler way than using Task.Run for doing the publishing asynchronously.
Having async Tasks with cancellation support for asynchronous methods would be great but the refactoring would exceed the scope of a #148 bugfix for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the link! I think this looks good with the current signature (fire and forget). Are you happy with me merging this pr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'm absolute happy if this makes it into the code. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me!
|
Thanks for the PR! I left some comments :) |
|
Ups, just got reminded by the compiler that
|
This probably fixes a different behavior change, in that .net framework would not observe any exceptions when using BeginInvoke. EndInvoke, which wasn’t called, would be needed for that. Awaiting the Task would cause exceptions to be observed. This most recent change returns that behavior. |
The exception would be "visible" inside the |
|
Merged! Thanks again for the pr! If I could get some help with #136 that would be a massive help for pushing a release with this. |
Messages are published twice but the callback is never invoked when using
PublishAsync.Fixes #148